-
-
Notifications
You must be signed in to change notification settings - Fork 507
/
BlameFile.java
47 lines (42 loc) · 1.74 KB
/
BlameFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package org.dstadler.jgit.porcelain;
import org.dstadler.jgit.helper.CookbookHelper;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.blame.BlameResult;
import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
// Simple example that shows how to get the Blame-information for a file
public class BlameFile {
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());
}
final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");
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 ? " - " + DATE_FORMAT.format(((long)sourceCommit.getCommitTime())*1000) +
" - " + sourceCommit.getName() : "") +
": " + rawText.getString(i));
}
}
}
}
}