GitDiff { public static GitDiff parse(String line) throws Exception { line = line.trim(); String[] split = line.split(","); Map<String, Integer> modifications = new HashMap<>(); Matcher matcher = FILE_PATTERN.matcher(split[0]); if (!matcher.find()) { throw new Exception("Unable to parse diff line: " + line); } int changedFiles = Integer.valueOf(matcher.group("number")); String input; if (split.length > 1) { input = split[1]; matcher = MODIF_PATTERN.matcher(input); if (matcher.find()) { modifications.put(matcher.group("sign"), Integer.valueOf(matcher.group("number"))); } } if (split.length > 2) { input = split[2]; matcher = MODIF_PATTERN.matcher(input); if (matcher.find()) { modifications.put(matcher.group("sign"), Integer.valueOf(matcher.group("number"))); } } int addition = modifications.getOrDefault("+", 0); int deletion = modifications.getOrDefault("-", 0); return new GitDiff(addition, deletion, changedFiles); } GitDiff(int addition, int deletion, int changedFiles); static GitDiff parse(String line); int getAddition(); int getDeletion(); int getChangedFiles(); @Override String toString(); static final GitDiff EMPTY; }
@Test(expected = Exception.class) public void parseIncorrectLineWillThrow() throws Exception { String line = "SmellTracker/src/main/resources/schema/tracker-sqlite.sql | 2 ++"; GitDiff.parse(line); }