Skip to content

Commit

Permalink
Bugfix logic, #1
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrothe committed Nov 21, 2024
1 parent 241ea8e commit a6c5a33
Showing 1 changed file with 82 additions and 13 deletions.
95 changes: 82 additions & 13 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public static void main(String[] args) throws IOException, InterruptedException

private static void linearize1(File repo, String main) throws IOException, InterruptedException {
exec(true, repo, "git", "fetch", "--all");

List<String> branches =
exec(
true,
Expand All @@ -26,28 +25,96 @@ private static void linearize1(File repo, String main) throws IOException, Inter
branches.add(main);
}
branches.sort(Comparator.naturalOrder());
// branches.sort(Comparator.reverseOrder());
// Collections.shuffle(branches);

for (int i = 0; i < branches.size(); i++) {
System.out.printf("%02d: %s%n", i + 1, branches.get(i));
}
System.out.println(
"Speedup. Wich branches to exclude? Type for example 2-5 or 2 3 4 5. <Enter> for no excludes:");
String[] excludes = new Scanner(System.in, Charset.defaultCharset()).nextLine().split(" ");
List<Integer> excludeIndices = new ArrayList<>();
for (String ex : excludes) {
if (ex.contains("-")) {
String[] sa = ex.split("-");
int from = Integer.parseInt(sa[0]);
int to = Integer.parseInt(sa[1]);
for (int i = from; i <= to; i++) {
excludeIndices.add(i - 1);
}
} else {
excludeIndices.add(Integer.parseInt(ex) - 1);
}
}
excludeIndices.sort(Comparator.reverseOrder());
for (int ie : excludeIndices) {
System.out.println(branches.remove(ie) + " removed.");
}

TreeMap<String, List<String>> commits = new TreeMap<>();
for (String b : branches) {
System.out.printf("Getting revs of %s ...%n", b);
List<String> revs = exec(true, repo, "git", "rev-list", "--first-parent", b);
revs.remove(0);
commits.put(b, revs);
}
for (int i = 0; i < branches.size(); i++) {
String b1 = branches.get(i);
List<String> c1 = commits.get(b1);
for (int j = i + 1; j < branches.size(); j++) {
String b2 = branches.get(j);
List<String> c2 = commits.get(b2);
boolean isNewer = new HashSet<>(c1).containsAll(c2);
if (isNewer) {
c1.removeAll(c2);
} else {
c2.removeAll(c1);
}
}
}

HashMap<Map.Entry<String, String>, Boolean> ancestorMap = new HashMap<>();
for (int i = 0; i < branches.size(); i++) {
String b1 = branches.get(i);
List<String> c1 = commits.get(b1);
for (int j = i + 1; j < branches.size(); j++) {
String b2 = branches.get(j);
List<String> c2 = commits.get(b2);
System.out.printf("Check %s and %s.%n", b1, b2);
boolean a =
"0".equals(exec(false, repo, "git", "merge-base", "--is-ancestor", b1, b2).get(0));
if (a) {
ancestorMap.put(new AbstractMap.SimpleEntry<>(b1, b2), true);
ancestorMap.put(new AbstractMap.SimpleEntry<>(b2, b1), false);
} else {
boolean b =
"0".equals(exec(false, repo, "git", "merge-base", "--is-ancestor", b2, b1).get(0));
ancestorMap.put(new AbstractMap.SimpleEntry<>(b1, b2), false);
ancestorMap.put(new AbstractMap.SimpleEntry<>(b2, b1), b);
"0"
.equals(
exec(
false,
repo,
"git",
"merge-base",
"--is-ancestor",
c1.get(c1.size() - 1),
c2.get(0))
.get(0));
boolean b =
"0"
.equals(
exec(
false,
repo,
"git",
"merge-base",
"--is-ancestor",
c2.get(c2.size() - 1),
c1.get(0))
.get(0));
AbstractMap.SimpleEntry<String, String> e1 = new AbstractMap.SimpleEntry<>(b1, b2);
AbstractMap.SimpleEntry<String, String> e2 = new AbstractMap.SimpleEntry<>(b2, b1);
if (ancestorMap.containsKey(e1) || ancestorMap.containsKey(e2)) {
throw new RuntimeException("Something went wrong.");
}
ancestorMap.put(e1, a);
ancestorMap.put(e2, b);
}
}
System.out.println("ancestorMap = " + ancestorMap);

TreeMap<String, List<String>> descendants = new TreeMap<>();
for (int i = 0; i < branches.size(); i++) {
String b1 = branches.get(i);
Expand All @@ -57,7 +124,7 @@ private static void linearize1(File repo, String main) throws IOException, Inter
}
String b2 = branches.get(j);
if (ancestorMap.get(new AbstractMap.SimpleEntry<>(b1, b2))) {
System.out.printf("%s is ancestor of %s.%n", b1, b2);
// System.out.printf("%s is ancestor of %s.%n", b1, b2);
boolean isDirectAncestor = true;
for (int k = 0; k < branches.size(); k++) {
if (k == i || k == j) {
Expand Down Expand Up @@ -85,11 +152,13 @@ private static void linearize1(File repo, String main) throws IOException, Inter
for (Map.Entry<String, List<String>> a : descendants.entrySet()) {
System.out.println(a);
}

linearize2(repo, main, descendants, true);
System.out.print("Would you like to continue? Then enter yes: ");
if ("yes".equals(new Scanner(System.in, Charset.defaultCharset()).nextLine())) {
linearize2(repo, main, descendants, false);
}

System.out.println("Program end.");
}

Expand Down

0 comments on commit a6c5a33

Please sign in to comment.