Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

'fatal: bad revision' when using git log with BITBUCKET_COMMIT

liamcmcdermott September 9, 2018

As part of a Bitbucket Pipeline I'm trying to get a list of files that have changed in the last commit (and its parents, back to the divergence from the `master` branch). This is the command the script is running:

git log --name-only --oneline --pretty=format: master..$BITBUCKET_COMMIT --

But this generates an error when the pipeline runs:

fatal: bad revision 'master..f85282eb10125f0de04afba8a87d5745baff5aff'

However, if I copy/paste the revision hash and run the exact command on my local dev machine it prints a list of files (as expected).

Why does this error on Bitbucket Pipelines (but not my local dev environment)?

git version is 2.17.1 (and on my local machine it's 2.17.1 also, the Docker container and my local dev machine are both Ubuntu 18.04).

---

Note, the actual script is in Haskell (using the Turtle library), the relevant part of the script is:

#!/usr/bin/env runhaskell

{-# LANGUAGE OverloadedStrings #-}

import Turtle
import qualified Control.Foldl as Fold

envCommit = "BITBUCKET_COMMIT"

main = do
commit
<- do
ct <- need envCommit
case ct of
Just ct' -> pure ct'
Nothing -> err "ERROR: No commit was specified." >> exit (ExitFailure 1)

let gits = [ "log" , "--name-only" , "--oneline" , "--pretty=format:" , format ("master.."%s) commit, "--" ]
let gitFiles = inproc "git" gits empty -- <- Here is where the script is failing.
let filteredFiles = nub $ grep (suffix ".ts") gitFiles
files <- fold filteredFiles Fold.list

if length files > 0
then procs "tslint" (map lineToText files) empty
else echo "No code files to lint."

But I don't want anyone to have to deal with the cognitive overhead of reading (my bad) Haskell code. This also works fine on my local dev machine (so it isn't the Haskell that's the problem).

2 answers

1 accepted

3 votes
Answer accepted
Matt Ryall
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 17, 2018

The error message "bad revision" indicates that the local repository in your build doesn't contain one of the commits you're referencing.

This happens because Pipelines does a shallow clone by default, containing just the most recent 50 commits on the current branch. So in your example, master probably isn't available in the local repo. Shallow cloning makes your builds faster, because we pull down less data from your Bitbucket repository before starting the builds.

You can configure the clone depth like this:

clone:
depth: 250 # or 'full' for everything

pipelines:
branches:
...

To get another branch included, you'll need to use a full clone, or fetch that branch with Git (requires setting up auth back to Bitbucket).

liamcmcdermott September 18, 2018

I tried adding a clone depth of full, but still got the same error. Presumably this only gets all commits for the current branch?

My `bitbucket-pipelines.yml`:

Screenshot from 2018-09-17 22-36-47.png

liamcmcdermott September 19, 2018

So, instead of doing a full clone (since that doesn't seem to work) should I fetch that branch (and setup auth back to Bitbucket)?

liamcmcdermott September 19, 2018

Tried adding `git fetch origin master` to the script part of the pipeline (clone depth is also set to full) and got the same error message. The git fetch seems to work fine, so I'm confused.

That commit should be available too, since it's the commit that triggered the pipeline.

Screenshot from 2018-09-19 15-56-17.png

Matt Ryall
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 19, 2018

Hmm, yeah maybe a full clone is only for a single branch. You can see the command in the build setup section. I haven’t had a chance to play with this myself yet.

And your fetch should work. But fetching should also show some download activity in the log, so your output looks a bit unusual. 

I’ll need to find someone else to take a look at this as I’m out for a few days now. Thanks for your patience.

liamcmcdermott September 19, 2018

Ok, thanks for the help so far Matt!

StannousBaratheon
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 25, 2018

Hi @liamcmcdermott

This doesn't work because in addition to performing a shallow clone by default, Pipelines also performs a branch checkout. You can see the clone command in the setup section of your build log: `git clone --branch="example" ...`

In order to run the git log command comparing the current revision to the master branch you must do 2 things:

  1. Add clone depth full to your yml file as Matt suggested.
  2. Checkout the master branch before running the log command. Note you may wish to return to the original branch after checking out master.

For example:

clone:
depth: full

pipelines:
default:
- step:
script:
- git checkout master
- git checkout $BITBUCKET_BRANCH
- "git log --name-only --oneline --pretty=format: master..$BITBUCKET_COMMIT --"
Like # people like this
liamcmcdermott October 26, 2018

Fantastic, this appears to work. Thank you so much.

I get why this error happened now, the local branch 'master' doesn't even exist until you check it out, only the remote branch. That's obtuse though, git could really do with some better error messaging here (just whingeing, not expecting you to do anything else).

0 votes
liamcmcdermott September 17, 2018

I set this same script up on Gitlab, the only difference being the "tslint" was changed to "echo". And it works, see screenshot below where it prints `foo.ts`.

Screenshot from 2018-09-17 18-13-11.png

 

So I'm assuming this is a bug in Bitbucket and the only current solution is, use something else (like Gitlab).

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events