-
Notifications
You must be signed in to change notification settings - Fork 1
/
GitConflictBlame.java
157 lines (128 loc) · 6.17 KB
/
GitConflictBlame.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 Olaf Lessenich
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class GitConflictBlame {
private static final String CONFLICT_START = "<<<<<<<";
private static final String CONFLICT_SEP = "=======";
private static final String CONFLICT_END = ">>>>>>>";
// -e prints the email addresses, -n the original line numbers
private static final String BLAME_CMD = "git blame -e -n";
// we need this to disable the pager
private static final String[] BLAME_ENV = {"GIT_PAGER=cat"};
static HashMap<String, HashMap<String, List<List<Integer>>>> blameChunks(File conflictFile) throws IOException {
/*
* Track location by using the following encoding for the values:
* -1 = out of conflict
* 0 = in variant1
* 1 = in variant2
*/
int location = -1;
// no octopus merges supported for now ;)
String[] revisions = new String[2];
HashMap<String, HashMap<String, List<List<Integer>>>> result = new HashMap<>();
HashMap<String, List<Integer>> chunkAuthors = new HashMap<>();
// run blame
Runtime run = Runtime.getRuntime();
Process pr = run.exec(BLAME_CMD + " " + conflictFile,
BLAME_ENV, conflictFile.getParentFile());
// parse output
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line;
while ((line = buf.readLine()) != null) {
// hack lines by blame into useful output
line = line.replaceAll("^[a-fA-F0-9]+?\\s+?([0-9]+?)\\s+?\\(<(.+?)>.+?\\)(.+?)", "$2:$1:$3");
String[] splitLine = line.split(":", 3);
String author = splitLine[0];
Integer number = Integer.valueOf(splitLine[1]);
String content = splitLine[2].trim();
// control flow seems a bit odd, I did this to collect the revision names before
// pushing the authors into the hashmap
if (content.startsWith(CONFLICT_START)) {
location = 0;
revisions[0] = content.split(" ", 2)[1];
continue;
} else if (content.startsWith(CONFLICT_SEP)) {
location = 1;
} else if (content.startsWith(CONFLICT_END)) {
revisions[1] = content.split(" ", 2)[1];
location = -1;
} else if (location >= 0) {
// we are in one of the conflicting chunks
List<Integer> authorLines = chunkAuthors.containsKey(author)
? chunkAuthors.get(author)
: new ArrayList<>();
authorLines.add(number);
chunkAuthors.put(author, authorLines);
continue;
} else {
continue;
}
// we are at separator or end of a conflict, processing authors found in chunk
String revision = location == -1 ? revisions[1] : revisions[0];
result.putIfAbsent(revision, new HashMap<>());
HashMap<String, List<List<Integer>>> authors = result.get(revision);
for (Map.Entry<String, List<Integer>> entry : chunkAuthors.entrySet()) {
String chunkAuthor = entry.getKey();
List<List<Integer>> authorLines = authors.containsKey(chunkAuthor)
? authors.get(chunkAuthor)
: new ArrayList<>();
authorLines.add(entry.getValue());
authors.put(chunkAuthor, authorLines);
}
chunkAuthors.clear();
}
buf.close();
if (pr.exitValue() != 0) {
buf = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
buf.lines().forEach(System.err::println);
buf.close();
throw new RuntimeException(String.format("Error on external call with exit code %d",
pr.exitValue()));
}
pr.getInputStream().close();
pr.getErrorStream().close();
pr.getOutputStream().close();
return result;
}
static HashMap<String, HashMap<String, List<Integer>>> blameFile(File conflictFile) throws IOException {
HashMap<String, HashMap<String, List<List<Integer>>>> chunkResult = blameChunks(conflictFile);
HashMap<String, HashMap<String, List<Integer>>> fileResult = new HashMap<>();
// just aggregate chunks
for (String revision : chunkResult.keySet()) {
HashMap<String, List<Integer>> authors = new HashMap<>();
HashMap<String, List<List<Integer>>> chunkMap = chunkResult.get(revision);
for (String author : chunkMap.keySet()) {
List<Integer> authorLines = new ArrayList<>();
chunkMap.get(author).forEach(authorLines::addAll);
authors.put(author, authorLines);
}
fileResult.put(revision, authors);
}
return fileResult;
}
}