My goal is to fetch a list of files that were modified between 2 commits (or in 1 commit) using the git diff-tree plumbing command, and I want to do this inside a GitHub Actions workflow on the ubuntu-latest runtime.
The problem is that the git diff-tree stdout / stderr never appears on screen, and I can’t pipe it to a file. I tried running the command in a step run block, python script and inside a private Docker container action to no avail. Another post describes a solution, but this did not work for me. What’s interesting is that I can see the output of other git commands like git --version or even git help diff-tree but not the output of git diff-tree ... itself.
My actions workflow configuration is basic:
ubuntu-latest runtime
push events only
private repository
Is GitHub preventing me from using the git diff-tree command? If so, where is this documented? Has anyone else encountered this issue?
Here is a snippet of the bash script I want to execute.
Thank you for the quick response and making the extra effort of looking inside one of my repositories. A fetch-depth of 1 is definitely the issue here. Increasing it solved my issue.
In case you’re still looking for answer @shotor or if someone has the same issue, this “bad object” error is happening because of the default config for actions/checkout@v2:
Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set fetch-depth: 0 to fetch all history for all branches and tags.
Previous answer (by @pozil) fetches full repository and it may be slow on big repositories. My solution is to fetch with depth 1 by using actions/checkout and fetch later manually. Also my solution works for both Pull Requests and normal pushes to branches.
on: [push, pull_request]
jobs:
job1:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Check for changes
id: diff
run: |
if [ $GITHUB_BASE_REF ]; then
# Pull Request
git fetch origin $GITHUB_BASE_REF --depth=1
export DIFF=$( git diff --name-only origin/$GITHUB_BASE_REF $GITHUB_SHA )
echo "Diff between origin/$GITHUB_BASE_REF and $GITHUB_SHA"
else
# Push
git fetch origin ${{ github.event.before }} --depth=1
export DIFF=$( git diff --name-only ${{ github.event.before }} $GITHUB_SHA )
echo "Diff between ${{ github.event.before }} and $GITHUB_SHA"
fi
echo "$DIFF"
# Escape newlines (replace \n with %0A)
echo "::set-output name=diff::$( echo "$DIFF" | sed ':a;N;$!ba;s/\n/%0A/g' )"
- run: echo "${{ steps.diff.outputs.diff }}"
CPython uses similar way to run tests only if changed files are not in Docs directory (for Pull Requests). For pushes to master they always run tests.