Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

How to run git diff in github actions

I am getting this:

Command failed: git diff --name-only HEAD^..HEAD
fatal: ambiguous argument 'HEAD^..HEAD': unknown revision or path not in the working tree.

I want to run git diff --name-only HEAD^..HEAD in my branch to get a list of the files that were changed. It's working locally but not on GitHub actions. What must I do?

My code is this:

name: build
on:
  push:
    branches:
      - main
jobs:
  run:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
      - name: Configure Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 14.x
      - name: Install dependencies
        run: yarn install
      - name: Publish file changes to Slack
        # HERE I run `git diff` in node.js process
        run: "SLACK_TOKEN=${{ secrets.GITHUB_TOKEN }} npx ts-node scripts/publishSlackUpdate"
      - name: Build TOC
        run: make toc
      - name: Commit build changes
        uses: EndBug/add-and-commit@v7
        with:
          author_name: Docs Builder
          author_email: [email protected]
          message: 'Updated build'
          add: '*.md'
question from:https://stackoverflow.com/questions/65944700/how-to-run-git-diff-in-github-actions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you take a look at the documentation for the actions/checkout@v2 action, you'll see it performs a shallow clone with a single revision by default:

    # Number of commits to fetch. 0 indicates all history for all branches and tags.
    # Default: 1
    fetch-depth: ''

Because it only fetches a single revision, there is no HEAD^.

You can fix this by setting the fetch-depth option on the checkout action. Setting it to 0 will fetch the entire history; alternatively, for what you're doing you could probably just set it to 2:

    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...