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 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); } } }
return git.getRepository().findRef("HEAD").getObjectId().getName();
private void insertChangeId(ObjectId treeId) { ObjectId firstParentId = null; if (!parents.isEmpty()) firstParentId = parents.get(0); ObjectId changeId = ChangeIdUtil.computeChangeId(treeId, firstParentId, author, committer, message); message = ChangeIdUtil.insertId(message, changeId); if (changeId != null) message = message.replaceAll("\nChange-Id: I" //$NON-NLS-1$ + ObjectId.zeroId().getName() + "\n", "\nChange-Id: I" //$NON-NLS-1$ //$NON-NLS-2$ + changeId.getName() + "\n"); //$NON-NLS-1$ }
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { List<String> paths = readElementsAt(repository, "6409ee1597a53c6fbee31edf9cde31dc3afbe20f", "src/main/java/org/dstadler/jgit/porcelain"); System.out.println("Had paths for commit: " + paths); final ObjectId testbranch = repository.resolve("testbranch"); paths = readElementsAt(repository, testbranch.getName(), "src/main/java/org/dstadler/jgit/porcelain"); System.out.println("Had paths for tag: " + paths); } }
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { List<String> paths = readElementsAt(repository, "6409ee1597a53c6fbee31edf9cde31dc3afbe20f", "src/main/java/org/dstadler/jgit/porcelain"); System.out.println("Had paths for commit: " + paths); final ObjectId testbranch = repository.resolve("testbranch"); paths = readElementsAt(repository, testbranch.getName(), "src/main/java/org/dstadler/jgit/porcelain"); System.out.println("Had paths for tag: " + paths); } }
latestGitHash = head.getName();
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // the Ref holds an ObjectId for any type of object (tree, commit, blob, tree) Ref head = repository.exactRef("refs/heads/master"); System.out.println("Ref of refs/heads/master: " + head + ": " + head.getName() + " - " + head.getObjectId().getName()); } } }
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // the Ref holds an ObjectId for any type of object (tree, commit, blob, tree) Ref head = repository.exactRef("refs/heads/master"); System.out.println("Ref of refs/heads/master: " + head + ": " + head.getName() + " - " + head.getObjectId().getName()); } } }
private String getShortBranchName(Ref headRef) { if (headRef.isSymbolic()) { return Repository.shortenRefName(headRef.getTarget().getName()); } // Detached HEAD. Every non-symbolic ref in the ref database has an // object id, so this cannot be null. ObjectId id = headRef.getObjectId(); if (id == null) { throw new NullPointerException(); } return id.getName(); }
private static String getHeadName(Ref head) { String headName; if (head.isSymbolic()) { headName = head.getTarget().getName(); } else { ObjectId headId = head.getObjectId(); // the callers are checking this already assert headId != null; headName = headId.getName(); } return headName; }
private void writeRewrittenHashes() throws RevisionSyntaxException, IOException, RefNotFoundException { File currentCommitFile = rebaseState.getFile(CURRENT_COMMIT); if (!currentCommitFile.exists()) return; ObjectId headId = getHead().getObjectId(); // getHead() checks for null assert headId != null; String head = headId.getName(); String currentCommits = rebaseState.readFile(CURRENT_COMMIT); for (String current : currentCommits.split("\n")) //$NON-NLS-1$ RebaseState .createFile(rebaseState.getRewrittenDir(), current, head); FileUtils.delete(currentCommitFile); }
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); } } } }
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); } } } }
@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()); } }
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(); } } } }
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(); } } } }