Git fetch Pull Requests from Azure DevOps
Asked Answered
C

2

10

I want to fetch, using git, the list of Pull Requests in a repository hosted in Azure DevOps.

Github has the support. We can just add this to .git/config:

[remote "origin"]
  fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

But when I tried the same method for Azure DevOps, it didn't fetch anything. I also tried git-pr from the git-extras package.

I am guessing if they have the support, the path might be different from refs/remotes/origin/pr/.They have a REST API endpoint to fetch pull requests, but I couldn't find anything about doing the same using git.

Civilian answered 27/5, 2021 at 3:45 Comment(2)
one way to discover what refs are available on a remote : git ls-remote <remote or url>Hoarhound
@LeGEC, thanks! git ls-remote is the solution for any remote.Civilian
M
20

For Azure DevOps the configuration is slightly different. You can add the following to .git/config:

[remote "origin"]
        fetch = +refs/pull/*/merge:refs/remotes/origin/pull/*

It is also possible to perform an ad-hoc fetch of a pull-request into a local branch without prior configuration like this:

git fetch origin refs/pull/$ID/merge:$LOCALBRANCH

Example fetch and checkout (with configuration):

$ git fetch origin -v
remote: Azure Repos
remote: Found 1 objects to send. (18 ms)
Unpacking objects: 100% (1/1), done.
From ssh.dev.azure.com:v3/aaaa/bbbb/cccc
 = [up to date]      main              -> origin/main
 = [up to date]      test-branch       -> origin/test-branch
 * [new ref]         refs/pull/1/merge -> origin/pull/1
$ git checkout pull/1
Branch 'pull/1' set up to track remote branch 'pull/1' from 'origin'.
Switched to a new branch 'pull/1'

Example ad-hoc fetch and checkout (no prior configuration):

$ git fetch origin refs/pull/1/merge:pull/1
remote: Azure Repos
remote: Found 1 objects to send. (53 ms)
Unpacking objects: 100% (1/1), done.
From ssh.dev.azure.com:v3/aaaa/bbbb/cccc
 * [new ref]         refs/pull/1/merge -> pull/1
$ git checkout pull/1
Switched to branch 'pull/1'
Mesocratic answered 27/5, 2021 at 7:18 Comment(1)
A note about the config change described above, git config --add remote.origin.fetch "+refs/pull/*/merge:refs/remotes/origin/pull/*". If you follow the first example -- automatically setting up the new local branch with something like git checkout pull/1 -- you may have just created a broken branch.pull/1.merge config value, and you may have trouble trying to just git pull it again to get PR updates. See here for how to address that issue (there 'head' was used, not 'merge' -- so not sure if it fully applies here).Alcot
C
0

It'd be better to define a new remote with the same url but different fetch config.

 [remote "pull"]
     url = <same-as-origin>
     fetch = +refs/pull/*/merge:refs/remotes/pull/*      

You can check out pull requests with:

git fetch pull
git checkout pull/<pull-request-id>

This will avoid the merge conflict that would arise when you are using the same remote with two different fetch configs.

Here's a similar answer with explanation: https://mcmap.net/q/74600/-what-is-the-correct-git-config-for-working-with-github-pull-requests

Chard answered 13/2 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.