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).
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).
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`:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
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 --"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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`.
So I'm assuming this is a bug in Bitbucket and the only current solution is, use something else (like Gitlab).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.