Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I diff two git branches in bitbucket pipeline

Tags:

git

bitbucket

Background

I want to make sure that all commit messages on the pushed branch have a time log in it

ie. add readme /spend 5m

Problem

I want to get the commit diff between two git branches in bitbucket pipeline,

this is my yaml pipeline config:

pipelines:
  default:
    - step:
        script:
          - git log $BITBUCKET_BRANCH --oneline --not master

$BITBUCKET_BRANCH is the branch the pipeline is acting on.

but the pipeline is returning an error when trying to compare with master

+ git log $BITBUCKET_BRANCH --oneline --not master
fatal: ambiguous argument 'master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Note that the setup step in the pipeline(this is pre-defined by bitbucket and I can't change it)

git clone --branch="abdullah-s/bitbucketpipelinesyml-created-online-wit-1489917130851" --depth 50 https://x-token-auth:[email protected]/abdullah-s/webook.git $BUILD_DIR;
git reset --hard ac61f080a28428bdd885735374164577a2b0aa43;
git remote set-url origin [email protected]:abdullah-s/webook.git

in the first command of the setup, bitbucket is cloning only one branch from my repo

What I tried

I tried to pull master

- git checkout -b master
- git pull origin master
- git log $BITBUCKET_BRANCH --oneline --not master

but got an error

+ git pull origin master
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

Question

How can I compare two branches in bitbucket pipeline?

like image 210
evexoio Avatar asked Mar 19 '17 11:03

evexoio


People also ask

How do I see the difference between two branches in bitbucket?

From the sidebar, click Compare. In the Compare page, from both the Source and Destination dropdown, select any combination of branches, tags, or commits. Once selections are made, the comparison results display in a diff and a commits list tab.

How do I pull from one branch to another in bitbucket?

The Bitbucket interface gives you the basic command for checking out a branch. If you're using Sourcetree, Bitbucket gives you a single button checkout. In the repository's Branches, click the branch you want to checkout. Press the Check out button to display the appropriate check out command.

How do I compare two branches in Bitbucket?

From the sidebar, click Compare. In the Compare page, from both the Source and Destination dropdown, select any combination of branches, tags, or commits. Once selections are made, the comparison results display in a diff and a commits list tab. Bitbucket uses git diff...

How do I compare two Git branches?

This is used to compare the most recent commitment of one branch to its base on the other branch, i.e. the two branches' common ancestor. Various variants of the same file may exist in two branches. We can use the Git Diff command to compare these updates by specifying the file name or location.

How do I create a pipeline in Bitbucket?

The pipeline will be executed on every change pushed into the specified branches (e.g. dev and sync the changes with the remote GitHub repository). Log in to the BitBucket repository, click on “Pipelines,” and click on “Create your first pipeline”: Enable the configurations below in bitbucket-pipelines.yml and commit the changes.

How do I Checkout a branch in Bitbucket?

Bitbucket offers a single-button checkout if you’re utilizing Sourcetree. Select the branch you wish to checkout from the repository’s Branches page. To show the proper check out instruction, press the Check out button. The “ sourcetree compare two branches ” is a tool that allows users to compare different branches in Bitbucket.


1 Answers

As you rightfully point out, Bitbucket pipelines will only clone the specific branch that triggered the build.

As such, the RefSpec will be set to the specific branch, and you'll be unable to merge or diff other branches.

For example, if the build was triggered on the develop branch, then the following refspec will be set:

[remote "origin"]
    url = [email protected]:xxxxxx
    fetch = +refs/heads/develop:refs/remotes/origin/develop
[branch "develop"]
    remote = origin
    merge = refs/heads/develop

If you look at the available branches you'll see this :

+ git branch -a
* develop
  remotes/origin/develop

You can execute the following command :

git fetch origin "+refs/heads/*:refs/remotes/origin/*"

to pull in all other branches / tags

From bitbucket.org:xxxx/xxxxx
 * [new branch]      master     -> origin/master
 * [new branch]      release    -> origin/release
 * [new tag]         xxxx -> xxxx
like image 190
ddewaele Avatar answered Nov 10 '22 00:11

ddewaele