private static boolean isMergeCommit(RevCommit currentCommit) { return currentCommit.getParentCount() > 1; }
private GoConfigRevision getGoConfigRevision(final RevCommit revision) { return new GoConfigRevision(contentFromTree(revision.getTree()), revision.getFullMessage()); }
RevCommit getRevCommitForCommitSHA(String commitSHA) throws GitAPIException { for (RevCommit revision : revisions()) { if (revision.getName().equals(commitSHA)) { return revision; } } throw new IllegalArgumentException(String.format("There is no commit corresponding to SHA: '%s'", commitSHA)); }
public static ArrayMemory valueOf(RevCommit value) { ArrayMemory memory = valueOf((RevObject) value); memory.refOfIndex("commitTime").assign(value.getCommitTime()); memory.refOfIndex("encoding").assign(value.getEncodingName()); memory.refOfIndex("shortMessage").assign(value.getShortMessage()); memory.refOfIndex("fullMessage").assign(value.getFullMessage()); ArrayMemory parents = new ArrayMemory(); for (RevCommit revCommit : value.getParents()) { parents.add(valueOf((RevObject)revCommit)); } memory.refOfIndex("parents").assign(parents); PersonIdent authorIdent = value.getAuthorIdent(); memory.refOfIndex("author").assign(authorIdent == null ? Memory.NULL : valueOf(authorIdent)); PersonIdent committerIdent = value.getCommitterIdent(); memory.refOfIndex("committer").assign(committerIdent == null ? Memory.NULL : valueOf(committerIdent)); return memory; }
/** * @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 ); } }
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; }
@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); } } }
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); }
private void parseReachable(ObjectId id) { try { RevCommit o = walk.parseCommit(id); if (!o.has(REACHABLE)) { o.add(REACHABLE); reachableCommits.add(o); } } catch (IOException readError) { // If we cannot read the value of the ref skip it. } }
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; }
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 RevCommit getRevCommitForMd5(String md5) throws GitAPIException { Assert.notNull(md5, "md5 is required"); final String expectedPart = GoConfigRevision.Fragment.md5.represent(GoConfigRevision.esc(md5)); for (RevCommit revision : revisions()) { String message = revision.getFullMessage(); if (message.endsWith(expectedPart)) { return revision; } } throw new IllegalArgumentException(String.format("There is no config version corresponding to md5: '%s'", md5)); }
private static List<RevCommit> filterValidCommits(List<RevCommit> revCommits) { final List<RevCommit> filteredCommits = new LinkedList<>(); for (RevCommit commit : revCommits) { final String commitAuthor = commit.getAuthorIdent().getName(); if (!USERS_EXCLUDED_FROM_VALIDATION.contains(commitAuthor)) { filteredCommits.add(commit); } } return filteredCommits; }
BlockRevQueue(Generator s) throws MissingObjectException, IncorrectObjectTypeException, IOException { free = new BlockFreeList(); outputType = s.outputType(); s.shareFreeList(this); for (;;) { final RevCommit c = s.next(); if (c == null) break; add(c); } }
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; } }
this.lastProcessedGitHash = lastLog.getName();
private static List<RevCommit> getCommitsByLastCommitAuthor( Iterator<RevCommit> previousCommitsIterator) { final List<RevCommit> commits = new LinkedList<>(); if (previousCommitsIterator.hasNext()) { final RevCommit lastCommit = previousCommitsIterator.next(); final String lastCommitAuthor = lastCommit.getAuthorIdent().getName(); commits.add(lastCommit); boolean wasLastCheckedCommitAuthorSameAsLastCommit = true; while (wasLastCheckedCommitAuthorSameAsLastCommit && previousCommitsIterator.hasNext()) { final RevCommit currentCommit = previousCommitsIterator.next(); final String currentCommitAuthor = currentCommit.getAuthorIdent().getName(); if (currentCommitAuthor.equals(lastCommitAuthor)) { commits.add(currentCommit); } else { wasLastCheckedCommitAuthorSameAsLastCommit = false; } } } return commits; }
@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())); }