admin管理员组

文章数量:1357307

I want to reduce the number of tests performed when executing a pipeline. I want to do it this way:

  1. Get a list of changed files for the current branch
  2. Run tests only for services that were affected

I can't get information about what files were changed while working on a branch after checking it out. I can only operate with the following constants: CI_COMMIT_BEFORE_SHA and CI_COMMIT_SHA.

I get a list of all commits for the current branch, look for the very first one, get its hash and compare it with the current one. The following command works locally:

git diff --name-only $(git log --walk-reflogs $(git branch --show-current) | grep "commit " | awk '{print $2}' | awk 'END{print}') $CI_COMMIT_SHA

Example:

$ git log --walk-reflogs $(git branch --show-current)
commit 9281739270df6c0cd297954172fe740d8b6 (HEAD -> exp1, origin/exp1
Reflog: exp1@{0} ()
Reflog message: commit: msg
Author:
Date:   Fri Mar 28 17:18:34 2025 +0400

    msg1

commit 4abb16bf1d0357c66e4b37b72b7e3bf2f (origin/master, master)
Reflog: exp1@{1} ()
Reflog message: branch: Created from refs/heads/master
Merge: 2fae6b22b c39f93a12
Author:
Date:   Fri Mar 28 15:28:19 2025 +0300

    msg

Commit with hash 4abb16bf1d0357c66e4b37b72b7e3bf2f is the commit after the command git checkout -b exp1 (initial state of the branch). I need to compare all the following commits with it. Locally this command shows all the commits in the branch and I can find it using awk. In the pipeline this command gives me only the latest and previous commit.

How can I get which files were changed since the checkout command?

I want to reduce the number of tests performed when executing a pipeline. I want to do it this way:

  1. Get a list of changed files for the current branch
  2. Run tests only for services that were affected

I can't get information about what files were changed while working on a branch after checking it out. I can only operate with the following constants: CI_COMMIT_BEFORE_SHA and CI_COMMIT_SHA.

I get a list of all commits for the current branch, look for the very first one, get its hash and compare it with the current one. The following command works locally:

git diff --name-only $(git log --walk-reflogs $(git branch --show-current) | grep "commit " | awk '{print $2}' | awk 'END{print}') $CI_COMMIT_SHA

Example:

$ git log --walk-reflogs $(git branch --show-current)
commit 9281739270df6c0cd297954172fe740d8b6 (HEAD -> exp1, origin/exp1
Reflog: exp1@{0} ()
Reflog message: commit: msg
Author:
Date:   Fri Mar 28 17:18:34 2025 +0400

    msg1

commit 4abb16bf1d0357c66e4b37b72b7e3bf2f (origin/master, master)
Reflog: exp1@{1} ()
Reflog message: branch: Created from refs/heads/master
Merge: 2fae6b22b c39f93a12
Author:
Date:   Fri Mar 28 15:28:19 2025 +0300

    msg

Commit with hash 4abb16bf1d0357c66e4b37b72b7e3bf2f is the commit after the command git checkout -b exp1 (initial state of the branch). I need to compare all the following commits with it. Locally this command shows all the commits in the branch and I can find it using awk. In the pipeline this command gives me only the latest and previous commit.

How can I get which files were changed since the checkout command?

Share Improve this question edited Mar 28 at 14:08 Gleb Kalaychev asked Mar 28 at 8:48 Gleb KalaychevGleb Kalaychev 112 bronze badges 3
  • hi I can't get information about what files were changed while working on a branch after checking it out why? Also, why not use rules: from gitlab-ci? How can I get which files were changed since the checkout on it? "since the checkout" - what does it mean? You want list of changed files in current commit vs previous commit? vs different branch? Usually I get all files in current branch vs master branch. – KamilCuk Commented Mar 28 at 9:33
  • What is the reason that you can only use CI_COMMIT_BEFORE_SHA and CI_COMMIT_SHA? Using CI_MERGE_REQUEST_SOURCE_BRANCH_NAME and CI_MERGE_REQUEST_TARGET_BRANCH_NAME would make this super easy – Michael Kotzjan Commented Mar 28 at 10:05
  • @KamilCuk Added an example to make it clearer what I want to do. – Gleb Kalaychev Commented Mar 28 at 14:05
Add a comment  | 

1 Answer 1

Reset to default 0

The reason, why your command only sees the last commit in your pipeline is that only one commit is checked out in your pipeline. Try calling git fetch before your command. If you want to reduce the output you can also use git fetch --quiet.

I still suggest you try to use CI_MERGE_REQUEST_SOURCE_BRANCH_NAME and CI_MERGE_REQUEST_TARGET_BRANCH_NAME in your pipeline like this:

git fetch --quiet
git diff --name-only ${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}...${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}

this should get you all changed files between your branch and the branch you want to merge on.

本文标签: How can I get list of changes for current branch in GitLab CIStack Overflow