Bachmann1234 / diff_cover

Automatically find diff lines that need test coverage.
Apache License 2.0
699 stars 190 forks source link

Command "git diff {branch}...HEAD" fails (master fetch doesn't help) #55

Open twall opened 7 years ago

twall commented 7 years ago

Jenkins SCM module does the following (minimal representation, it actually does a few more things):

git checkout git@github.com:<path>.git <dir>
cd <dir>
git checkout -f <commit-hash>

If I invoke git diff {branch}...HEAD (as is done by diff-cover), I get the same error as is produced when running diff-cover:

Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Preceding the diff with the recommended fetch does not work either:

git fetch origin $(TARGET_BRANCH):refs/remotes/origin/$(TARGET_BRANCH)

In this case, TARGET_BRANCH is "master", which is the repo's default branch.

Output of "git branch":

% git branch
* (HEAD detached at 0fcb182)
  master

The full Jenkins SCM operation looks like this:

 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@github.com:<repo-path> # timeout=10
Fetching upstream changes from git@github.com:<repo-path>
 > git --version # timeout=10
using GIT_SSH to set credentials 
 > git -c core.askpass=true fetch --tags --progress git@github.com:<repo-path> +refs/pull/*:refs/remotes/origin/pr/* +refs/heads/master:refs/remotes/origin/master
 > git rev-parse 0fcb1822ad5bcf71b9e0c7cddb85dcd5a54d4967^{commit} # timeout=10
Checking out Revision 0fcb1822ad5bcf71b9e0c7cddb85dcd5a54d4967 (detached)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 0fcb1822ad5bcf71b9e0c7cddb85dcd5a54d4967
 > git rev-list 7ea84cf781acc06bb72767860dc1132d5663a90d # timeout=10
Bachmann1234 commented 7 years ago

Howdy stranger!

I'll see if I can take a look at this. But as I've been saying in other things that have come up my free time has dried up dramatically recently so it may be a bit. We can talk in a less public forum if your curious.

But yeah, I'll try and look this week. No promises though

Bachmann1234 commented 7 years ago

Hey, So the issue title makes me think you read the troubleshooting but I wanted to be extra sure.

When you say 'fetch master did not help' did you specifically make sure to try

git fetch origin master:refs/remotes/origin/master from the readme?

twall commented 7 years ago

No, I tried the troubleshooting tip, but it didn't work.

An explicit checkout of the target branch (master) then switching back works, so I'm trying to figure out what happens differently in that case w/r/t symbolic references.

I believe if you do a fresh clone of a repo and snag a hash as described above you can reproduce reliably (don't need jenkins).

f18m commented 3 years ago

Hi @twall , @Bachmann1234 , I think I hit a similar problem. In my case I'm not using Jenkins but rather Teamcity. Also Teamcity does something like "git checkout -f " at some point and thus clones the git repo in a "HEAD detached" condition (https://www.git-tower.com/learn/git/faq/detached-head-when-checkout-commit/). I tried and indeed a command like "git diff origin/develop..HEAD" in such condition fails with error

fatal: ambiguous argument 'origin/develop..HEAD': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'

I'm not sure how to workaround this... any solution or workaround so far ?

altendky commented 3 years ago

For what it's worth... This didn't work.

    - name: Make ${{ github.base_ref }} branch exist
      run: |
        git fetch origin master:refs/remotes/origin/master
diff_cover.command_runner.CommandError: fatal: ambiguous argument 'master...HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

But this did.

    - name: Make ${{ github.base_ref }} branch exist
      run: |
        git checkout -b ${{ github.base_ref }} --track origin/${{ github.base_ref }}
        git checkout -

Though now I'm just passing in the origin/master or the parent hash depending. (isn't choosing either this or that just silly in GHA...)

    - name: Run tox environment
      env:
        BASE_REF: ${{ fromJSON(format('[{0}, {1}]', toJSON(github.event.before), toJSON(format('origin/{0}', github.base_ref))))[github.base_ref != ''] }}
      run: |
        tox --skip-pkg-install -- --compare-branch="${BASE_REF}"
[testenv:check-coverage]
changedir = {toxinidir}
commands =
    coverage combine coverage_reports/
    coverage xml -o {toxinidir}/coverage.xml
    coverage report --fail-under=90 --ignore-errors
    diff-cover --fail-under=100 {posargs:--compare-branch=master} coverage.xml

https://github.com/Parquery/pylddwrap/pull/20

dmcconnell-m commented 2 years ago

I was able to get this to work in GitHub Actions by doing 2 separate checkouts in my workflow (1 for master / main first, then the branch / ref that triggered the workflow, my PR branch in my case):

      - name: Check out the repo
        uses: actions/checkout@v2
        with:
          ref: master
      - name: Check out the repo
        uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v1
        with:
          python-version: "3.7"
      - name: Install diff_cover
        run: |
          python -m pip install --upgrade pip
          pip install diff_cover
      - run: diff-cover coverage.xml --compare-branch=origin/master --diff-range-notation '..'

I have added this in case anyone else is trying to get diff_cover working with GitHub Actions