| Class | Line # | Actions | |||||
|---|---|---|---|---|---|---|---|
| GitScriptService | 63 | 8 | 0% | 5 | 1 |
| 1 | /* | |
| 2 | * See the NOTICE file distributed with this work for additional | |
| 3 | * information regarding copyright ownership. | |
| 4 | * | |
| 5 | * This is free software; you can redistribute it and/or modify it | |
| 6 | * under the terms of the GNU Lesser General Public License as | |
| 7 | * published by the Free Software Foundation; either version 2.1 of | |
| 8 | * the License, or (at your option) any later version. | |
| 9 | * | |
| 10 | * This software is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | * Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU Lesser General Public | |
| 16 | * License along with this software; if not, write to the Free | |
| 17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
| 18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
| 19 | */ | |
| 20 | package org.xwiki.git.script; | |
| 21 | ||
| 22 | import java.util.Arrays; | |
| 23 | import java.util.Date; | |
| 24 | import java.util.List; | |
| 25 | import java.util.Set; | |
| 26 | ||
| 27 | import javax.inject.Inject; | |
| 28 | import javax.inject.Named; | |
| 29 | import javax.inject.Singleton; | |
| 30 | ||
| 31 | import org.eclipse.jgit.lib.PersonIdent; | |
| 32 | import org.eclipse.jgit.lib.Repository; | |
| 33 | import org.gitective.core.stat.UserCommitActivity; | |
| 34 | import org.joda.time.DateTime; | |
| 35 | import org.xwiki.component.annotation.Component; | |
| 36 | import org.xwiki.git.GitManager; | |
| 37 | import org.xwiki.script.service.ScriptService; | |
| 38 | ||
| 39 | /** | |
| 40 | * Various APIs to make it easy to perform Git commands from within scripts. | |
| 41 | * <p> | |
| 42 | * Example usage from Velocity: | |
| 43 | * <pre><code> | |
| 44 | * {{velocity}} | |
| 45 | * #set ($calendar = $datetool.calendar) | |
| 46 | * #set ($now = $datetool.date) | |
| 47 | * #set ($discard = $calendar.add(6, -10)) | |
| 48 | * #set ($repository = $services.git.getRepository("https://github.com/xwiki/xwiki-commons.git", | |
| 49 | * "xwiki/xwiki-commons")) | |
| 50 | * #set ($data = $services.git.countAuthorCommits($calendar.getTime(), $repository)) | |
| 51 | * #foreach ($authorCommits in $data) | |
| 52 | * * $authorCommits.email = $authorCommits.count | |
| 53 | * #end | |
| 54 | * {{/velocity}} | |
| 55 | * </code></pre> | |
| 56 | * | |
| 57 | * @version $Id: c255fb90d9de5bc8d3b530bd781c6c24b291cd8a $ | |
| 58 | * @since 4.2M1 | |
| 59 | */ | |
| 60 | @Component | |
| 61 | @Named("git") | |
| 62 | @Singleton | |
| 63 | public class GitScriptService implements ScriptService | |
| 64 | { | |
| 65 | @Inject | |
| 66 | private GitManager gitManager; | |
| 67 | ||
| 68 | /** | |
| 69 | * Clone a Git repository by storing it locally in the XWiki Permanent directory. If the repository is already | |
| 70 | * cloned, no action is done. | |
| 71 | * | |
| 72 | * @param repositoryURI the URI to the Git repository to clone (eg "git://github.com/xwiki/xwiki-commons.git") | |
| 73 | * @param localDirectoryName the name of the directory where the Git repository will be cloned (this directory is | |
| 74 | * relative to the permanent directory | |
| 75 | * @return the cloned Repository instance | |
| 76 | */ | |
| 77 | 2 | public Repository getRepository(String repositoryURI, String localDirectoryName) |
| 78 | { | |
| 79 | 2 | return this.gitManager.getRepository(repositoryURI, localDirectoryName); |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Find all authors who have ever committed code in the passed repository. | |
| 84 | * | |
| 85 | * @param repositories the list of repositories in which to look for authors | |
| 86 | * @return the list of authors who have ever contributed code in the passed repository | |
| 87 | * @since 5.3M2 | |
| 88 | */ | |
| 89 | 1 | public Set<PersonIdent> findAuthors(Repository... repositories) |
| 90 | { | |
| 91 | 1 | return this.gitManager.findAuthors(Arrays.asList(repositories)); |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * Count commits done by all authors in the passed repositories and since the passed date. | |
| 96 | * | |
| 97 | * @param sinceDays the number of days to look back in the past or look from the beginning if set to 0 | |
| 98 | * @param repositories the list of repositories in which to look for commits | |
| 99 | * @return the author commit activity | |
| 100 | * @since 5.3M2 | |
| 101 | */ | |
| 102 | 1 | public UserCommitActivity[] countAuthorCommits(int sinceDays, Repository... repositories) |
| 103 | { | |
| 104 | 1 | return countAuthorCommits(sinceDays, Arrays.asList(repositories)); |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * Count commits done by all authors in the passed repositories and since the passed date. | |
| 109 | * | |
| 110 | * @param sinceDays the number of days to look back in the past or look from the beginning if set to 0 | |
| 111 | * @param repositories the list of repositories in which to look for commits | |
| 112 | * @return the author commit activity | |
| 113 | * @since 5.3M2 | |
| 114 | */ | |
| 115 | 1 | public UserCommitActivity[] countAuthorCommits(int sinceDays, List<Repository> repositories) |
| 116 | { | |
| 117 | 1 | Date date = null; |
| 118 | 1 | if (sinceDays > 0) { |
| 119 | // Compute today - since days | |
| 120 | 1 | DateTime now = new DateTime(); |
| 121 | 1 | date = now.minusDays(sinceDays).toDate(); |
| 122 | } | |
| 123 | 1 | return this.gitManager.countAuthorCommits(date, repositories); |
| 124 | } | |
| 125 | } |