Skip to content

Commit

Permalink
Remove heads pointing to missing old refs (go-gitea#17076)
Browse files Browse the repository at this point in the history
Co-authored-by: zeripath <[email protected]>
Co-authored-by: Lauris BH <[email protected]>
Co-authored-by: 6543 <[email protected]>
  • Loading branch information
4 people authored and Stelios Malathouras committed Oct 15, 2021
1 parent b1db066 commit 98d9d00
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions modules/migrations/base/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
17 changes: 17 additions & 0 deletions modules/migrations/gitea_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 98d9d00

Please sign in to comment.