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

File GitHelper.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

8
27
5
1
93
66
9
0.33
5.4
5
1.8

Classes

Class Line # Actions
GitHelper 33 27 0% 9 8
0.880%
 

Contributing tests

No tests hitting this source file were found.

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;
21   
22    import java.io.File;
23    import java.io.PrintWriter;
24   
25    import org.eclipse.jgit.api.Git;
26    import org.eclipse.jgit.lib.PersonIdent;
27    import org.eclipse.jgit.lib.Repository;
28    import org.eclipse.jgit.revwalk.RevCommit;
29    import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
30    import org.junit.*;
31    import org.xwiki.environment.Environment;
32   
 
33    public class GitHelper
34    {
35    private Environment environment;
36   
 
37  2 toggle public GitHelper(Environment environment)
38    {
39  2 this.environment = environment;
40    }
41   
 
42  6 toggle public File getRepositoryFile(String repoName) throws Exception
43    {
44  6 File localGitDirectory = new File(this.environment.getPermanentDirectory(), "git");
45  6 File localDirectory = new File(localGitDirectory, repoName);
46  6 return localDirectory;
47    }
48   
 
49  0 toggle public boolean exists(String repoName) throws Exception
50    {
51  0 return getRepositoryFile(repoName).exists();
52    }
53   
 
54  2 toggle public Repository createGitTestRepository(String repoName) throws Exception
55    {
56  2 File localDirectory = getRepositoryFile(repoName);
57  2 File gitDirectory = new File(localDirectory, ".git");
58  2 FileRepositoryBuilder builder = new FileRepositoryBuilder();
59  2 Repository repository = builder.setGitDir(gitDirectory)
60    .readEnvironment()
61    .findGitDir()
62    .build();
63  2 if (!gitDirectory.exists()) {
64  2 repository.create();
65    }
66  2 return repository;
67    }
68   
 
69  2 toggle public void add(File repo, String path, String content, PersonIdent author, PersonIdent committer,
70    String message) throws Exception
71    {
72  2 File file = new File(repo.getParentFile(), path);
73  2 if (!file.getParentFile().exists()) {
74  0 Assert.assertTrue(file.getParentFile().mkdirs());
75    }
76  2 if (!file.exists()) {
77  2 Assert.assertTrue(file.createNewFile());
78    }
79  2 PrintWriter writer = new PrintWriter(file);
80  2 if (content == null)
81  0 content = "";
82  2 try {
83  2 writer.print(content);
84    } finally {
85  2 writer.close();
86    }
87  2 Git git = Git.open(repo);
88  2 git.add().addFilepattern(path).call();
89  2 RevCommit commit = git.commit().setOnly(path).setMessage(message).setAuthor(author).setCommitter(committer)
90    .call();
91  2 Assert.assertNotNull(commit);
92    }
93    }