arxanas / git-branchless

High-velocity, monorepo-scale workflow for Git
Apache License 2.0
3.37k stars 80 forks source link

[Feature Request] Add commit being operated on as an arg to 'git test' #1337

Closed mfulbright closed 2 weeks ago

mfulbright commented 1 month ago

So far as I know, there is no way to refer to the commit that a git test command is operating on. I would find it useful to be able to do this:

# Fail any commits that still have "TODO_BEFORE_SUBMIT" in their changes: $ git test run --exec '[[ ! $(git diff "$1^" "$1" | grep TODO_BEFORE_SUBMIT) ]]'

$1 represents the commit that git test is currently operating on, e.g. if git test is going to operate on 2 commits, abc and def, then the first test would be [[ ! $(git diff "abc^" "abc" | grep TODO_BEFORE_SUBMIT) ]] and the second test would be [[ ! $(git diff "def^" "def" | grep TODO_BEFORE_SUBMIT) ]]

claytonrcarter commented 3 weeks ago

Hi there. It looks like this may already be supported via the $BRANCHLESS_TEST_COMMIT env var. (I didn't see that mentioned in the wiki docs, but it looks like it's being injected in the code: https://github.com/arxanas/git-branchless/blob/master/git-branchless-test/src/lib.rs#L2645) Can you try this and see if it works for you:

# Fail any commits that still have "TODO_BEFORE_SUBMIT" in their changes:
$ git test run --exec '[[ ! $(git diff "$BRANCHLESS_TEST_COMMIT"^ "$BRANCHLESS_TEST_COMMIT" | grep TODO_BEFORE_SUBMIT) ]]'

Although in this case, I wonder if you could bypass the env var entirely and just do git diff @^ to get the diff for the current commit and its parent.

Fair warning: I didn't test either of these, but maybe they're worth a shot.