@Signature public Memory resolveCommit(String revstr) throws IOException, GitAPIException { ObjectId objectId = getWrappedObject().getRepository().resolve(revstr); if (objectId == null) { return Memory.NULL; } LogCommand command = getWrappedObject() .log() .add(objectId) .setMaxCount(1); Iterable<RevCommit> call = command.call(); for (RevCommit revCommit : call) { return GitUtils.valueOf(revCommit); } return Memory.NULL; }
@Signature public Memory log(Environment env, ArrayMemory settings) throws Throwable { LogCommand log = getWrappedObject().log(); Repository repository = getWrappedObject().getRepository(); log.setMaxCount(maxCount.toInteger()); log.setSkip(skip.toInteger()); log.all(); } else { Memory range = settings.valueOfIndex("range"); log.addRange(sId, fId); } else { log.add(fId); ForeachIterator iterator = paths.getNewIterator(env); while (iterator.next()) { log.addPath(iterator.getValue().toString()); log.addPath(paths.toString()); return GitUtils.valueOfRevCommits(log.call());
public Long commitCountOnMaster() throws GitAPIException, IncorrectObjectTypeException, MissingObjectException { // not inside a doLocked/synchronized block because we don't want to block the server status service. // we do a `git branch` because we switch branches as part of normal git operations, // and we don't care about number of commits on those branches. List<Ref> branches = git.branchList().call(); for (Ref branch : branches) { if (branch.getName().equals("refs/heads/master")) { Iterable<RevCommit> commits = git.log().add(branch.getObjectId()).call(); long count = 0; for (RevCommit commit : commits) { count++; } return count; } } return Long.valueOf(-1); } }
public GoConfigRevisions getCommits(final int pageSize, final int offset) { return doLocked(() -> { GoConfigRevisions goConfigRevisions = new GoConfigRevisions(); try { LogCommand command = git.log().setMaxCount(pageSize).setSkip(offset); Iterable<RevCommit> revisions = command.call(); for (RevCommit revision : revisions) { GoConfigRevision goConfigRevision = new GoConfigRevision((byte[]) null, revision.getFullMessage()); goConfigRevision.setCommitSHA(revision.name()); goConfigRevisions.add(goConfigRevision); } } catch (Exception e) { // ignore } return goConfigRevisions; }); }
public static boolean hasGitHead(Git git) throws GitAPIException, IOException { boolean hasHead = true; try { git.log().all().call(); hasHead = git.getRepository().getAllRefs().containsKey("HEAD"); } catch (NoHeadException e) { hasHead = false; } return hasHead; }
public static void main(String[] args) throws IOException, GitAPIException { final File path; try (Repository repository = CookbookHelper.createNewRepository()) { try (Git git = new Git(repository)) { path = repository.getWorkTree(); System.out.println("Repository at " + path); git.add().addFilepattern("file1.txt").call(); RevCommit rev1 = git.commit().setAuthor("test", "test@test.com").setMessage("Commit Log 1").call(); System.out.println("Rev1: " + rev1); Iterable<RevCommit> gitLog = git.log().call(); for (RevCommit logMessage : gitLog) { System.out.println("Before revert: " + logMessage.getName() + " - " + logMessage.getFullMessage()); gitLog = git.log().call(); for (RevCommit logMessage : gitLog) { System.out.println("After revert: " + logMessage.getName() + " - " + logMessage.getFullMessage());
private List<RevCommit> calculatePickList(RevCommit headCommit) throws GitAPIException, NoHeadException, IOException { Iterable<RevCommit> commitsToUse; try (Git git = new Git(repo)) { LogCommand cmd = git.log().addRange(upstreamCommit, headCommit); commitsToUse = cmd.call(); if (preserveMerges || commit.getParentCount() == 1) cherryPickList.add(commit); RevCommit base; while ((base = walk.next()) != null) RebaseState.createFile(rewrittenDir, base.getName(), upstreamCommit.getName());
if (fork.getRepository().getRef("refs/heads/" + gitPatchRepository.getMainBranchName()) == null) { String startPoint = "patch-management^{commit}"; if (fork.getRepository().getRef("refs/remotes/origin/" + gitPatchRepository.getMainBranchName()) != null) { startPoint = "refs/remotes/origin/" + gitPatchRepository.getMainBranchName(); ObjectId since = fork.getRepository().resolve("patch-management^{commit}"); ObjectId to = fork.getRepository().resolve(gitPatchRepository.getMainBranchName() + "^{commit}"); Iterable<RevCommit> mainChanges = fork.log().addRange(since, to).call(); List<RevCommit> userChanges = new LinkedList<>(); for (RevCommit rc : mainChanges) { String prefix = String.format("%0" + prefixSize + "d-%s", count++, userChange.getName()); CherryPickResult result = fork.cherryPick() .include(userChange) gitPatchRepository.prepareCommit(fork, userChange.getFullMessage()).call();
private FileData createFileData(org.eclipse.jgit.lib.Repository repository, TreeWalk dirWalk, String baseFolder, ObjectId start) throws GitAPIException, IOException { String fullPath = baseFolder + dirWalk.getPathString(); Iterator<RevCommit> iterator = git.log() .add(start) .addPath(fullPath) .call() .iterator(); if (!iterator.hasNext()) { throw new IllegalStateException("Can't find revision for a file " + dirWalk.getPathString()); } return createFileData(repository, dirWalk, baseFolder, iterator.next()); }
private static void checkoutExistingBranchOrCreateOrphan(Git git, boolean isLocalOnly, String username, String password, String branch) throws IOException, GitAPIException { if (!isLocalOnly) { fetch(git, username, password); } // need to checkout branch ? if (!branch.equals(git.getRepository().getBranch())) { // HEAD is needed for creating a branch try { git.log().setMaxCount(1).call(); } catch (NoHeadException e) { // no HEAD - commit for creating a HEAD git.add().addFilepattern(".").call(); git.commit().setCommitter("a4c-bot", "a4c-bot@robot.org").setMessage("initial commit").call(); } // checkout branch CheckoutCommand checkoutCommand = git.checkout(); checkoutCommand.setName(branch); if (!branchExistsLocally(git, branch)) { checkoutCommand.setCreateBranch(true); String fullReference = getFullBranchReference(git, branch); boolean existRemotely = !fullReference.isEmpty(); if (existRemotely) { checkoutCommand.setStartPoint(getFullBranchReference(git, branch)); } else { checkoutCommand.setOrphan(true); } } checkoutCommand.call(); } }
@Override public Map<String, RevTag> findTagsBetween(Git git, RevCommit c1, RevCommit c2) throws GitAPIException, IOException { Map<ObjectId, List<RevTag>> reverseReferences = new HashMap<>(); RevWalk walk = new RevWalk(git.getRepository()); for (Ref t : git.tagList().call()) { Ref peeled = git.getRepository().peel(t); if (peeled != null && peeled.getPeeledObjectId() != null) { if (!reverseReferences.containsKey(peeled.getPeeledObjectId())) { reverseReferences.put(peeled.getPeeledObjectId(), new LinkedList<RevTag>()); } reverseReferences.get(peeled.getPeeledObjectId()).add(walk.parseTag(t.getObjectId())); } } Map<String, RevTag> result = new HashMap<>(); Iterable<RevCommit> commits = git.log().addRange(c1, c2).call(); for (RevCommit commit : commits) { if (reverseReferences.containsKey(commit.getId())) { for (RevTag tag : reverseReferences.get(commit.getId())) { result.put(tag.getTagName(), tag); } } } return result; }
@Override public boolean containsCommit(Git git, String branch, String commitMessage) throws IOException, GitAPIException { ObjectId head = git.getRepository().resolve(branch); Iterable<RevCommit> log = git.log().add(head).call(); for (RevCommit rc : log) { if (rc.getFullMessage().equals(commitMessage)) { return true; } } return false; }
this.git = Git.open(repoDirFile); String uri = this.git.getRepository().getConfig().getString("remote", REMOTE_NAME, "url"); this.git = Git.cloneRepository() .setDirectory(repoDirFile) .setURI(this.repoUri) } catch (FileNotFoundException e) { Iterable<RevCommit> logs = git.log().call(); RevCommit lastLog = null; this.lastProcessedGitHash = lastLog.getName();
git.commit().setMessage("Add alphabets.txt").call(); git.checkout().setCreateBranch(true).setName("fork").call(); addToGitIndex("alphabets.txt", // Add the missing 'x'. "a\nb\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz\n"); final RevCommit commit1 = git.commit().setMessage("Add missing 'x'").call(); final MergeResult mergeResult = git.merge() .include(commit1.getId()) .setFastForward(FastForwardMode.NO_FF) .setMessage("Merge 'fork'").call(); final RevCommit lastCommit = git.log().all().call().iterator().next(); assertThat(lastCommit.getParentCount()).isEqualTo(2); assertThat(lastCommit.getParents()).containsExactlyInAnyOrder(commit1, commit2);
public Collection<LogInfo> getLog() throws IOException { Collection<LogInfo> msgs = new ArrayList<LogInfo>(); try { // TODO find a way to limit the number of log entries - e.g. ..log().addRange(...).call() for (RevCommit c : new Git(this.openRepository()).log().call()) { msgs.add(new LogInfo(c.getName(), c.getAuthorIdent().getName(), c.getCommitterIdent().getName(), new Date(c.getCommitTime()), c.getFullMessage())); } } catch (NoHeadException e) { throw new IOException("not able to retrieve git log", e); } catch (GitAPIException e) { throw new IOException("problem executing log command", e); } return msgs; }
public GitTool() { File gitWorkDir = new File("."); try { Git git = Git.open(gitWorkDir); Iterable<RevCommit> commits = git.log().all().call(); Repository repo = git.getRepository(); branch = repo.getBranch(); RevCommit latestCommit = commits.iterator().next(); name = latestCommit.getName(); message = latestCommit.getFullMessage(); } catch (Throwable e) { name = ""; message = ""; branch = ""; } }
/** * Return a simplified git commit history list. * * @param repositoryDirectory The directory in which the git repo exists. * @param from Start to query from the given history. * @param count The number of history entries to retrieve. * @return A list of simplified history entries. */ public static List<SimpleGitHistoryEntry> getHistory(Path repositoryDirectory, int from, int count) { Git repository = null; try { repository = Git.open(repositoryDirectory.toFile()); Iterable<RevCommit> commits = repository.log().setSkip(from).setMaxCount(count).call(); List<SimpleGitHistoryEntry> historyEntries = Lists.newArrayList(); for (RevCommit commit : commits) { historyEntries.add(new SimpleGitHistoryEntry(commit.getId().getName(), commit.getAuthorIdent().getName(), commit.getAuthorIdent().getEmailAddress(), commit.getFullMessage(), new Date(commit.getCommitTime() * 1000L))); } return historyEntries; } catch (NoHeadException e) { log.debug("Your repository has no head, you need to save your topology before using the git history."); return Lists.newArrayList(); } catch (GitAPIException | IOException e) { throw new GitException("Unable to get history from the git repository", e); } finally { close(repository); } }
public List<RevCommit> getCommitsInBranchOnly(Git git, String baseBranch, String otherBranch) throws IOException, GitAPIException { Repository repository = git.getRepository(); Iterable<RevCommit> rawCommitsOnlyInPRbranch = git.log() .not(repository.resolve(baseBranch)) .add(repository.resolve(otherBranch)) .call(); return StreamSupport.stream(rawCommitsOnlyInPRbranch.spliterator(), false).collect(toList()); }
@SuppressWarnings("unused") public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { Iterable<RevCommit> logs = git.log() .call(); int count = 0; for (RevCommit rev : logs) { logs = git.log() .add(repository.resolve("remotes/origin/testbranch")) .call(); count = 0; for (RevCommit rev : logs) { .not(repository.resolve("master")) .add(repository.resolve("remotes/origin/testbranch")) .call(); count = 0; for (RevCommit rev : logs) { .all() .call(); count = 0; for (RevCommit rev : logs) { .addPath("README.md") .call(); count = 0; for (RevCommit rev : logs) {