Skip to content

Commit

Permalink
2286: Git jcheck --working-tree/--staged not compatible with some checks
Browse files Browse the repository at this point in the history
Reviewed-by: erikj
  • Loading branch information
zhaosongzs committed Jun 7, 2024
1 parent c40e621 commit c93a0d0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
6 changes: 5 additions & 1 deletion cli/src/main/java/org/openjdk/skara/cli/GitJCheck.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -325,10 +325,14 @@ else if (confFile || confWorkingTree || workingTree) {
if (staged) {
ranges.clear();
ranges.add(STAGED_REV);
System.out.println("When jcheck is running on staged, only the following commit checks are available: " +
JCheck.commitCheckNamesForStagedOrWorkingTree());
}
if (workingTree) {
ranges.clear();
ranges.add(WORKING_TREE_REV);
System.out.println("When jcheck is running on working-tree, only the following commit checks are available: " +
JCheck.commitCheckNamesForStagedOrWorkingTree());
}

var isLax = getSwitch("lax", arguments);
Expand Down
17 changes: 16 additions & 1 deletion jcheck/src/main/java/org/openjdk/skara/jcheck/JCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public class JCheck {
private final CommitMessageParser parser;
private final String revisionRange;
private final List<CommitCheck> commitChecks;
private final static List<CommitCheck> commitChecksForStagedOrWorkingTree = List.of(
new AuthorCheck(),
new CommitterCheck(),
new WhitespaceCheck(),
new ExecutableCheck(),
new SymlinkCheck(),
new BinaryCheck()
);
private final List<RepositoryCheck> repositoryChecks;
private final List<String> additionalConfiguration;
private final JCheckConfiguration overridingConfiguration;
Expand Down Expand Up @@ -138,7 +146,8 @@ private Iterator<Issue> checkCommit(Commit commit) {
}
var finalCensus = census;
var message = parser.parse(commit);
var enabled = conf.checks().enabled(commitChecks);
var availableChecks = (revisionRange.equals(STAGED_REV) || revisionRange.equals(WORKING_TREE_REV)) ? commitChecksForStagedOrWorkingTree : commitChecks;
var enabled = conf.checks().enabled(availableChecks);
var iterator = new MapIterator<>(enabled.iterator(), c -> {
log.finer("Running commit check '" + c.name() + "' for " + commit.hash().hex());
return c.check(commit, message, conf, finalCensus);
Expand Down Expand Up @@ -311,4 +320,10 @@ public static Set<Check> checksFor(ReadOnlyRepository repository, JCheckConfigur
null);
return jcheck.checksForRange();
}

public static List<String> commitCheckNamesForStagedOrWorkingTree() {
return commitChecksForStagedOrWorkingTree.stream()
.map(Check::name)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1716,7 +1716,7 @@ public Optional<List<String>> stagedFileContents(Path path) {
public Commit staged() throws IOException {
var author = new Author(username().orElse("jcheck"), email().orElse("jcheck@none.none"));
var commitMetaData = new CommitMetadata(new Hash("staged"), List.of(head()), author, ZonedDateTime.now(),
author, ZonedDateTime.now(), List.of(""));
author, ZonedDateTime.now(), List.of("Fake commit message for staged"));
return new Commit(commitMetaData, List.of(diffStaged()));
}

Expand All @@ -1727,7 +1727,7 @@ public Commit staged() throws IOException {
public Commit workingTree() throws IOException {
var author = new Author(username().orElse("jcheck"), email().orElse("jcheck@none.none"));
var commitMetaData = new CommitMetadata(new Hash("working-tree"), List.of(head()), author, ZonedDateTime.now(),
author, ZonedDateTime.now(), List.of(""));
author, ZonedDateTime.now(), List.of("Fake commit message for working-tree"));
return new Commit(commitMetaData, List.of(diff(head())));
}

Expand Down

0 comments on commit c93a0d0

Please sign in to comment.