Skip to content

Commit

Permalink
add support for retrieving .diff (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajivm authored and cvrebert committed Dec 17, 2013
1 parent 166a90c commit 74128ea
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions restfulgit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,17 @@ def _get_commit_for_refspec(repo, branch_or_tag_or_sha):
raise NotFound("no such branch, tag, or commit SHA")


def _get_patches(repo, commit):
def _get_diff(repo, commit):
if commit.parents:
diff = repo.diff(commit.parents[0], commit)
diff.find_similar()
else:
diff = commit.tree.diff_to_tree(swap=True)
return diff, [patch for patch in diff]
return diff


def _get_patches(diff):
return [patch for patch in diff]


SPLIT_PATCH_TXT_RE = re.compile(r'^\+\+\+\ b\/(.*?)\n(@@.*?)(?=\n^diff|\n\Z)', re.M | re.S)
Expand Down Expand Up @@ -401,7 +405,8 @@ def _repos_convert_commit(repo_key, repo, commit, include_diff=False):
} for c in commit.parents],
}
if include_diff:
diff, patches = _get_patches(repo, commit)
diff = _get_diff(repo, commit)
patches = _get_patches(diff)
patches_txt = _get_patches_txt(diff)
patches_additions = sum(patch.additions for patch in patches)
patches_deletions = sum(patch.deletions for patch in patches)
Expand Down Expand Up @@ -677,6 +682,22 @@ def get_repos_commit(repo_key, sha=None, branch_or_tag_or_sha=None):
return _repos_convert_commit(repo_key, repo, commit, include_diff=True)


@restfulgit.route('/repos/<repo_key>/commit/<branch_or_tag_or_sha>.diff')
@restfulgit.route('/repos/<repo_key>/commit/<sha:sha>.diff')
@corsify
def get_repos_diff(repo_key, sha=None, branch_or_tag_or_sha=None):
repo = _get_repo(repo_key)
if sha:
try:
commit = _get_commit(repo, sha)
except NotFound:
branch_or_tag_or_sha = sha
if branch_or_tag_or_sha:
commit = _get_commit_for_refspec(repo, branch_or_tag_or_sha)
diff = _get_diff(repo, commit)
return Response(diff.patch, mimetype="text/x-diff")


TAG_REF_PREFIX = "refs/tags/"


Expand Down

0 comments on commit 74128ea

Please sign in to comment.