public static void main(String args[]) throws IOException, GitAPIException { try (Repository repo = CookbookHelper.openJGitCookbookRepository()) { final String[] list = new File(".").list(); if(list == null) { throw new IllegalStateException("Did not find any files at " + new File(".").getAbsolutePath()); } for(String file : list) { if(new File(file).isDirectory()) { continue; } System.out.println("Blaming " + file); final BlameResult result = new Git(repo).blame().setFilePath(file) .setTextComparator(RawTextComparator.WS_IGNORE_ALL).call(); final RawText rawText = result.getResultContents(); for (int i = 0; i < rawText.size(); i++) { final PersonIdent sourceAuthor = result.getSourceAuthor(i); final RevCommit sourceCommit = result.getSourceCommit(i); System.out.println(sourceAuthor.getName() + (sourceCommit != null ? "/" + sourceCommit.getCommitTime() + "/" + sourceCommit.getName() : "") + ": " + rawText.getString(i)); } } } } }
/** * Execute the generator in a blocking fashion until all data is ready. * * @return the complete result. Null if no file exists for the given path. * @throws java.io.IOException * the repository cannot be read. */ public BlameResult computeBlameResult() throws IOException { try { BlameResult r = BlameResult.create(this); if (r != null) r.computeAll(); return r; } finally { close(); } }
/** * Construct a new BlameResult for a generator. * * @param gen * the generator the result will consume records from. * @return the new result object. null if the generator cannot find the path * it starts from. * @throws java.io.IOException * the repository cannot be read. */ public static BlameResult create(BlameGenerator gen) throws IOException { String path = gen.getResultPath(); RawText contents = gen.getResultContents(); if (contents == null) { gen.close(); return null; } return new BlameResult(gen, path, contents); }
return; if (start == 0 && end == resultContents.size()) { computeAll(); return; if (hasSourceData(start, end)) return; loadFrom(gen);
return; for (int i = 0; i < blameResult.getResultContents().size(); i++) { if (blameResult.getSourceAuthor(i) == null || blameResult.getSourceCommit(i) == null) { LOG.debug("Unable to blame file {}. No blame info at line {}. Is file committed? [Author: {} Source commit: {}]", inputFile.relativePath(), i + 1, blameResult.getSourceAuthor(i), blameResult.getSourceCommit(i)); return; .date(blameResult.getSourceCommitter(i).getWhen()) .revision(blameResult.getSourceCommit(i).getName()) .author(blameResult.getSourceAuthor(i).getEmailAddress()));
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() ) );
BlameResult blameResult = git.blame().setFilePath( ... ).call(); int size = blameResult.getResultContents().size(); for( int i = 0; i < size; i++ ) { System.out.println( blameResult.getSourceAuthor( i ) ); }
blame = BlameResult.create(generator); begin = 0; end = blame.getResultContents().size(); if (rangeString != null) parseLineRangeOption(); blame.computeRange(begin, end); int maxSourceLine = 1; for (int line = begin; line < end; line++) { RevCommit c = blame.getSourceCommit(line); if (c != null && !c.has(scanned)) { c.add(scanned); while (line + 1 < end && blame.getSourceCommit(line + 1) == c) line++; maxSourceLine = Math.max(maxSourceLine, blame.getSourceLine(line)); RevCommit c = blame.getSourceCommit(line); String commit = abbreviate(c); String author = null; outw.format(pathFmt, path(line)); if (showSourceLine) outw.format(numFmt, valueOf(blame.getSourceLine(line) + 1)); if (!noAuthor) outw.format(authorFmt, author, date); outw.format(lineFmt, valueOf(line + 1)); outw.flush(); blame.getResultContents().writeLine(outs, line);
private String date(int line) { if (blame.getSourceCommit(line) == null) return ""; //$NON-NLS-1$ PersonIdent author = blame.getSourceAuthor(line); if (author == null) return ""; //$NON-NLS-1$ dateFmt.setTimeZone(author.getTimeZone()); if (!showRawTimestamp) return dateFmt.format(author.getWhen()); return String.format("%d %s", //$NON-NLS-1$ valueOf(author.getWhen().getTime() / 1000L), dateFmt.format(author.getWhen())); }
int lineCount = result.getResultContents().size(); BlameRevision previous = null; for (int i = 0; i < lineCount; i++) { RevCommit commit = result.getSourceCommit(i); if (commit == null) {
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 given revision, this excludes changes from the last two commits due to the "~~" above int lines = countLinesOfFileInCommit(repository, commitID, "README.md"); for (int i = 0; i < lines; i++) { RevCommit commit = blame.getSourceCommit(i); System.out.println("Line: " + i + ": " + commit); } final int currentLines; try (final FileInputStream input = new FileInputStream("README.md")) { currentLines = IOUtils.readLines(input, "UTF-8").size(); } System.out.println("Displayed commits responsible for " + lines + " lines of README.md, current version has " + currentLines + " lines"); } }
private int findLine(int b, String regex) { String re = regex.substring(1, regex.length() - 1); if (!re.startsWith("^")) //$NON-NLS-1$ re = ".*" + re; //$NON-NLS-1$ if (!re.endsWith("$")) //$NON-NLS-1$ re = re + ".*"; //$NON-NLS-1$ Pattern p = Pattern.compile(re); RawText text = blame.getResultContents(); for (int line = b; line < text.size(); line++) { if (p.matcher(text.getString(line)).matches()) return line; } return b; }
private String author(int line) { PersonIdent author = blame.getSourceAuthor(line); if (author == null) return ""; //$NON-NLS-1$ String name = showAuthorEmail ? author.getEmailAddress() : author .getName(); return name != null ? name : ""; //$NON-NLS-1$ }
/** {@inheritDoc} */ @Override public String toString() { StringBuilder r = new StringBuilder(); r.append("BlameResult: "); //$NON-NLS-1$ r.append(getResultPath()); return r.toString(); }
public List<BlamedLine> blame(String file, String commitToBeBlamed, boolean priorCommit) { try (Git git = openRepository()) { ObjectId gitCommitToBeBlamed; if (priorCommit) { Iterable<RevCommit> commits = git.log().add(git.getRepository().resolve(commitToBeBlamed)).call(); gitCommitToBeBlamed = commits.iterator().next().getParent(0).getId(); } else { gitCommitToBeBlamed = git.getRepository().resolve(commitToBeBlamed); } BlameResult blameResult = git.blame().setFilePath(file).setStartCommit(gitCommitToBeBlamed).setFollowFileRenames(true).call(); if (blameResult != null) { int rows = blameResult.getResultContents().size(); List<BlamedLine> result = new ArrayList<>(); for (int i = 0; i < rows; i++) { result.add(new BlamedLine(i, blameResult.getResultContents().getString(i), blameResult.getSourceAuthor(i).getName(), blameResult.getSourceCommitter(i).getName(), blameResult.getSourceCommit(i).getId().getName())); } return result; } else { throw new RuntimeException("BlameResult not found."); } } catch (Exception e) { throw new RuntimeException(e); } }
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; if (start == 0 && end == resultContents.size()) { computeAll(); return; if (hasSourceData(start, end)) return; loadFrom(gen);
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 given revision, this excludes changes from the last two commits due to the "~~" above int lines = countLinesOfFileInCommit(repository, commitID, "README.md"); for (int i = 0; i < lines; i++) { RevCommit commit = blame.getSourceCommit(i); System.out.println("Line: " + i + ": " + commit); } final int currentLines; try (final FileInputStream input = new FileInputStream("README.md")) { currentLines = IOUtils.readLines(input, "UTF-8").size(); } System.out.println("Displayed commits responsible for " + lines + " lines of README.md, current version has " + currentLines + " lines"); } }
end = blame.getResultContents().size(); else if (endStr.startsWith("/")) //$NON-NLS-1$ end = findLine(begin, endStr);
@Override public String toString() { StringBuilder r = new StringBuilder(); r.append("BlameResult: "); //$NON-NLS-1$ r.append(getResultPath()); return r.toString(); }