Browse Source

Add the well-known commits used to anchor detached comments to the archive ref

Omar Jarjur 11 months ago
parent
commit
dce6344ceb
2 changed files with 20 additions and 4 deletions
  1. 6 0
      repository/git.go
  2. 14 4
      review/review.go

+ 6 - 0
repository/git.go

@@ -394,6 +394,12 @@ func (repo *GitRepo) ArchiveRef(ref, archive string) error {
 	if err != nil {
 		archiveHash = ""
 	} else {
+		if isAncestor, err := repo.IsAncestor(refHash, archiveHash); err != nil {
+			return err
+		} else if isAncestor {
+			// The ref has already been archived, so we have nothing to do
+			return nil
+		}
 		commitTreeArgs = append(commitTreeArgs, "-p", archiveHash)
 	}
 	commitTreeArgs = append(commitTreeArgs, "-p", refHash, "-m", fmt.Sprintf("Archive %s", refHash), refDetails.Tree)

+ 14 - 4
review/review.go

@@ -794,7 +794,7 @@ func (r *Review) RebaseAndSign(archivePrevious bool) error {
 	return r.Repo.AppendNote(request.Ref, r.Revision, newNote)
 }
 
-func wellKnownCommitForPath(repo repository.Repo, path string) (string, error) {
+func wellKnownCommitForPath(repo repository.Repo, path string, archive bool) (string, error) {
 	commitDetails := &repository.CommitDetails{
 		Author:         "nobody",
 		AuthorEmail:    "nobody",
@@ -804,12 +804,22 @@ func wellKnownCommitForPath(repo repository.Repo, path string) (string, error) {
 		Time:           "100000000 +0000",
 		Summary:        path,
 	}
-	return repo.CreateCommitWithTree(commitDetails, emptyTree)
+	commitHash, err := repo.CreateCommitWithTree(commitDetails, emptyTree)
+	if err != nil {
+		return "", err
+	}
+	if !archive {
+		return commitHash, nil
+	}
+	if err := repo.ArchiveRef(commitHash, archiveRef); err != nil {
+		return "", err
+	}
+	return commitHash, nil
 }
 
 func AddDetachedComment(repo repository.Repo, c *comment.Comment) error {
 	path := c.Location.Path
-	wellKnownCommit, err := wellKnownCommitForPath(repo, path)
+	wellKnownCommit, err := wellKnownCommitForPath(repo, path, true)
 	if err != nil {
 		return fmt.Errorf("Failure finding the well-known commit for detached comments on %q: %v", path, err)
 	}
@@ -821,7 +831,7 @@ func AddDetachedComment(repo repository.Repo, c *comment.Comment) error {
 }
 
 func GetDetachedComments(repo repository.Repo, path string) ([]CommentThread, error) {
-	wellKnownCommit, err := wellKnownCommitForPath(repo, path)
+	wellKnownCommit, err := wellKnownCommitForPath(repo, path, false)
 	if err != nil {
 		return nil, fmt.Errorf("Failure finding the well-known commit for detached comments on %q: %v", path, err)
 	}