diff --git a/pagure/api/project.py b/pagure/api/project.py index e86652b..5cfa1c1 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -271,6 +271,17 @@ def api_git_branches(repo, username=None, namespace=None): GET /api/0/fork///git/branches GET /api/0/fork////git/branches + Parameters + ^^^^^^^^^^ + + +-----------------+----------+---------------+--------------------------+ + | Key | Type | Optionality | Description | + +=================+==========+===============+==========================+ + | ``with_commits``| string | Optional | | Include the commit hash| + | | | | corresponding to the | + | | | | HEAD of each branch | + +-----------------+----------+---------------+--------------------------+ + Sample response ^^^^^^^^^^^^^^^ @@ -281,10 +292,23 @@ def api_git_branches(repo, username=None, namespace=None): "branches": ["master", "dev"] } + { + "total_branches": 2, + "branches": { + "master": "16ae2a4df107658b52750063ae203f978cf02ff7", + "dev": "8351c460167a41defc393f5b6c1d51fe1b3b82b8" + } + } + """ + + with_commits = pagure.utils.is_true( + flask.request.values.get("with_commits", False) + ) + repo = _get_repo(repo, username, namespace) - branches = pagure.lib.git.get_git_branches(repo) + branches = pagure.lib.git.get_git_branches(repo, with_commits=with_commits) return flask.jsonify( {"total_branches": len(branches), "branches": branches} diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 30c927c..0339ca9 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -2450,13 +2450,25 @@ def reinit_git(project, repofolder): ) -def get_git_branches(project): +def get_git_branches(project, with_commits=False): """ Return a list of branches for the project :arg project: The Project instance to get the branches for """ repo_path = pagure.utils.get_repo_path(project) - repo_obj = pygit2.Repository(repo_path) - return repo_obj.listall_branches() + repo_obj = PagureRepo(repo_path) + + if with_commits: + branches = {} + + for branch in repo_obj.listall_branches(): + resolved_branch = repo_obj.lookup_branch(branch).resolve() + com = resolved_branch.peel() + if com: + branches[branch] = com.oid.hex + else: + branches = repo_obj.listall_branches() + + return branches def new_git_branch(