<div dir="ltr">+1<br>
</div><span>
</span><br><div class="gmail_quote"><div dir="ltr">Andrew Shadura <<a href="mailto:andrew@shadura.me">andrew@shadura.me</a>> schrieb am Sa., 15. Apr. 2017, 14:12:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"># HG changeset patch<br>
# User Andrew Shadura <<a href="mailto:andrew@shadura.me" target="_blank">andrew@shadura.me</a>><br>
# Date 1492258239 -7200<br>
#      Sat Apr 15 14:10:39 2017 +0200<br>
# Node ID 977c0cfec8c00806d4331ebf402ce492657cf34a<br>
# Parent  a1f8bf0428c5a3123da9c3ba2dbd8ada50be0b0e<br>
git: add references for Git pull request heads<br>
<br>
When a pull request is created, stick a reference to it in the refs/pull<br>
namespace, similarly to what GitHub does, and remove it when the pull<br>
request is deleted. That reference guarantees the commits stay around<br>
and don't get garbage-collected when a PR is rebased or revised.<br>
Also, that makes it possible to access head of the historis pull<br>
requests using Git by fetching a ref from the source repository.<br>
<br>
Unlike GitHub though, we don't put the ref into the destination repository<br>
and don't copy commits there to prevent undesired repository growth.<br>
Kallithea uses global pull request identifiers, so there should not be<br>
any confusion as to what pull request the ref corresponds to.<br>
We may later provide a shim to redirect users to the source repository<br>
if that deems needed.<br>
<br>
diff --git a/kallithea/model/pull_request.py b/kallithea/model/pull_request.py<br>
--- a/kallithea/model/pull_request.py<br>
+++ b/kallithea/model/pull_request.py<br>
@@ -39,8 +39,7 @@ from kallithea.lib import helpers as h<br>
 from kallithea.model.db import PullRequest, PullRequestReviewer, Notification, \<br>
     ChangesetStatus, User<br>
 from kallithea.model.notification import NotificationModel<br>
-from kallithea.lib.utils2 import extract_mentioned_users, safe_unicode<br>
-<br>
+from kallithea.lib.utils2 import extract_mentioned_users, safe_str, safe_unicode<br>
<br>
 log = logging.getLogger(__name__)<br>
<br>
@@ -139,6 +138,12 @@ class PullRequestModel(object):<br>
     def delete(self, pull_request):<br>
         pull_request = PullRequest.guess_instance(pull_request)<br>
         Session().delete(pull_request)<br>
+       if pull_request.org_repo.scm_instance.alias == 'git':<br>
+           # remove a ref under refs/pull/ so that commits can be garbage-collected<br>
+           try:<br>
+               del pull_request.org_repo.scm_instance._repo["refs/pull/%d/head" % pull_request.pull_request_id]<br>
+           except KeyError:<br>
+               pass<br>
<br>
     def close_pull_request(self, pull_request):<br>
         pull_request = PullRequest.guess_instance(pull_request)<br>
@@ -228,6 +233,7 @@ class CreatePullRequestAction(object):<br>
         self.org_repo = org_repo<br>
         self.other_repo = other_repo<br>
         self.org_ref = org_ref<br>
+        self.org_rev = org_rev<br>
         self.other_ref = other_ref<br>
         self.title = title<br>
         self.description = description<br>
@@ -252,6 +258,10 @@ class CreatePullRequestAction(object):<br>
         Session().add(pr)<br>
         Session().flush() # make database assign pull_request_id<br>
<br>
+        if self.org_repo.scm_instance.alias == 'git':<br>
+            # create a ref under refs/pull/ so that commits don't get garbage-collected<br>
+            self.org_repo.scm_instance._repo["refs/pull/%d/head" % pr.pull_request_id] = safe_str(self.org_rev)<br>
+<br>
         #reset state to under-review<br>
         from kallithea.model.changeset_status import ChangesetStatusModel<br>
         from kallithea.model.comment import ChangesetCommentsModel<br>
_______________________________________________<br>
kallithea-general mailing list<br>
<a href="mailto:kallithea-general@sfconservancy.org" target="_blank">kallithea-general@sfconservancy.org</a><br>
<a href="https://lists.sfconservancy.org/mailman/listinfo/kallithea-general" rel="noreferrer" target="_blank">https://lists.sfconservancy.org/mailman/listinfo/kallithea-general</a><br>
</blockquote></div>