diff options
author | 99rgosse <61579380+99rgosse@users.noreply.github.com> | 2021-12-23 09:32:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-23 16:32:29 +0800 |
commit | e0cf3d86c44fde99b49f12c7a1386cbf433a0207 (patch) | |
tree | b05b80758373312aa8c071b0a1317d5f03f6e7d7 /routers | |
parent | ba6efb105abd2a64b53491952a96a0a7af4b5ab9 (diff) | |
download | gitea-e0cf3d86c44fde99b49f12c7a1386cbf433a0207.tar.gz gitea-e0cf3d86c44fde99b49f12c7a1386cbf433a0207.zip |
Migrated Repository will show modifications when possible (#17191)
* Read patches to get history
Diffstat (limited to 'routers')
-rw-r--r-- | routers/web/repo/pull.go | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index bf80a7e5df..ba775d761b 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -320,8 +320,46 @@ func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) *git.C setMergeTarget(ctx, pull) ctx.Data["HasMerged"] = true + var baseCommit string + // Some migrated PR won't have any Base SHA and lose history, try to get one + if pull.MergeBase == "" { + var commitSHA, parentCommit string + // If there is a head or a patch file, and it is readable, grab info + commitSHA, err := ctx.Repo.GitRepo.ReadPullHead(pull.Index) + if err != nil { + // Head File does not exist, try the patch + commitSHA, err = ctx.Repo.GitRepo.ReadPatchCommit(pull.Index) + if err == nil { + // Recreate pull head in files for next time + if err := ctx.Repo.GitRepo.WritePullHead(pull.Index, commitSHA); err != nil { + log.Error("Could not write head file", err) + } + } else { + // There is no history available + log.Trace("No history file available for PR %d", pull.Index) + } + } + if commitSHA != "" { + // Get immediate parent of the first commit in the patch, grab history back + parentCommit, err = git.NewCommandContext(ctx, "rev-list", "-1", "--skip=1", commitSHA).RunInDir(ctx.Repo.GitRepo.Path) + if err == nil { + parentCommit = strings.TrimSpace(parentCommit) + } + // Special case on Git < 2.25 that doesn't fail on immediate empty history + if err != nil || parentCommit == "" { + log.Info("No known parent commit for PR %d, error: %v", pull.Index, err) + // bring at least partial history if it can work + parentCommit = commitSHA + } + } + baseCommit = parentCommit + } else { + // Keep an empty history or original commit + baseCommit = pull.MergeBase + } + compareInfo, err := ctx.Repo.GitRepo.GetCompareInfo(ctx.Repo.Repository.RepoPath(), - pull.MergeBase, pull.GetGitRefName(), true, false) + baseCommit, pull.GetGitRefName(), true, false) if err != nil { if strings.Contains(err.Error(), "fatal: Not a valid object name") || strings.Contains(err.Error(), "unknown revision or path not in the working tree") { ctx.Data["IsPullRequestBroken"] = true |