Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git diff return 'bad object'?

I am running a ci task on my Gitlab server. One of the actions is to call an exe on the build runner machine which does some updates.

I pass through the SHA1s from $CI_COMMIT_BEFORE_SHA and $CI_COMMIT_SHA and the code calls:

git diff --name-only $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA

Usually it gives me a list of file names which have changed in the commit but sometimes I get the error:

fatal: bad object abcd1234
                  ^
                  |-- This is the $CI_COMMIT_SHA

The repo has just been downloaded to the build runner so it is up to date, why would the git diff return a bad object here?

like image 849
Stefan Avatar asked Jun 22 '26 10:06

Stefan


2 Answers

If the commit is "old" it could be missing from the clone because Gitlab uses a shallow clone with a depth of 20 by default.

This is why you see this message in the job's log:

Fetching changes with git depth set to 20...

Possible solutions:

  • use the web interface to increase this value for your project (or disable shallow cloning).
  • override variable GIT_DEPTH from .gitlab-ci.yml
  • re-clone the repo manually using a standard clone inside its .gitlab-ci.yml scripts
  • stop relying on "old" hashes
like image 91
Gabriel Devillers Avatar answered Jun 25 '26 02:06

Gabriel Devillers


git fetch origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME git diff-tree --no-commit-id --name-only -r $CI_COMMIT_BEFORE_SHA -r $CI_COMMIT_SHA

It helped me!

like image 27
Faik91 Avatar answered Jun 25 '26 02:06

Faik91



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!