-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to get all commit history for a single branch? #520
Comments
This is very painful in our ever growing monorepo. |
If you persist credentials, you can just check out a single commit, and then fetch whatever you want yourself. For example: steps:
- uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579
with:
ref: "${{ github.event.pull_request.head.sha }}"
- run: git fetch --prune --progress origin +refs/heads/${{ github.event.pull_request.head.ref }}:refs/remotes/origin/${{ github.event.pull_request.head.ref }} This will fetch all refs for the PR branch. This is a hack, though, and it require re-fetching objects and re-resolving deltas. |
It would probably be better if there was just an action that set up credentials and let users run their own fetches. |
Here's the job yml for the workaround I just figured out for my use case of running tests and linting on pull requests on my team's Nx monorepo, in case anyone else finds this later with a similar need: lint-and-test:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Add PR base ref
# Fetch the ref of the base branch, just the single commit.
run: git fetch --depth=1 origin +refs/heads/${{github.base_ref}}:refs/remotes/origin/${{github.base_ref}}
- name: Track PR base branch
# Turn the just-fetched ref into a local branch.
run: git branch --track ${{github.base_ref}} origin/${{github.base_ref}}
- name: Fetch commits in-between base and HEAD
# While the ancestor commit between HEAD and the branch base isn't in the local tree
# fetch X more parent commits of the branch base and branch head.
run: |
while [ -z $( git merge-base ${{github.base_ref}} HEAD ) ]; do
git fetch --deepen=10 origin ${{github.base_ref}} HEAD;
done
# Sets the commit SHAs of head and branch base into two environment variables
# that `nx affected` will read from.
- name: Derive SHAs for base and head for `nx affected` commands
uses: nrwl/nx-set-shas@v2
with:
main-branch-name: ${{github.base_ref}}
- uses: actions/setup-node@v2
with:
node-version: 16.x
cache: yarn
- name: Install dependencies
run: yarn install
- name: Lint
run: yarn nx affected --target=lint
- name: Tests
run: yarn nx affected --target=test |
Single branch history:Assuming "all" history is from where the branch started/branched from: Pull Requests- name: 'PR commits + 1'
run: echo "PR_FETCH_DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> "${GITHUB_ENV}"
- name: 'Checkout PR branch and all PR commits'
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: ${{ env.PR_FETCH_DEPTH }} NOTE: Prefer a
The overhead should be minimal AFAIK, especially with Workflows triggered by events that aren't pull requests (but act on PR branches) -
|
I use
|
@floating-cat That's a cool solution... I'll give it a try! |
BTW |
I'm using this action like this (this works splendidly):
I only need to get the entire commit history for just that one
${{ env.BRANCH }}
. But usingfetch-depth: 0
gets all commit history for all branches and tags, which is unnecessary in my case. Is there a way to get the entire commit history for just the branch referenced in theref
?The text was updated successfully, but these errors were encountered: