Skip to content

Instantly share code, notes, and snippets.

@jknair0
Created April 19, 2025 12:14
Show Gist options
  • Save jknair0/b3e2f54cfece1d5f099fcad47a2c440b to your computer and use it in GitHub Desktop.
Save jknair0/b3e2f54cfece1d5f099fcad47a2c440b to your computer and use it in GitHub Desktop.
Script to check the most changed folder in the repository at different levels
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
class ChangesCount {
public static void main(String[] args) throws IOException {
List<String> paths = readPaths();
for (int i = 1; i <= 5; i++) {
System.out.println("------------");
System.out.println("LEVEL " + i);
System.out.println("------------");
changeCount(paths, i);
System.out.println();
System.out.println();
}
}
private static void changeCount(List<String> paths, int levels) {
HashMap<String, Integer> changeCount = new HashMap<>();
Set<String> uniqueL1FolderFileNamesSet = new HashSet<>();
for (String path : paths) {
String[] split = path.split("/");
String firstPath = split[0].toLowerCase(Locale.ROOT);
if (isDirectory(firstPath)) {
try {
StringBuilder parentPathBuilder = new StringBuilder(firstPath);
for (int i = 1; i < levels; i++) {
parentPathBuilder.append('/')
.append(split[i]);
}
String pathBuilder = parentPathBuilder.toString();
uniqueL1FolderFileNamesSet.add(pathBuilder);
changeCount.put(pathBuilder, changeCount.getOrDefault(pathBuilder, 0) + 1);
} catch (Exception e) {
// System.out.println("failed for path " + path);
}
}
}
List<String> uniqueL1FolderFileNames = new ArrayList<>(uniqueL1FolderFileNamesSet);
uniqueL1FolderFileNames.sort(Comparator.comparingInt(changeCount::get).reversed());
StringBuilder result = new StringBuilder("Names\t:\tChanges\n");
result.append("--------------------\n");
for (int i = 0; i < Math.min(uniqueL1FolderFileNames.size(), 10); i++) {
String name = uniqueL1FolderFileNames.get(i);
result.append(name).append("\t:\t").append(changeCount.get(name)).append("\n");
}
System.out.println(result);
}
private static List<String> readPaths() throws IOException {
List<String> paths = new ArrayList<>();
// file contains output of
// `git log --since="1 month ago" --name-only --pretty="format:" | grep . > log.txt` on repository
File log = new File("log.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(log))) {
String path;
while ((path = reader.readLine()) != null) {
paths.add(path);
}
}
return paths;
}
private static boolean isDirectory(String path) {
return !path.matches(FILE_REGEX);
}
private static final String FILE_REGEX = ".+\\..+";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment