ソースを参照

Support comparing tags in addition to branches (#6493)

Co-authored-by: Joe Chen <jc@unknwon.io>
Co-authored-by: Claude <noreply@anthropic.com>
Rajat Jain 1 週間 前
コミット
9f1499f3ab
3 ファイル変更30 行追加28 行削除
  1. 2 1
      AGENTS.md
  2. 1 0
      CHANGELOG.md
  3. 27 27
      internal/route/repo/pull.go

+ 2 - 1
AGENTS.md

@@ -28,4 +28,5 @@ This applies to all texts, including but not limited to UI, documentation, code
 ## Source code control
 ## Source code control
 
 
 - When pushing changes to a pull request from a fork, use SSH address and do not add remote.
 - When pushing changes to a pull request from a fork, use SSH address and do not add remote.
-- Never automatically executes commands that touches Git history even if the session does not require approvals, including but not limited to `rebase`, `commit`, `push`, `pull`. One-time execution is only allowed when being explicitly asked to do so.
+- Never automatically executes commands that touches Git history even if the session does not require approvals, including but not limited to `rebase`, `commit`, `push`, `pull`, `reset`, `amend`. Exceptions are only allowed case-by-case.
+- Do not amend commits unless being explicitly asked to do so.

+ 1 - 0
CHANGELOG.md

@@ -6,6 +6,7 @@ All notable changes to Gogs are documented in this file.
 
 
 ### Added
 ### Added
 
 
+- Support comparing tags in addition to branches. [#6141](https://github.com/gogs/gogs/issues/6141)
 - Show file name in browser tab title when viewing files. [#5896](https://github.com/gogs/gogs/pull/5896)
 - Show file name in browser tab title when viewing files. [#5896](https://github.com/gogs/gogs/pull/5896)
 - Support using TLS for Redis session provider using `[session] PROVIDER_CONFIG = ...,tls=true`. [#7860](https://github.com/gogs/gogs/pull/7860)
 - Support using TLS for Redis session provider using `[session] PROVIDER_CONFIG = ...,tls=true`. [#7860](https://github.com/gogs/gogs/pull/7860)
 - Support expanading values in `app.ini` from environment variables, e.g. `[database] PASSWORD = ${DATABASE_PASSWORD}`. [#8057](https://github.com/gogs/gogs/pull/8057)
 - Support expanading values in `app.ini` from environment variables, e.g. `[database] PASSWORD = ${DATABASE_PASSWORD}`. [#8057](https://github.com/gogs/gogs/pull/8057)

+ 27 - 27
internal/route/repo/pull.go

@@ -433,23 +433,23 @@ func MergePullRequest(c *context.Context) {
 func ParseCompareInfo(c *context.Context) (*database.User, *database.Repository, *git.Repository, *gitutil.PullRequestMeta, string, string) {
 func ParseCompareInfo(c *context.Context) (*database.User, *database.Repository, *git.Repository, *gitutil.PullRequestMeta, string, string) {
 	baseRepo := c.Repo.Repository
 	baseRepo := c.Repo.Repository
 
 
-	// Get compared branches information
-	// format: <base branch>...[<head repo>:]<head branch>
+	// Get compared refs information
+	// format: <base ref>...[<head repo>:]<head ref>
 	// base<-head: master...head:feature
 	// base<-head: master...head:feature
 	// same repo: master...feature
 	// same repo: master...feature
 	infos := strings.Split(c.Params("*"), "...")
 	infos := strings.Split(c.Params("*"), "...")
 	if len(infos) != 2 {
 	if len(infos) != 2 {
-		log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
+		log.Trace("ParseCompareInfo[%d]: not enough compared refs information %s", baseRepo.ID, infos)
 		c.NotFound()
 		c.NotFound()
 		return nil, nil, nil, nil, "", ""
 		return nil, nil, nil, nil, "", ""
 	}
 	}
 
 
-	baseBranch := infos[0]
-	c.Data["BaseBranch"] = baseBranch
+	baseRef := infos[0]
+	c.Data["BaseBranch"] = baseRef
 
 
 	var (
 	var (
 		headUser   *database.User
 		headUser   *database.User
-		headBranch string
+		headRef    string
 		isSameRepo bool
 		isSameRepo bool
 		err        error
 		err        error
 	)
 	)
@@ -459,7 +459,7 @@ func ParseCompareInfo(c *context.Context) (*database.User, *database.Repository,
 	if len(headInfos) == 1 {
 	if len(headInfos) == 1 {
 		isSameRepo = true
 		isSameRepo = true
 		headUser = c.Repo.Owner
 		headUser = c.Repo.Owner
-		headBranch = headInfos[0]
+		headRef = headInfos[0]
 
 
 	} else if len(headInfos) == 2 {
 	} else if len(headInfos) == 2 {
 		headUser, err = database.Handle.Users().GetByUsername(c.Req.Context(), headInfos[0])
 		headUser, err = database.Handle.Users().GetByUsername(c.Req.Context(), headInfos[0])
@@ -467,7 +467,7 @@ func ParseCompareInfo(c *context.Context) (*database.User, *database.Repository,
 			c.NotFoundOrError(err, "get user by name")
 			c.NotFoundOrError(err, "get user by name")
 			return nil, nil, nil, nil, "", ""
 			return nil, nil, nil, nil, "", ""
 		}
 		}
-		headBranch = headInfos[1]
+		headRef = headInfos[1]
 		isSameRepo = headUser.ID == baseRepo.OwnerID
 		isSameRepo = headUser.ID == baseRepo.OwnerID
 
 
 	} else {
 	} else {
@@ -475,11 +475,11 @@ func ParseCompareInfo(c *context.Context) (*database.User, *database.Repository,
 		return nil, nil, nil, nil, "", ""
 		return nil, nil, nil, nil, "", ""
 	}
 	}
 	c.Data["HeadUser"] = headUser
 	c.Data["HeadUser"] = headUser
-	c.Data["HeadBranch"] = headBranch
+	c.Data["HeadBranch"] = headRef
 	c.Repo.PullRequest.SameRepo = isSameRepo
 	c.Repo.PullRequest.SameRepo = isSameRepo
 
 
-	// Check if base branch is valid.
-	if !c.Repo.GitRepo.HasBranch(baseBranch) {
+	// Check if base ref is valid.
+	if _, err := c.Repo.GitRepo.RevParse(baseRef); err != nil {
 		c.NotFound()
 		c.NotFound()
 		return nil, nil, nil, nil, "", ""
 		return nil, nil, nil, nil, "", ""
 	}
 	}
@@ -528,8 +528,8 @@ func ParseCompareInfo(c *context.Context) (*database.User, *database.Repository,
 		return nil, nil, nil, nil, "", ""
 		return nil, nil, nil, nil, "", ""
 	}
 	}
 
 
-	// Check if head branch is valid.
-	if !headGitRepo.HasBranch(headBranch) {
+	// Check if head ref is valid.
+	if _, err := headGitRepo.RevParse(headRef); err != nil {
 		c.NotFound()
 		c.NotFound()
 		return nil, nil, nil, nil, "", ""
 		return nil, nil, nil, nil, "", ""
 	}
 	}
@@ -542,7 +542,7 @@ func ParseCompareInfo(c *context.Context) (*database.User, *database.Repository,
 	c.Data["HeadBranches"] = headBranches
 	c.Data["HeadBranches"] = headBranches
 
 
 	baseRepoPath := database.RepoPath(baseRepo.Owner.Name, baseRepo.Name)
 	baseRepoPath := database.RepoPath(baseRepo.Owner.Name, baseRepo.Name)
-	meta, err := gitutil.Module.PullRequestMeta(headGitRepo.Path(), baseRepoPath, headBranch, baseBranch)
+	meta, err := gitutil.Module.PullRequestMeta(headGitRepo.Path(), baseRepoPath, headRef, baseRef)
 	if err != nil {
 	if err != nil {
 		if gitutil.IsErrNoMergeBase(err) {
 		if gitutil.IsErrNoMergeBase(err) {
 			c.Data["IsNoMergeBase"] = true
 			c.Data["IsNoMergeBase"] = true
@@ -554,7 +554,7 @@ func ParseCompareInfo(c *context.Context) (*database.User, *database.Repository,
 	}
 	}
 	c.Data["BeforeCommitID"] = meta.MergeBase
 	c.Data["BeforeCommitID"] = meta.MergeBase
 
 
-	return headUser, headRepo, headGitRepo, meta, baseBranch, headBranch
+	return headUser, headRepo, headGitRepo, meta, baseRef, headRef
 }
 }
 
 
 func PrepareCompareDiff(
 func PrepareCompareDiff(
@@ -563,7 +563,7 @@ func PrepareCompareDiff(
 	headRepo *database.Repository,
 	headRepo *database.Repository,
 	headGitRepo *git.Repository,
 	headGitRepo *git.Repository,
 	meta *gitutil.PullRequestMeta,
 	meta *gitutil.PullRequestMeta,
-	headBranch string,
+	headRef string,
 ) bool {
 ) bool {
 	var (
 	var (
 		repo = c.Repo.Repository
 		repo = c.Repo.Repository
@@ -573,9 +573,9 @@ func PrepareCompareDiff(
 	// Get diff information.
 	// Get diff information.
 	c.Data["CommitRepoLink"] = headRepo.Link()
 	c.Data["CommitRepoLink"] = headRepo.Link()
 
 
-	headCommitID, err := headGitRepo.BranchCommitID(headBranch)
+	headCommitID, err := headGitRepo.RevParse(headRef)
 	if err != nil {
 	if err != nil {
-		c.Error(err, "get head branch commit ID")
+		c.Error(err, "get head commit ID")
 		return false
 		return false
 	}
 	}
 	c.Data["AfterCommitID"] = headCommitID
 	c.Data["AfterCommitID"] = headCommitID
@@ -625,12 +625,12 @@ func CompareAndPullRequest(c *context.Context) {
 	setTemplateIfExists(c, PullRequestTemplateKey, PullRequestTemplateCandidates)
 	setTemplateIfExists(c, PullRequestTemplateKey, PullRequestTemplateCandidates)
 	renderAttachmentSettings(c)
 	renderAttachmentSettings(c)
 
 
-	headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(c)
+	headUser, headRepo, headGitRepo, prInfo, baseRef, headRef := ParseCompareInfo(c)
 	if c.Written() {
 	if c.Written() {
 		return
 		return
 	}
 	}
 
 
-	pr, err := database.GetUnmergedPullRequest(headRepo.ID, c.Repo.Repository.ID, headBranch, baseBranch)
+	pr, err := database.GetUnmergedPullRequest(headRepo.ID, c.Repo.Repository.ID, headRef, baseRef)
 	if err != nil {
 	if err != nil {
 		if !database.IsErrPullRequestNotExist(err) {
 		if !database.IsErrPullRequestNotExist(err) {
 			c.Error(err, "get unmerged pull request")
 			c.Error(err, "get unmerged pull request")
@@ -643,7 +643,7 @@ func CompareAndPullRequest(c *context.Context) {
 		return
 		return
 	}
 	}
 
 
-	nothingToCompare := PrepareCompareDiff(c, headUser, headRepo, headGitRepo, prInfo, headBranch)
+	nothingToCompare := PrepareCompareDiff(c, headUser, headRepo, headGitRepo, prInfo, headRef)
 	if c.Written() {
 	if c.Written() {
 		return
 		return
 	}
 	}
@@ -666,7 +666,7 @@ func CompareAndPullRequest(c *context.Context) {
 
 
 	if c.Data[PullRequestTitleTemplateKey] != nil {
 	if c.Data[PullRequestTitleTemplateKey] != nil {
 		customTitle := c.Data[PullRequestTitleTemplateKey].(string)
 		customTitle := c.Data[PullRequestTitleTemplateKey].(string)
-		r := strings.NewReplacer("{{headBranch}}", headBranch, "{{baseBranch}}", baseBranch)
+		r := strings.NewReplacer("{{headBranch}}", headRef, "{{baseBranch}}", baseRef)
 		c.Data["title"] = r.Replace(customTitle)
 		c.Data["title"] = r.Replace(customTitle)
 	}
 	}
 
 
@@ -685,7 +685,7 @@ func CompareAndPullRequestPost(c *context.Context, f form.NewIssue) {
 		attachments []string
 		attachments []string
 	)
 	)
 
 
-	headUser, headRepo, headGitRepo, meta, baseBranch, headBranch := ParseCompareInfo(c)
+	headUser, headRepo, headGitRepo, meta, baseRef, headRef := ParseCompareInfo(c)
 	if c.Written() {
 	if c.Written() {
 		return
 		return
 	}
 	}
@@ -704,7 +704,7 @@ func CompareAndPullRequestPost(c *context.Context, f form.NewIssue) {
 
 
 		// This stage is already stop creating new pull request, so it does not matter if it has
 		// This stage is already stop creating new pull request, so it does not matter if it has
 		// something to compare or not.
 		// something to compare or not.
-		PrepareCompareDiff(c, headUser, headRepo, headGitRepo, meta, headBranch)
+		PrepareCompareDiff(c, headUser, headRepo, headGitRepo, meta, headRef)
 		if c.Written() {
 		if c.Written() {
 			return
 			return
 		}
 		}
@@ -713,7 +713,7 @@ func CompareAndPullRequestPost(c *context.Context, f form.NewIssue) {
 		return
 		return
 	}
 	}
 
 
-	patch, err := headGitRepo.DiffBinary(meta.MergeBase, headBranch)
+	patch, err := headGitRepo.DiffBinary(meta.MergeBase, headRef)
 	if err != nil {
 	if err != nil {
 		c.Error(err, "get patch")
 		c.Error(err, "get patch")
 		return
 		return
@@ -734,8 +734,8 @@ func CompareAndPullRequestPost(c *context.Context, f form.NewIssue) {
 		HeadRepoID:   headRepo.ID,
 		HeadRepoID:   headRepo.ID,
 		BaseRepoID:   repo.ID,
 		BaseRepoID:   repo.ID,
 		HeadUserName: headUser.Name,
 		HeadUserName: headUser.Name,
-		HeadBranch:   headBranch,
-		BaseBranch:   baseBranch,
+		HeadBranch:   headRef,
+		BaseBranch:   baseRef,
 		HeadRepo:     headRepo,
 		HeadRepo:     headRepo,
 		BaseRepo:     repo,
 		BaseRepo:     repo,
 		MergeBase:    meta.MergeBase,
 		MergeBase:    meta.MergeBase,