Skip to content

Commit

Permalink
fixed reverse order of gitignore file. (#4645)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenson3210 authored Nov 5, 2024
1 parent 0b7ecf5 commit 11cb9db
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,21 @@ private List<String> sortRules(String[] originalRules, List<String> newRules) {
}
resultsIndexCurrentlyAt++;
}
return results;

return distinctValuesStartingReversed(results);
}

private <T> List<T> distinctValuesStartingReversed(List<T> list) {
Set<T> set = new LinkedHashSet<>();
ListIterator<T> iterator = list.listIterator(list.size());

while (iterator.hasPrevious()) {
set.add(iterator.previous());
}

List<T> result = new ArrayList<>(set);
Collections.reverse(result);
return result;
}
});
}
Expand All @@ -148,24 +162,39 @@ public void exclude(String path) {
}
if (IGNORED == isIgnored) {
LinkedHashSet<FastIgnoreRule> remainingRules = new LinkedHashSet<>();
for (FastIgnoreRule rule : ignoreNode.getRules()) {
if (!rule.getResult() || !isMatch(rule, nestedPath)) {
// If this rule has nothing to do with the path to remove / it is a negated rule, we keep it.
boolean negated = false;
for (int i = ignoreNode.getRules().size() - 1; i > -1; i--) {
FastIgnoreRule rule = ignoreNode.getRules().get(i);
if (!isMatch(rule, nestedPath)) {
// If this rule has nothing to do with the path to remove, we keep it.
remainingRules.add(rule);
continue;
} else if (rule.toString().equals(nestedPath)) {
// If this rule is an exact match to the path to remove, we remove it.
continue;
} else if (rule.toString().equals("!" + nestedPath)) {
// If we've already negated the path, we remove this negated occurance. Probably the initial order was wrong.
if (!negated) {
remainingRules.add(rule);
negated = true;
}
continue;
} else if (isMatch(rule, nestedPath)) {
StringBuilder rulePath = new StringBuilder(rule.toString());
if (rulePath.toString().contains("*") || ("/" + rule).equals(nestedPath)) {
if (!negated) {
remainingRules.add(new FastIgnoreRule("!" + nestedPath));
negated = true;
}
remainingRules.add(rule);
remainingRules.add(new FastIgnoreRule("!" + nestedPath));
continue;
}
if (!rule.dirOnly()) {
if (!negated) {
remainingRules.add(new FastIgnoreRule("!" + normalizedPath));
negated = true;
}
remainingRules.add(rule);
remainingRules.add(new FastIgnoreRule("!" + normalizedPath));
continue;
}
String pathToTraverse = nestedPath.substring(rule.toString().length());
Expand All @@ -175,19 +204,25 @@ public void exclude(String path) {
String pathToSplit = pathToTraverse.startsWith("/") ? pathToTraverse.substring(1) : pathToTraverse;
pathToSplit = pathToSplit.endsWith("/") ? pathToSplit.substring(0, pathToSplit.length() - 1) : pathToSplit;
String[] splitPath = pathToSplit.split("/");
for (int i = 0; i < splitPath.length; i++) {
String s = splitPath[i];
remainingRules.add(new FastIgnoreRule(rulePath + "*"));
ArrayList<FastIgnoreRule> traversedRemainingRules = new ArrayList<>();
for (int j = 0; j < splitPath.length; j++) {
String s = splitPath[j];
traversedRemainingRules.add(new FastIgnoreRule(rulePath + "*"));
rulePath.append(s);
remainingRules.add(new FastIgnoreRule("!" + rulePath + (i < splitPath.length - 1 || nestedPath.endsWith("/") ? "/" : "")));
traversedRemainingRules.add(new FastIgnoreRule("!" + rulePath + (j < splitPath.length - 1 || nestedPath.endsWith("/") ? "/" : "")));
rulePath.append("/");
}
Collections.reverse(traversedRemainingRules);
remainingRules.addAll(traversedRemainingRules);
negated = true;
continue;
}
// If we still have the rule, we keep it. --> not making changes to an unknown flow.
remainingRules.add(rule);
}
IgnoreNode replacedNode = new IgnoreNode(new ArrayList<>(remainingRules));
ArrayList<FastIgnoreRule> ignoreRules = new ArrayList<>(remainingRules);
Collections.reverse(ignoreRules);
IgnoreNode replacedNode = new IgnoreNode(ignoreRules);
rules.put(impactingFile, replacedNode);
if (CHECK_PARENT == isIgnored(replacedNode, nestedPath)) {
continue;
Expand Down Expand Up @@ -220,18 +255,17 @@ private boolean isMatch(FastIgnoreRule rule, String path) {
}

private IgnoreNode.MatchResult isIgnored(IgnoreNode ignoreNode, String path) {
IgnoreNode.MatchResult isIgnored = CHECK_PARENT;
for (int i = ignoreNode.getRules().size() - 1; i > -1; i--) {
FastIgnoreRule rule = ignoreNode.getRules().get(i);
if (isMatch(rule, path)) {
if (rule.getResult()) {
isIgnored = IGNORED;
return IGNORED;
} else {
return NOT_IGNORED;
}
}
}
return isIgnored;
return CHECK_PARENT;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ void excludedPathsOnlyGetAddedOnce() {
""",
"""
test.yml
!/nested/test.yml
otherfile.yml
nested/test.yml
!/nested/test.yml
""",
spec -> spec.path(".gitignore")
)
Expand Down Expand Up @@ -419,4 +419,41 @@ void newRulesGetAddedBesidesExistingRules() {
)
);
}

@Test
void wrongExactNegationPositionedBeforeIgnoreMovesToCorrectPosition() {
rewriteRun(
spec -> spec.recipe(new ExcludeFileFromGitignore(List.of("directory/test.yml"))),
text(
"""
!/directory/test.yml
/directory/*
""",
"""
/directory/*
!/directory/test.yml
""",
spec -> spec.path(".gitignore")
)
);
}

@Test
void wrongNonExactNegationPositionedBeforeIgnoreDoesNotGetChanged() {
rewriteRun(
spec -> spec.recipe(new ExcludeFileFromGitignore(List.of("directory/test.yml"))),
text(
"""
!directory/test.yml
/directory/*
""",
"""
!directory/test.yml
/directory/*
!/directory/test.yml
""",
spec -> spec.path(".gitignore")
)
);
}
}

0 comments on commit 11cb9db

Please sign in to comment.