Skip to content

Commit 88fa9f3

Browse files
99rgossezeripathlafriks6543
authored
Remove heads pointing to missing old refs (#17076)
Co-authored-by: zeripath <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent b614715 commit 88fa9f3

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

modules/migrations/base/pullrequest.go

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ func (p *PullRequest) IsForkPullRequest() bool {
4141
return p.Head.RepoPath() != p.Base.RepoPath()
4242
}
4343

44+
// GetGitRefName returns pull request relative path to head
45+
func (p PullRequest) GetGitRefName() string {
46+
return fmt.Sprintf("refs/pull/%d/head", p.Number)
47+
}
48+
4449
// PullRequestBranch represents a pull request branch
4550
type PullRequestBranch struct {
4651
CloneURL string `yaml:"clone_url"`

modules/migrations/gitea_uploader.go

+17
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,23 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
690690
}
691691
} else {
692692
head = pr.Head.Ref
693+
// Ensure the closed PR SHA still points to an existing ref
694+
_, err = git.NewCommand("rev-list", "--quiet", "-1", pr.Head.SHA).RunInDir(g.repo.RepoPath())
695+
if err != nil {
696+
if pr.Head.SHA != "" {
697+
// Git update-ref remove bad references with a relative path
698+
log.Warn("Deprecated local head, removing : %v", pr.Head.SHA)
699+
relPath := pr.GetGitRefName()
700+
_, err = git.NewCommand("update-ref", "--no-deref", "-d", relPath).RunInDir(g.repo.RepoPath())
701+
} else {
702+
// The SHA is empty, remove the head file
703+
log.Warn("Empty reference, removing : %v", pullHead)
704+
err = os.Remove(filepath.Join(pullHead, "head"))
705+
}
706+
if err != nil {
707+
log.Error("Cannot remove local head ref, %v", err)
708+
}
709+
}
693710
}
694711

695712
if pr.Created.IsZero() {

0 commit comments

Comments
 (0)