2015-11-12 147 views
0

我试图使用GitHub API获取指定存储库的默认分支(owner/repo)。获取GitHub存储库的存储库默认分支

我没有找到任何有关文件。为了让所有的分支我使用:

curl https://api.github.com/repos/owner/owner/branches 

此输出以下JSON:

[ 
    { 
    "name": "docs", 
    "commit": { 
     "sha": "a3...", 
     "url": "https://api.github.com/repos/owner/repo/commits/a3..." 
    } 
    }, 
    { 
    "name": "master", 
    "commit": { 
     "sha": "8c...", 
     "url": "https://api.github.com/repos/owner/repo/commits/8c..." 
    } 
    } 
] 

这已经是有用的,因为在我的情况下,默认分支或者是master(如果存在)或gh-pages。但我仍然想知道获取GitHub存储库默认分支的正确方法。

回答

3

存储库对象包含default_branch字段。

所以,我可以这样做:

curl https://api.github.com/repos/owner/repo 

{ 
    "id": 29..., 
    "name": "repo", 
    "full_name": "owner/repo", 
    "owner": {...}, 
    "private": false, 
    ... 
    "default_branch": "master", 
    ... 
} 

在这种情况下,default_branchmaster值。