List of usage examples for org.eclipse.jgit.blame BlameResult getSourceCommit
public RevCommit getSourceCommit(int idx)
From source file:br.com.metricminer2.scm.GitRepository.java
License:Apache License
@Override public String blame(String file, String currentCommit, Integer line) { Git git = null;/*from ww w .j ava 2 s . c o m*/ try { git = Git.open(new File(path)); Iterable<RevCommit> commits = git.log().add(git.getRepository().resolve(currentCommit)).call(); ObjectId prior = commits.iterator().next().getParent(0).getId(); BlameResult blameResult = git.blame().setFilePath(file).setStartCommit(prior).setFollowFileRenames(true) .call(); return blameResult.getSourceCommit(line).getId().getName(); } catch (Exception e) { throw new RuntimeException(e); } finally { if (git != null) git.close(); } }
From source file:com.buildautomation.jgit.api.ShowBlame.java
License:Apache License
public static void showBlame() throws IOException, GitAPIException { // prepare a new test-repository try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { BlameCommand blamer = new BlameCommand(repository); ObjectId commitID = repository.resolve("HEAD"); blamer.setStartCommit(commitID); blamer.setFilePath("README.md"); BlameResult blame = blamer.call(); // read the number of lines from the commit to not look at changes in the working copy int lines = countFiles(repository, commitID, "README.md"); for (int i = 0; i < lines; i++) { RevCommit commit = blame.getSourceCommit(i); System.out.println("Line: " + i + ": " + commit); }//from w w w. j a v a 2s . com System.out.println("Displayed commits responsible for " + lines + " lines of README.md"); } }
From source file:com.gitblit.utils.DiffUtils.java
License:Apache License
/** * Returns the list of lines in the specified source file annotated with the * source commit metadata.//from www .j a v a 2 s . com * * @param repository * @param blobPath * @param objectId * @return list of annotated lines */ public static List<AnnotatedLine> blame(Repository repository, String blobPath, String objectId) { List<AnnotatedLine> lines = new ArrayList<AnnotatedLine>(); try { ObjectId object; if (StringUtils.isEmpty(objectId)) { object = JGitUtils.getDefaultBranch(repository); } else { object = repository.resolve(objectId); } BlameCommand blameCommand = new BlameCommand(repository); blameCommand.setFilePath(blobPath); blameCommand.setStartCommit(object); BlameResult blameResult = blameCommand.call(); RawText rawText = blameResult.getResultContents(); int length = rawText.size(); for (int i = 0; i < length; i++) { RevCommit commit = blameResult.getSourceCommit(i); AnnotatedLine line = new AnnotatedLine(commit, i + 1, rawText.getString(i)); lines.add(line); } } catch (Throwable t) { LOGGER.error(MessageFormat.format("failed to generate blame for {0} {1}!", blobPath, objectId), t); } return lines; }
From source file:com.googlesource.gerrit.plugins.reviewersbyblame.ReviewersByBlame.java
License:Apache License
/** * Get a map of all the possible reviewers based on the provided blame data * * @param edits List of edits that were made for this patch * @param blameResult Result of blame computation * @return a set of all possible reviewers, empty if none, never * <code>null</code> *//*from www. ja v a2 s . c om*/ private Map<Account, Integer> getReviewersForPatch(final List<Edit> edits, final BlameResult blameResult) { Map<Account, Integer> reviewers = Maps.newHashMap(); for (Edit edit : edits) { for (int i = edit.getBeginA(); i < edit.getEndA(); i++) { RevCommit commit = blameResult.getSourceCommit(i); Set<Account.Id> ids = byEmailCache.get(commit.getAuthorIdent().getEmailAddress()); for (Account.Id id : ids) { Account account = accountCache.get(id).getAccount(); if (account.isActive() && !change.getOwner().equals(account.getId())) { Integer count = reviewers.get(account); reviewers.put(account, count == null ? 1 : count.intValue() + 1); } } } } return reviewers; }
From source file:com.googlesource.gerrit.plugins.smartreviewers.SmartReviewers.java
License:Apache License
/** * Fill a map of all the possible reviewers based on the provided blame data * * @param edits List of edits that were made for this patch * @param blameResult Result of blame computation * @param reviewers the reviewer hash table to fill *///ww w .j a v a 2s . c om private void getReviewersFromBlame(final List<Edit> edits, final BlameResult blameResult, Map<Account, Integer> reviewers) { for (Edit edit : edits) { for (int i = edit.getBeginA(); i < edit.getEndA(); i++) { RevCommit commit = blameResult.getSourceCommit(i); Set<Account.Id> ids = byEmailCache.get(commit.getAuthorIdent().getEmailAddress()); for (Account.Id id : ids) { Account account = accountCache.get(id).getAccount(); addAccount(account, reviewers, weightBlame); } } } }
From source file:com.searchcode.app.jobs.IndexGitRepoJob.java
private List<CodeOwner> getBlameInfo(int codeLinesSize, String repoName, String repoLocations, String fileName) {// w w w. j a va 2 s. c o m List<CodeOwner> codeOwners = new ArrayList<>(codeLinesSize); try { // The / part is required due to centos bug for version 1.1.1 // This appears to be correct String repoLoc = repoLocations + "/" + repoName + "/.git"; Repository localRepository = new FileRepository(new File(repoLoc)); BlameCommand blamer = new BlameCommand(localRepository); ObjectId commitID = localRepository.resolve("HEAD"); if (commitID == null) { Singleton.getLogger().info("getBlameInfo commitID is null for " + repoLoc + " " + fileName); return codeOwners; } BlameResult blame; // Somewhere in here appears to be wrong... blamer.setStartCommit(commitID); blamer.setFilePath(fileName); blame = blamer.call(); // Hail mary attempt to solve issue on CentOS Attempt to set at all costs if (blame == null) { // This one appears to solve the issue so don't remove it String[] split = fileName.split("/"); blamer.setStartCommit(commitID); if (split.length != 1) { blamer.setFilePath(String.join("/", Arrays.asList(split).subList(1, split.length))); } blame = blamer.call(); } if (blame == null) { String[] split = fileName.split("/"); blamer.setStartCommit(commitID); if (split.length != 1) { blamer.setFilePath("/" + String.join("/", Arrays.asList(split).subList(1, split.length))); } blame = blamer.call(); } if (blame == null) { Singleton.getLogger().info("getBlameInfo blame is null for " + repoLoc + " " + fileName); } if (blame != null) { // Get all the owners their number of commits and most recent commit HashMap<String, CodeOwner> owners = new HashMap<>(); RevCommit commit; PersonIdent authorIdent; try { for (int i = 0; i < codeLinesSize; i++) { commit = blame.getSourceCommit(i); authorIdent = commit.getAuthorIdent(); if (owners.containsKey(authorIdent.getName())) { CodeOwner codeOwner = owners.get(authorIdent.getName()); codeOwner.incrementLines(); int timestamp = codeOwner.getMostRecentUnixCommitTimestamp(); if (commit.getCommitTime() > timestamp) { codeOwner.setMostRecentUnixCommitTimestamp(commit.getCommitTime()); } owners.put(authorIdent.getName(), codeOwner); } else { owners.put(authorIdent.getName(), new CodeOwner(authorIdent.getName(), 1, commit.getCommitTime())); } } } catch (IndexOutOfBoundsException ex) { // Ignore this as its not really a problem or is it? Singleton.getLogger() .info("IndexOutOfBoundsException when trying to get blame for " + repoName + fileName); } codeOwners = new ArrayList<>(owners.values()); } } catch (IOException e) { e.printStackTrace(); } catch (GitAPIException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } System.gc(); // Try to clean up return codeOwners; }
From source file:com.searchcode.app.jobs.repository.IndexGitRepoJob.java
License:Open Source License
/** * Uses the inbuilt git//from www. ja v a2 s .c o m * TODO this method appears to leak memory like crazy... need to investigate * TODO lots of hairy bits in here need tests to capture issues */ public List<CodeOwner> getBlameInfo(int codeLinesSize, String repoName, String repoLocations, String fileName) { List<CodeOwner> codeOwners = new ArrayList<>(codeLinesSize); try { // The / part is required due to centos bug for version 1.1.1 // This appears to be correct String repoLoc = repoLocations + "/" + repoName + "/.git"; Repository localRepository = new FileRepository(new File(repoLoc)); BlameCommand blamer = new BlameCommand(localRepository); ObjectId commitID = localRepository.resolve("HEAD"); if (commitID == null) { Singleton.getLogger().info("getBlameInfo commitID is null for " + repoLoc + " " + fileName); return codeOwners; } BlameResult blame; // Somewhere in here appears to be wrong... blamer.setStartCommit(commitID); blamer.setFilePath(fileName); blame = blamer.call(); // Hail mary attempt to solve issue on CentOS Attempt to set at all costs if (blame == null) { // This one appears to solve the issue so don't remove it String[] split = fileName.split("/"); blamer.setStartCommit(commitID); if (split.length != 1) { blamer.setFilePath(String.join("/", Arrays.asList(split).subList(1, split.length))); } blame = blamer.call(); } if (blame == null) { String[] split = fileName.split("/"); blamer.setStartCommit(commitID); if (split.length != 1) { blamer.setFilePath("/" + String.join("/", Arrays.asList(split).subList(1, split.length))); } blame = blamer.call(); } if (blame == null) { Singleton.getLogger().info("getBlameInfo blame is null for " + repoLoc + " " + fileName); } if (blame != null) { // Get all the owners their number of commits and most recent commit HashMap<String, CodeOwner> owners = new HashMap<>(); RevCommit commit; PersonIdent authorIdent; try { for (int i = 0; i < codeLinesSize; i++) { commit = blame.getSourceCommit(i); authorIdent = commit.getAuthorIdent(); if (owners.containsKey(authorIdent.getName())) { CodeOwner codeOwner = owners.get(authorIdent.getName()); codeOwner.incrementLines(); int timestamp = codeOwner.getMostRecentUnixCommitTimestamp(); if (commit.getCommitTime() > timestamp) { codeOwner.setMostRecentUnixCommitTimestamp(commit.getCommitTime()); } owners.put(authorIdent.getName(), codeOwner); } else { owners.put(authorIdent.getName(), new CodeOwner(authorIdent.getName(), 1, commit.getCommitTime())); } } } catch (IndexOutOfBoundsException ex) { // Ignore this as its not really a problem or is it? Singleton.getLogger().info( "IndexOutOfBoundsException when trying to get blame for " + repoName + " " + fileName); } codeOwners = new ArrayList<>(owners.values()); } } catch (IOException ex) { Singleton.getLogger().info("IOException getBlameInfo when trying to get blame for " + repoName + " " + fileName + " " + ex.toString()); } catch (GitAPIException ex) { Singleton.getLogger().info("GitAPIException getBlameInfo when trying to get blame for " + repoName + " " + fileName + " " + ex.toString()); } catch (IllegalArgumentException ex) { Singleton.getLogger().info("IllegalArgumentException getBlameInfo when trying to get blame for " + repoName + " " + fileName + " " + ex.toString()); } System.gc(); // Try to clean up return codeOwners; }
From source file:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java
License:Open Source License
public static Blame getBlame(Repository r, String revision, String path) throws IOException, GitAPIException { if (path.startsWith("/")) { path = path.substring(1);/*from w w w . j a va 2s .c o m*/ } Git git = new Git(r); ObjectId revId = r.resolve(revision); BlameCommand bc = git.blame(); bc.setStartCommit(revId); bc.setFilePath(path); BlameResult br = bc.call(); Blame blame = new Blame(); blame.path = path; blame.revision = revision; blame.commits = new ArrayList<Commit>(); blame.lines = new ArrayList<Blame.Line>(); Map<String, Integer> sha2idx = new HashMap<String, Integer>(); RawText resultContents = br.getResultContents(); for (int i = 0; i < br.getResultContents().size(); i++) { RevCommit sourceCommit = br.getSourceCommit(i); // XXX should it really be the source commit String sha = sourceCommit.getName(); Integer cIdx = sha2idx.get(sha); if (cIdx == null) { cIdx = blame.commits.size(); Commit commit = GitDomain.createCommit(sourceCommit); blame.commits.add(commit); sha2idx.put(sha, cIdx); } Blame.Line bl = new Blame.Line(); bl.commit = cIdx; bl.text = resultContents.getString(i); blame.lines.add(bl); } return blame; }
From source file:edu.nju.cs.inform.jgit.porcelain.ShowBlame.java
License:Apache License
public static void main(String[] args) throws IOException, GitAPIException { // prepare a new test-repository try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { BlameCommand blamer = new BlameCommand(repository); ObjectId commitID = repository.resolve("HEAD"); blamer.setStartCommit(commitID); blamer.setFilePath("README.md"); BlameResult blame = blamer.call(); // read the number of lines from the commit to not look at changes in the working copy int lines = countFiles(repository, commitID, "README.md"); for (int i = 0; i < lines; i++) { RevCommit commit = blame.getSourceCommit(i); System.out.println("Line: " + i + ": " + commit); }//from w w w. j a va 2 s . c o m System.out.println("Displayed commits responsible for " + lines + " lines of README.md"); } }
From source file:org.apache.maven.scm.provider.git.jgit.command.blame.JGitBlameCommand.java
License:Apache License
@Override public BlameScmResult executeBlameCommand(ScmProviderRepository repo, ScmFileSet workingDirectory, String filename) throws ScmException { Git git = null;/*from w w w . j a va 2s .com*/ File basedir = workingDirectory.getBasedir(); try { git = Git.open(basedir); BlameResult blameResult = git.blame().setFilePath(filename).call(); List<BlameLine> lines = new ArrayList<BlameLine>(); int i = 0; while ((i = blameResult.computeNext()) != -1) { lines.add(new BlameLine(blameResult.getSourceAuthor(i).getWhen(), blameResult.getSourceCommit(i).getName(), blameResult.getSourceAuthor(i).getName(), blameResult.getSourceCommitter(i).getName())); } return new BlameScmResult("JGit blame", lines); } catch (Exception e) { throw new ScmException("JGit blame failure!", e); } finally { JGitUtils.closeRepo(git); } }