Home > Back-end >  Unable to list changed files in Gitlab pipeline: fatal: ambiguous argument: unknown revision or path
Unable to list changed files in Gitlab pipeline: fatal: ambiguous argument: unknown revision or path

Time:09-28

I am using the following line to collect changed files in my branch

git diff --name-only "$CI_COMMIT_BRANCH"^! | grep ...

But getting the following error

fatal: ambiguous argument 'my/branch^!': unknown revision or path not in the working tree

Why and how to fix?

CodePudding user response:

I don't think GitLab CI checks out the branch, but it instead runs the pipeline in detached HEAD mode. You can try "origin/$CI_COMMIT_BRANCH^!", maybe the remote tracking branch is set up and available.

But since the pipeline always checks out the branch, you can simply use HEAD to reference the current commit.

Depending on what you want to achieve, the solution might be simple or impossible:

  1. "Compare against the default branch of the repo" (usually main or master): git diff --name-only "$CI_DEFAULT_BRANCH" HEAD
  2. "Compare against the target of the merge request" (only possible in merge request pipelines): git diff --name-only "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"...HEAD (... here takes the merge base of the two commits, consequently showing you the changes since your branch was created, but ignoring any changes made on the target branch)
  3. Anything else: likely impossible or very difficult. At least not answerable with the current information given. (And it feels like the question is showing an error, but actually asking about a different use-case. Makes me wonder if this is XY problem question)
  • Related