@Autowired public ConfigRepository(SystemEnvironment systemEnvironment) throws IOException { this.systemEnvironment = systemEnvironment; workingDir = this.systemEnvironment.getConfigRepoDir(); File configRepoDir = new File(workingDir, ".git"); gitRepo = new FileRepositoryBuilder().setGitDir(configRepoDir).build(); gitRepo.getConfig().setInt("gc", null, "auto", 0); git = new Git(gitRepo); }
/** * @param gitConfigFolder e.g. /your/project/root/.git * * @return Returns true if 'git status' has modified files inside the 'Changes to be committed' section */ public static boolean isCommitNecessary( String gitConfigFolder ) throws MojoExecutionException { try { Repository repo = new FileRepository( gitConfigFolder ); Git git = new Git( repo ); Status status = git.status().call(); Set<String> modified = status.getModified(); return ( modified.size() != 0 ); } catch ( Exception e ) { throw new MojoExecutionException( "Error trying to find out if git commit is needed", e ); } }
/** * Wrap repository * * @param repo * the git repository this class is interacting with; * {@code null} is not allowed. * @return a {@link org.eclipse.jgit.api.Git} object for the existing git * repository. The caller is responsible for closing the repository; * {@link #close()} on this instance does not close the repo. */ public static Git wrap(Repository repo) { return new Git(repo); }
private static RevCommitsPair resolveRevCommitsPair(Repository repo) { RevCommitsPair revCommitIteratorPair; try (RevWalk revWalk = new RevWalk(repo); Git git = new Git(repo)) { final Iterator<RevCommit> first; final Iterator<RevCommit> second; final ObjectId headId = repo.resolve(Constants.HEAD); final RevCommit headCommit = revWalk.parseCommit(headId); if (isMergeCommit(headCommit)) { final RevCommit firstParent = headCommit.getParent(0); final RevCommit secondParent = headCommit.getParent(1); first = git.log().add(firstParent).call().iterator(); second = git.log().add(secondParent).call().iterator(); } else { first = git.log().call().iterator(); second = Collections.emptyIterator(); } revCommitIteratorPair = new RevCommitsPair(new OmitMergeCommitsIterator(first), new OmitMergeCommitsIterator(second)); } catch (GitAPIException | IOException ignored) { revCommitIteratorPair = new RevCommitsPair(); } return revCommitIteratorPair; }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { Properties ret = git.gc(). setProgressMonitor(new PrintlnProgressMonitor()).call(); for(Map.Entry<Object, Object> entry : ret.entrySet()) { System.out.println("Ret: " + entry.getKey() + ": " + entry.getValue()); } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { Properties ret = git.gc(). setProgressMonitor(new PrintlnProgressMonitor()).call(); for(Map.Entry<Object, Object> entry : ret.entrySet()) { System.out.println("Ret: " + entry.getKey() + ": " + entry.getValue()); } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { List<Ref> call = git.branchList().call(); for (Ref ref : call) { List<Integer> counts = getCounts(repository, ref.getName()); System.out.println("For branch: " + ref.getName()); System.out.println("Commits ahead : " + counts.get(0)); System.out.println("Commits behind : " + counts.get(1)); System.out.println(); } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { List<Ref> call = git.branchList().call(); for (Ref ref : call) { List<Integer> counts = getCounts(repository, ref.getName()); System.out.println("For branch: " + ref.getName()); System.out.println("Commits ahead : " + counts.get(0)); System.out.println("Commits behind : " + counts.get(1)); System.out.println(); } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { Iterable<RevCommit> commits = git.log().all().call(); int count = 0; for (RevCommit commit : commits) { System.out.println("LogCommit: " + commit); count++; } System.out.println(count); } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { Iterable<RevCommit> commits = git.log().all().call(); int count = 0; for (RevCommit commit : commits) { System.out.println("LogCommit: " + commit); count++; } System.out.println(count); } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { System.out.println("Starting fetch"); try (Git git = new Git(repository)) { FetchResult result = git.fetch().setCheckFetchedObjects(true).call(); System.out.println("Messages: " + result.getMessages()); } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { System.out.println("Starting fetch"); try (Git git = new Git(repository)) { FetchResult result = git.fetch().setCheckFetchedObjects(true).call(); System.out.println("Messages: " + result.getMessages()); } } } }
private static void listReflog(Repository repository, Ref ref) throws GitAPIException { /* * Ref head = repository.getRef(ref.getName()); * RevWalk walk = new RevWalk(repository); * RevCommit commit = walk.parseCommit(head.getObjectId()); */ try (Git git = new Git(repository)) { Collection<ReflogEntry> call = git.reflog().setRef(ref.getName()).call(); for (ReflogEntry reflog : call) { System.out.println("Reflog: " + reflog); } } } }
private static void listReflog(Repository repository, Ref ref) throws GitAPIException { /* * Ref head = repository.getRef(ref.getName()); * RevWalk walk = new RevWalk(repository); * RevCommit commit = walk.parseCommit(head.getObjectId()); */ try (Git git = new Git(repository)) { Collection<ReflogEntry> call = git.reflog().setRef(ref.getName()).call(); for (ReflogEntry reflog : call) { System.out.println("Reflog: " + reflog); } } } }
private static void addSubmodule(Repository mainRepo) throws GitAPIException { System.out.println("Adding submodule"); try (Git git = new Git(mainRepo)) { try (Repository subRepoInit = git.submoduleAdd(). setURI("https://github.com/github/testrepo.git"). setPath("testrepo"). call()) { if(subRepoInit.isBare()) { throw new IllegalStateException("Repository at " + subRepoInit.getDirectory() + " should not be bare"); } } } }
private static void addSubmodule(Repository mainRepo) throws GitAPIException { System.out.println("Adding submodule"); try (Git git = new Git(mainRepo)) { try (Repository subRepoInit = git.submoduleAdd(). setURI("https://github.com/github/testrepo.git"). setPath("testrepo"). call()) { if(subRepoInit.isBare()) { throw new IllegalStateException("Repository at " + subRepoInit.getDirectory() + " should not be bare"); } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (final Repository repo = CookbookHelper.openJGitCookbookRepository()) { try (final Git git = new Git(repo)) { final String testFile = "README.md"; // Modify the file FileUtils.write(new File(testFile), new Date().toString(), "UTF-8"); System.out.println("Modified files: " + getModifiedFiles(git)); new AssumeChangedCommand(repo, testFile, true).call(); System.out.println("Modified files after assume-changed: " + getModifiedFiles(git)); new AssumeChangedCommand(repo, testFile, false).call(); System.out.println("Modified files after no-assume-changed: " + getModifiedFiles(git)); git.checkout().addPath(testFile).call(); System.out.println("Modified files after reset: " + getModifiedFiles(git)); } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { List<Note> call = git.notesList().call(); System.out.println("Listing " + call.size() + " notes"); for (Note note : call) { System.out.println("Note: " + note + " " + note.getName() + " " + note.getData().getName() + "\nContent: "); // displaying the contents of the note is done via a simple blob-read ObjectLoader loader = repository.open(note.getData()); loader.copyTo(System.out); } } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { List<Note> call = git.notesList().call(); System.out.println("Listing " + call.size() + " notes"); for (Note note : call) { System.out.println("Note: " + note + " " + note.getName() + " " + note.getData().getName() + "\nContent: "); // displaying the contents of the note is done via a simple blob-read ObjectLoader loader = repository.open(note.getData()); loader.copyTo(System.out); } } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { List<Ref> refs = git.branchList().call(); for (Ref ref : refs) { System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); listReflog(repository, ref); } List<Ref> call = git.tagList().call(); for (Ref ref : call) { System.out.println("Tag: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); listReflog(repository, ref); } } } }