summaryrefslogtreecommitdiffstats
path: root/Differ.java
blob: b53f7fe49144d8c08d1e675dc41e92d4c86b4ae7 (plain) (blame)
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
/*
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2025 The FreeBSD Foundation
 *
 * This software was developed by Eilertsens Kodeknekkeri <haraldei@anduin.net>
 * under sponsorship from The FreeBSD Foundation.
 */

import java.io.IOException;
import java.io.StringWriter;

public class Differ
{
	private String commit;

	public static void main(String[] args)
	{
		var differ = new Differ(args);
		differ.run();
	}

	public Differ(String[] args)
	{
		commit = args[0];
	}

	public void run()
	{
		try {
			String[] cmd = {"git", "diff", "--stat", "--name-only", commit};
			var p = Runtime.getRuntime().exec(cmd);

			p.inputReader()
				.lines()
				.filter(filename -> { return includeDiff(filename); })
				.forEach(filename -> { System.out.println("[ ] " + filename); });
		} catch (IOException e) {
			System.err.println("That didn't work: " + e);
		}
	}

	private boolean includeDiff(String filename)
	{
		try {
			String[] cmd = {"git", "diff", commit, "--", filename};
			// System.out.println("[*] Running `" + String.join(" ", cmd) + "`...");
			var p = Runtime.getRuntime().exec(cmd);

			return p.inputReader()
				.lines()
				.anyMatch(line -> { return interestingLine(line); });

		} catch (IOException e) {
			System.err.println("Could not fetch diff: " + e);
			return false;
		}
	}

	private boolean interestingLine(String line)
	{
		var interesting = line.matches("^[+-][^+-].*") && ! line.matches("^.*Copyright.*");

		// System.out.println((interesting ? "! " : "  ") + line);

		return interesting;
	}
}