Reputation: 24256
I'm trying to summit a build definition based on the branch of the commit. However, I cannot find anywhere related to branch information or reference information from the commit info API.
My request looks like this
https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4?api-version=1.0
Response is exactly same as reference:
{
"parents": [],
"treeId": "7fa1a3523ffef51c525ea476bffff7d648b8cb3d",
"push": {
"pushedBy": {
"id": "8c8c7d32-6b1b-47f4-b2e9-30b477b5ab3d",
"displayName": "Chuck Reinhart",
"uniqueName": "[email protected]",
"url": "https://fabrikam-fiber-inc.vssps.visualstudio.com/_apis/Identities/8c8c7d32-6b1b-47f4-b2e9-30b477b5ab3d",
"imageUrl": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_api/_common/identityImage?id=8c8c7d32-6b1b-47f4-b2e9-30b477b5ab3d"
},
"pushId": 1,
"date": "2014-01-29T23:33:15.2434002Z"
},
"commitId": "be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4",
"author": {
"name": "Chuck Reinhart",
"email": "[email protected]",
"date": "2014-01-29T23:32:09Z"
},
"committer": {
"name": "Chuck Reinhart",
"email": "[email protected]",
"date": "2014-01-29T23:32:09Z"
},
"comment": "First cut\n",
"url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4",
"remoteUrl": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_git/Fabrikam-Fiber-Git/commit/be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4",
"_links": {
"self": {
"href": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4"
},
"repository": {
"href": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249"
},
"changes": {
"href": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/commits/be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4/changes"
},
"web": {
"href": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_git/Fabrikam-Fiber-Git/commit/be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4"
},
"tree": {
"href": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/trees/7fa1a3523ffef51c525ea476bffff7d648b8cb3d"
}
}
}
How to get a branch name of the commit?
https://www.visualstudio.com/en-us/docs/integrate/api/git/commits#just-the-commit
Upvotes: 2
Views: 1239
Reputation: 497
Is this something you are trying to do programmatically on a continuous basis, or are you just trying to build a specific version of your code?
If you are trying to do this on a continuous basis, the previous 2 answers state the reason that its not really a valid use case. However if you just need to kick off a build for a particular commit, VSTS already has that ability.
When you click 'Queue Build', enter the commit id be67f8871a4d2c75f13a51c1d3c30ac0d74d4ef4 in the 'Commit' textbox and click Queue. This will perform a checkout of the GIT repo at the time of that commit.
Upvotes: 0
Reputation: 38136
No, there isn’t such REST API to get branch name by a commit. And it’s also unreachable, because a commit can belong more than one branches. Let’s illustrate by below graph:
A---B---E---F master
\ /
C---D dev
This is a branch structure with master
and dev
branch. And dev
branch merge into master
with commit F
. If you want to get the branch name based on commit D
, you will get two branch names. Commit D
belongs to two branch: dev
(first parent) and master
(second parent).
And if you want to get the branch name(s) contain the given commit, you can use git command:
git branch --contains <commit>
Upvotes: 1
Reputation: 31227
I don't know the VSTS api but I think that is not provided out of the box by the api. For the good reason that a commit doesn't belong to a branch but is contained in one or more branches.
Branches are just references on the git history tree (named DAG).
You will have to compute the data yourself. By listing the branches and for each branch go from commits to their parents until you reach the root commit. That's very costly!
Perhaps a little easier, doing that only for a single branch if you want to verify if the commit is in the branch.
Here, I see that you took the root commit (has no parents), so it should be contained in all the branches (except if you have a repository with multiple root commits, which is quite uncommon)
Upvotes: 1