String findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit) { if (laterCommit == null || earlierCommit == null) { return null; } String output = null; try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { DiffFormatter diffFormatter = new DiffFormatter(out); diffFormatter.setRepository(gitRepo); diffFormatter.format(earlierCommit.getId(), laterCommit.getId()); output = out.toString(); output = StringUtil.stripTillLastOccurrenceOf(output, "+++ b/cruise-config.xml"); } catch (IOException e) { throw new RuntimeException("Error occurred during diff computation. Message: " + e.getMessage()); } return output; }
void createBranch(String branchName, RevCommit revCommit) throws GitAPIException { try { git.branchCreate().setName(branchName).setStartPoint(revCommit).call(); } catch (GitAPIException e) { LOGGER.error("[CONFIG_MERGE] Failed to create branch {} at revision {}", branchName, revCommit.getId(), e); throw e; } }
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); }
/** * @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 ); } }
@Test public void shouldCreateBranchForARevCommit() throws Exception { configRepo.checkin(goConfigRevision("something", "md5-1")); RevCommit revCommit = configRepo.getCurrentRevCommit(); configRepo.createBranch("branch1", revCommit); Ref branch = getBranch("branch1"); assertThat(branch, is(notNullValue())); assertThat(branch.getObjectId(), is(revCommit.getId())); }
@Test public void testCommitMessageHasProperStructure() { for (RevCommit commit : filterValidCommits(lastCommits)) { final String commitMessage = commit.getFullMessage(); final int error = validateCommitMessage(commitMessage); if (error != 0) { final String commitId = commit.getId().getName(); fail(getInvalidCommitMessageFormattingError(commitId, commitMessage) + error); } } }
public List<RevCommit> findCommitsUntilSomeTag(Repository repo, RevCommit head, @Nonnull Map<ObjectId, List<String>> tagObjectIdToName) { try (RevWalk revWalk = new RevWalk(repo)) { revWalk.markStart(head); for (RevCommit commit : revWalk) { ObjectId objId = commit.getId(); if (tagObjectIdToName.size() > 0) { List<String> maybeList = tagObjectIdToName.get(objId); if (maybeList != null && maybeList.get(0) != null) { return Collections.singletonList(commit); } } } return Collections.emptyList(); } catch (Exception e) { throw new RuntimeException("Unable to find commits until some tag", e); } }
private ObjectId getStartPointObjectId() throws AmbiguousObjectException, RefNotFoundException, IOException { if (startCommit != null) return startCommit.getId(); String startPointOrHead = (startPoint != null) ? startPoint : Constants.HEAD; ObjectId result = repo.resolve(startPointOrHead); if (result == null) throw new RefNotFoundException(MessageFormat.format( JGitText.get().refNotResolved, startPointOrHead)); return result; }
private ObjectId getStartPointObjectId() throws AmbiguousObjectException, RefNotFoundException, IOException { if (startCommit != null) return startCommit.getId(); String startPointOrHead = getStartPointOrHead(); ObjectId result = repo.resolve(startPointOrHead); if (result == null) throw new RefNotFoundException(MessageFormat.format( JGitText.get().refNotResolved, startPointOrHead)); return result; }
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { String from = "3408efc41a51555d488d30d8a91ea560c5e13311"; String to = "7228de6ebe2a3087118562414061af4e189624c0"; // a RevWalk allows to walk over commits based on some filtering that is defined try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(repository.resolve(to)); System.out.println("Start-Commit: " + commit); System.out.println("Walking all commits starting at " + to + " until we find " + from); walk.markStart(commit); int count = 0; for (RevCommit rev : walk) { System.out.println("Commit: " + rev); count++; if(rev.getId().getName().equals(from)) { System.out.println("Found from, stopping walk"); break; } } System.out.println(count); walk.dispose(); } } } }
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { String from = "3408efc41a51555d488d30d8a91ea560c5e13311"; String to = "7228de6ebe2a3087118562414061af4e189624c0"; // a RevWalk allows to walk over commits based on some filtering that is defined try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(repository.resolve(to)); System.out.println("Start-Commit: " + commit); System.out.println("Walking all commits starting at " + to + " until we find " + from); walk.markStart(commit); int count = 0; for (RevCommit rev : walk) { System.out.println("Commit: " + rev); count++; if(rev.getId().getName().equals(from)) { System.out.println("Found from, stopping walk"); break; } } System.out.println(count); walk.dispose(); } } } }
inc.addAll(include.values()); for (RevCommit r : assume) exc.add(r.getId()); packWriter.setIndexDisabled(true); packWriter.setDeltaBaseAsOffset(true);
@Override public void exec(Plugin plugin) { log.trace("Compare commit id"); try { // first checkout tag branch JGitUtil.checkout(gitCachePath(plugin), plugin.getTag()); // compare commit id is equal tag's latest commit id RevCommit commit = JGitUtil.latestCommit(gitCachePath(plugin)); if (!Objects.equals(plugin.getLatestCommit(), commit.getId().getName())) { throw new PluginException("Tag's latest commit id is not user provided"); } } catch (GitException e) { throw new PluginException(e.getMessage()); } }
if (id.equals(rc.getId())) { rc.parents = RevCommit.NO_PARENTS; } else {
ObjectId evalCommitId = evalCommit.getId();
commit = revWalk.parseCommit(commit.getId()); revWalk.reset(); List<Ref> result = new ArrayList<>();
u.setNewObjectId(commit.getId()); u.forceUpdate();
/** * 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); } }
RevCommit upstream = walk.lookupCommit(upstreamCommit.getId());