Refine search
private GoConfigRevision getGoConfigRevision(final RevCommit revision) { return new GoConfigRevision(contentFromTree(revision.getTree()), revision.getFullMessage()); }
/** * @param gitConfigFolder e.g. /your/project/root/.git * * @return Returns last commit's UUID, "nocommit" if there are no commits and returns null if an exception occured */ public static String getLastCommitUuid( String gitConfigFolder ) throws MojoExecutionException { try { Repository repo = new RepositoryBuilder().setGitDir( new File( gitConfigFolder ) ).readEnvironment().findGitDir() .build(); RevWalk walk = new RevWalk( repo ); ObjectId head = repo.resolve( "HEAD" ); if ( head != null ) { RevCommit lastCommit = walk.parseCommit( head ); return lastCommit.getId().getName(); } else { return "nocommit"; } } catch ( Exception e ) { throw new MojoExecutionException( "Error trying to get the last git commit uuid", e ); } }
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) this.lastProcessedGitHash = lastLog.getName();
String getMergedConfig(String branchName, RevCommit newCommit) throws GitAPIException, IOException { MergeResult result = null; try { checkout(branchName); result = git.merge().include(newCommit).call(); } catch (GitAPIException e) { LOGGER.info("[CONFIG_MERGE] Merging commit {} by user {} to branch {} at revision {} failed", newCommit.getId().getName(), newCommit.getAuthorIdent().getName(), branchName, getCurrentRevCommit().getId().getName()); throw e; } if (!result.getMergeStatus().isSuccessful()) { LOGGER.info("[CONFIG_MERGE] Merging commit {} by user {} to branch {} at revision {} failed as config file has changed", newCommit.getId().getName(), newCommit.getAuthorIdent().getName(), branchName, getCurrentRevCommit().getId().getName()); throw new ConfigFileHasChangedException(); } LOGGER.info("[CONFIG_MERGE] Successfully merged commit {} by user {} to branch {}. Merge commit revision is {}", newCommit.getId().getName(), newCommit.getAuthorIdent().getName(), branchName, getCurrentRevCommit().getId().getName()); return FileUtils.readFileToString(new File(workingDir, CRUISE_CONFIG_XML), UTF_8); }
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; }); }
private static int countLinesOfFileInCommit(Repository repository, ObjectId commitID, String name) throws IOException { try (RevWalk revWalk = new RevWalk(repository)) { RevCommit commit = revWalk.parseCommit(commitID); RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(name)); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repository.open(objectId); // load the content of the file into a stream ByteArrayOutputStream stream = new ByteArrayOutputStream(); loader.copyTo(stream); revWalk.dispose(); return IOUtils.readLines(new ByteArrayInputStream(stream.toByteArray()), "UTF-8").size(); } } } }
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { Ref head = repository.findRef("HEAD"); // a RevWalk allows to walk over commits based on some filtering that is defined try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(head.getObjectId()); RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); // now use a TreeWalk to iterate over all files in the Tree // you can set Filters to narrow down the results if needed try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree); // not walk the tree recursively so we only get the elements in the top-level directory treeWalk.setRecursive(false); while (treeWalk.next()) { System.out.println("found: " + treeWalk.getPathString()); } } } } } }
Collections.sort(only); Ref head = repo.exactRef(Constants.HEAD); if (head == null) throw new NoHeadException( RevCommit previousCommit = rw.parseCommit(headId); for (RevCommit p : previousCommit.getParents()) parents.add(p.getId()); if (author == null) author = previousCommit.getAuthorIdent(); } else { parents.add(0, headId); RevCommit headCommit = rw.parseCommit(headId); headCommit.getTree(); if (indexTreeId.equals(headCommit.getTree())) { throw new EmptyCommitException( JGitText.get().emptyCommit); : parents.size() == 0 ? "commit (initial): " //$NON-NLS-1$ : "commit: "; //$NON-NLS-1$ ru.setRefLogMessage(prefix + revCommit.getShortMessage(), false);
String cherryPickName = srcCommit.getId().abbreviate(7).name() + " " + srcCommit.getShortMessage(); //$NON-NLS-1$ merger.setBase(srcParent.getTree()); merger.setCommitNames(new String[] { "BASE", ourName, //$NON-NLS-1$ cherryPickName }); if (merger.merge(newHead, srcCommit)) { if (AnyObjectId.equals(newHead.getTree().getId(), merger .getResultTreeId())) continue; DirCacheCheckout dco = new DirCacheCheckout(repo, newHead.getTree(), repo.lockDirCache(), merger.getResultTreeId()); dco.setFailOnConflict(true); dco.checkout(); if (!noCommit) newHead = new Git(getRepository()).commit() .setMessage(srcCommit.getFullMessage()) .setReflogComment(reflogPrefix + " " //$NON-NLS-1$ + srcCommit.getShortMessage()) .setAuthor(srcCommit.getAuthorIdent()) .setNoVerify(true).call(); cherryPickedRefs.add(src); .formatWithConflicts(srcCommit.getFullMessage(), merger.getUnmergedPaths()); repo.writeCherryPickHead(srcCommit.getId());
if (srcCommit.getParentCount() != 1) throw new MultipleParentsNotAllowedException( MessageFormat.format( JGitText.get().canOnlyRevertCommitsWithOneParent, srcCommit.name(), Integer.valueOf(srcCommit.getParentCount()))); RevCommit srcParent = srcCommit.getParent(0); String revertName = srcCommit.getId().abbreviate(7).name() + " " + srcCommit.getShortMessage(); //$NON-NLS-1$ merger.setBase(srcCommit.getTree()); merger.setCommitNames(new String[] { "BASE", ourName, revertName }); //$NON-NLS-1$ String shortMessage = "Revert \"" + srcCommit.getShortMessage() //$NON-NLS-1$ + "\""; //$NON-NLS-1$ String newMessage = shortMessage + "\n\n" //$NON-NLS-1$ + "This reverts commit " + srcCommit.getId().getName() //$NON-NLS-1$ + ".\n"; //$NON-NLS-1$ if (merger.merge(headCommit, srcParent)) { if (AnyObjectId.equals(headCommit.getTree().getId(), merger .getResultTreeId())) continue; DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.getTree(), repo.lockDirCache(), merger.getResultTreeId()); dco.setFailOnConflict(true);
private Git.Head getHead(final Repository repository) throws IOException { ObjectId revision = repository.resolve(Constants.HEAD); RevCommit commit = new RevWalk(repository).parseCommit(revision); Git.Head head = new Git.Head( revision.getName(), commit.getAuthorIdent().getName(), commit.getAuthorIdent().getEmailAddress(), commit.getCommitterIdent().getName(), commit.getCommitterIdent().getEmailAddress(), commit.getFullMessage() ); return head; }
try (Git git = new Git(repo)) { CreateBranchCommand command = git.branchCreate(); command.setName(name); if (startCommit != null) Ref headRef = repo.exactRef(Constants.HEAD); if (headRef == null) { if (orphan) { if (startPoint == null && startCommit == null) { Result r = repo.updateRef(Constants.HEAD).link( getBranchName()); if (!EnumSet.of(Result.NEW, Result.FORCED).contains(r)) r.name())); this.status = CheckoutResult.NOT_TRIED_RESULT; return repo.exactRef(Constants.HEAD); RevTree headTree = headCommit == null ? null : headCommit.getTree(); dco = new DirCacheCheckout(repo, headTree, dc, newCommit.getTree()); dco.setFailOnConflict(!force); dco.setProgressMonitor(monitor); if (ref != null && !ref.getName().startsWith(Constants.R_HEADS))
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // See e.g. GetRevCommitFromObjectId for how to use a SHA-1 directly Ref head = repository.findRef("HEAD"); System.out.println("Ref of HEAD: " + head + ": " + head.getName() + " - " + head.getObjectId().getName()); // a RevWalk allows to walk over commits based on some filtering that is defined try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(head.getObjectId()); System.out.println("Commit: " + commit); // a commit points to a tree RevTree tree = walk.parseTree(commit.getTree().getId()); System.out.println("Found Tree: " + tree); walk.dispose(); } } } }
/** * Get latest commit by ref name from local .git */ @Override public GitCommit commit(String refName) throws GitException { try (Git git = gitOpen()) { Repository repo = git.getRepository(); Ref head = repo.findRef(refName); if (head == null) { return null; } try (RevWalk walk = new RevWalk(repo)) { RevCommit commit = walk.parseCommit(head.getObjectId()); walk.dispose(); String id = commit.getId().getName(); String message = commit.getFullMessage(); String author = commit.getAuthorIdent().getName(); return new GitCommit(id, message, author); } } catch (IOException e) { throw new GitException("Fail to get commit message", e); } }
private static AbstractTreeIterator prepareTreeParser(Repository repository, String ref) throws IOException { // from the commit we can build the tree which allows us to construct the TreeParser Ref head = repository.exactRef(ref); try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(head.getObjectId()); RevTree tree = walk.parseTree(commit.getTree().getId()); CanonicalTreeParser treeParser = new CanonicalTreeParser(); try (ObjectReader reader = repository.newObjectReader()) { treeParser.reset(reader, tree.getId()); } walk.dispose(); return treeParser; } } }
private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId) throws IOException { // from the commit we can build the tree which allows us to construct the TreeParser //noinspection Duplicates try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(repository.resolve(objectId)); RevTree tree = walk.parseTree(commit.getTree().getId()); CanonicalTreeParser treeParser = new CanonicalTreeParser(); try (ObjectReader reader = repository.newObjectReader()) { treeParser.reset(reader, tree.getId()); } walk.dispose(); return treeParser; } }
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { Ref head = repository.findRef("refs/heads/master"); System.out.println("Found head: " + head); // a RevWalk allows to walk over commits based on some filtering that is defined try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(head.getObjectId()); System.out.println("\nCommit-Message: " + commit.getFullMessage()); walk.dispose(); } } } }
for (int i = 1; i < commitToPick.getParentCount(); i++) otherParentsUnchanged &= newParents.get(i).equals( commitToPick.getParent(i)); try (Git git = new Git(repo)) { if (otherParentsUnchanged) { boolean isMerge = commitToPick.getParentCount() > 1; String ourCommitName = getOurCommitName(); CherryPickCommand pickCommand = git.cherryPick() .include(commitToPick) .setOurCommitName(ourCommitName) CommitCommand commit = git.commit(); commit.setAuthor(commitToPick.getAuthorIdent()); commit.setReflogComment(REFLOG_PREFIX + " " //$NON-NLS-1$ + commitToPick.getShortMessage()); newHead = commit.call(); } else .setProgressMonitor(monitor) .setCommit(false); for (int i = 1; i < commitToPick.getParentCount(); i++) merge.include(newParents.get(i)); MergeResult mergeResult = merge.call(); commit.setAuthor(commitToPick.getAuthorIdent()); commit.setMessage(commitToPick.getFullMessage()); commit.setReflogComment(REFLOG_PREFIX + " " //$NON-NLS-1$ + commitToPick.getShortMessage());
private static RevTree getTree(Repository repository) throws IOException { ObjectId lastCommitId = repository.resolve(Constants.HEAD); // a RevWalk allows to walk over commits based on some filtering try (RevWalk revWalk = new RevWalk(repository)) { RevCommit commit = revWalk.parseCommit(lastCommitId); System.out.println("Time of commit (seconds since epoch): " + commit.getCommitTime()); // and using commit's tree find the path RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); return tree; } }
localPath = repository.getWorkTree(); try (Git git = new Git(repository)) { File file1 = new File(repository.getDirectory().getParent(), "testfile"); FileUtils.writeStringToFile(file1, "some text", "UTF-8"); File file2 = new File(repository.getDirectory().getParent(), "testfile2"); FileUtils.writeStringToFile(file2, "some text", "UTF-8"); git.add() .addFilepattern("testfile") .call(); git.add() .addFilepattern("testfile2") .call(); Collection<RevCommit> stashes = git.stashList().call(); for(RevCommit rev : stashes) { System.out.println("Found stash: " + rev + ": " + rev.getFullMessage()); System.out.println("StashDrop returned: " + call); ObjectId applied = git.stashApply().setStashRef(stash.getName()).call(); System.out.println("Applied 2nd stash as: " + applied);