1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.git.internal

File DefaultGitManager.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

6
29
3
1
136
84
7
0.24
9.67
3
2.33

Classes

Class Line # Actions
DefaultGitManager 55 29 0% 7 5
0.868421186.8%
 

Contributing tests

This file is covered by 2 tests. .

Source view

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.internal;
21   
22    import java.io.File;
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.Singleton;
29   
30    import org.eclipse.jgit.api.Git;
31    import org.eclipse.jgit.lib.PersonIdent;
32    import org.eclipse.jgit.lib.Repository;
33    import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
34    import org.gitective.core.CommitFinder;
35    import org.gitective.core.filter.commit.AndCommitFilter;
36    import org.gitective.core.filter.commit.AuthorDateFilter;
37    import org.gitective.core.filter.commit.AuthorSetFilter;
38    import org.gitective.core.filter.commit.CommitCountFilter;
39    import org.gitective.core.stat.AuthorHistogramFilter;
40    import org.gitective.core.stat.CommitCountComparator;
41    import org.gitective.core.stat.UserCommitActivity;
42    import org.slf4j.Logger;
43    import org.xwiki.component.annotation.Component;
44    import org.xwiki.environment.Environment;
45    import org.xwiki.git.GitManager;
46   
47    /**
48    * Provides services to access a Git repository by storing the data in the XWiki permanent directory.
49    *
50    * @version $Id: df3881da96a1f40d17b139018c8ccc570dbe68a2 $
51    * @since 5.3M2
52    */
53    @Component
54    @Singleton
 
55    public class DefaultGitManager implements GitManager
56    {
57    /**
58    * Required to get access to the Environment's permanent directory, where the Script service will clone Git
59    * repositories.
60    */
61    @Inject
62    private Environment environment;
63   
64    /**
65    * The logger to log.
66    */
67    @Inject
68    private Logger logger;
69   
 
70  2 toggle @Override
71    public Repository getRepository(String repositoryURI, String localDirectoryName)
72    {
73  2 Repository repository;
74   
75  2 File localGitDirectory = new File(this.environment.getPermanentDirectory(), "git");
76  2 File localDirectory = new File(localGitDirectory, localDirectoryName);
77  2 File gitDirectory = new File(localDirectory, ".git");
78  2 this.logger.debug("Local Git repository is at [{}]", gitDirectory);
79  2 FileRepositoryBuilder builder = new FileRepositoryBuilder();
80   
81  2 try {
82    // Step 1: Initialize Git environment
83  2 repository = builder.setGitDir(gitDirectory)
84    .readEnvironment()
85    .findGitDir()
86    .build();
87  2 Git git = new Git(repository);
88   
89    // Step 2: Verify if the directory exists and isn't empty.
90  2 if (!gitDirectory.exists()) {
91    // Step 2.1: Need to clone the remote repository since it doesn't exist
92  2 git.cloneRepository()
93    .setDirectory(localDirectory)
94    .setURI(repositoryURI)
95    .call();
96    }
97    } catch (Exception e) {
98  0 throw new RuntimeException(String.format("Failed to execute Git command in [%s]", gitDirectory), e);
99    }
100   
101  2 return repository;
102    }
103   
 
104  1 toggle @Override
105    public Set<PersonIdent> findAuthors(List<Repository> repositories)
106    {
107  1 CommitFinder finder = new CommitFinder(repositories);
108  1 AuthorSetFilter authors = new AuthorSetFilter();
109  1 finder.setFilter(authors).find();
110   
111  1 return authors.getPersons();
112    }
113   
 
114  1 toggle @Override
115    public UserCommitActivity[] countAuthorCommits(Date since, List<Repository> repositories)
116    {
117  1 if (repositories.isEmpty()) {
118  0 return new UserCommitActivity[0];
119    }
120   
121  1 CommitFinder finder = new CommitFinder(repositories);
122  1 CommitCountFilter countFilter = new CommitCountFilter();
123  1 AuthorHistogramFilter histogramFilter = new AuthorHistogramFilter();
124  1 AuthorSetFilter authorFilter = new AuthorSetFilter();
125  1 AndCommitFilter filters = new AndCommitFilter(countFilter, authorFilter, histogramFilter);
126   
127  1 if (since != null) {
128  1 AuthorDateFilter dateFilter = new AuthorDateFilter(since);
129  1 finder.setFilter(dateFilter);
130    }
131   
132  1 finder.setMatcher(filters).find();
133   
134  1 return histogramFilter.getHistogram().getUserActivity(new CommitCountComparator());
135    }
136    }