summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
author99rgosse <61579380+99rgosse@users.noreply.github.com>2021-10-08 11:59:35 +0200
committerGitHub <noreply@github.com>2021-10-08 12:59:35 +0300
commit88fa9f3fb168bc6c9c2bdc6341b83bc048b38846 (patch)
tree55dca312c4c4adf78a4159f1c2dcb5017c59ae2d /modules
parentb6147152f8a60db3d1b4c134280afa3e9f77e3bc (diff)
downloadgitea-88fa9f3fb168bc6c9c2bdc6341b83bc048b38846.tar.gz
gitea-88fa9f3fb168bc6c9c2bdc6341b83bc048b38846.zip
Remove heads pointing to missing old refs (#17076)
Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'modules')
-rw-r--r--modules/migrations/base/pullrequest.go5
-rw-r--r--modules/migrations/gitea_uploader.go17
2 files changed, 22 insertions, 0 deletions
diff --git a/modules/migrations/base/pullrequest.go b/modules/migrations/base/pullrequest.go
index 84b302d18f..b51a14e47c 100644
--- a/modules/migrations/base/pullrequest.go
+++ b/modules/migrations/base/pullrequest.go
@@ -41,6 +41,11 @@ func (p *PullRequest) IsForkPullRequest() bool {
return p.Head.RepoPath() != p.Base.RepoPath()
}
+// GetGitRefName returns pull request relative path to head
+func (p PullRequest) GetGitRefName() string {
+ return fmt.Sprintf("refs/pull/%d/head", p.Number)
+}
+
// PullRequestBranch represents a pull request branch
type PullRequestBranch struct {
CloneURL string `yaml:"clone_url"`
diff --git a/modules/migrations/gitea_uploader.go b/modules/migrations/gitea_uploader.go
index 3e8d396ed1..62e8924e54 100644
--- a/modules/migrations/gitea_uploader.go
+++ b/modules/migrations/gitea_uploader.go
@@ -690,6 +690,23 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
}
} else {
head = pr.Head.Ref
+ // Ensure the closed PR SHA still points to an existing ref
+ _, err = git.NewCommand("rev-list", "--quiet", "-1", pr.Head.SHA).RunInDir(g.repo.RepoPath())
+ if err != nil {
+ if pr.Head.SHA != "" {
+ // Git update-ref remove bad references with a relative path
+ log.Warn("Deprecated local head, removing : %v", pr.Head.SHA)
+ relPath := pr.GetGitRefName()
+ _, err = git.NewCommand("update-ref", "--no-deref", "-d", relPath).RunInDir(g.repo.RepoPath())
+ } else {
+ // The SHA is empty, remove the head file
+ log.Warn("Empty reference, removing : %v", pullHead)
+ err = os.Remove(filepath.Join(pullHead, "head"))
+ }
+ if err != nil {
+ log.Error("Cannot remove local head ref, %v", err)
+ }
+ }
}
if pr.Created.IsZero() {