From e8dffdec046a22b86ee7b6b63557c9bdc7727b15 Mon Sep 17 00:00:00 2001 From: Divya Jain Date: Thu, 15 May 2025 23:23:39 +0530 Subject: [PATCH 1/3] types: merge: add commit footers --- types/merge.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/types/merge.go b/types/merge.go index c9f25ec..107575a 100644 --- a/types/merge.go +++ b/types/merge.go @@ -13,10 +13,11 @@ type MergeCheckResponse struct { } type MergeRequest struct { - Patch string `json:"patch"` - AuthorName string `json:"authorName,omitempty"` - AuthorEmail string `json:"authorEmail,omitempty"` - CommitBody string `json:"commitBody,omitempty"` - CommitMessage string `json:"commitMessage,omitempty"` - Branch string `json:"branch"` + Patch string `json:"patch"` + AuthorName string `json:"authorName,omitempty"` + AuthorEmail string `json:"authorEmail,omitempty"` + CommitBody string `json:"commitBody,omitempty"` + CommitMessage string `json:"commitMessage,omitempty"` + CommitFooters map[string]string `json:"commitFooters,omitempty"` + Branch string `json:"branch"` } -- 2.47.2 From 5240cff6c23d3bdbfaa03486138e1cb9f1923980 Mon Sep 17 00:00:00 2001 From: Divya Jain Date: Thu, 15 May 2025 23:26:00 +0530 Subject: [PATCH 2/3] appview: state: add Pull-id and Merged-by commit footers --- appview/knotclient/signer.go | 3 ++- appview/state/pull.go | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/appview/knotclient/signer.go b/appview/knotclient/signer.go index 807d9fa..a878e2d 100644 --- a/appview/knotclient/signer.go +++ b/appview/knotclient/signer.go @@ -201,7 +201,7 @@ func (s *SignedClient) AddCollaborator(ownerDid, repoName, memberDid string) (*h func (s *SignedClient) Merge( patch []byte, - ownerDid, targetRepo, branch, commitMessage, commitBody, authorName, authorEmail string, + ownerDid, targetRepo, branch, commitMessage, commitBody, authorName, authorEmail string, footers map[string]string, ) (*http.Response, error) { const ( Method = "POST" @@ -215,6 +215,7 @@ func (s *SignedClient) Merge( AuthorName: authorName, AuthorEmail: authorEmail, Patch: string(patch), + CommitFooters: footers, } body, _ := json.Marshal(mr) diff --git a/appview/state/pull.go b/appview/state/pull.go index 6bb2ac2..173fa31 100644 --- a/appview/state/pull.go +++ b/appview/state/pull.go @@ -1534,6 +1534,13 @@ func (s *State) MergePull(w http.ResponseWriter, r *http.Request) { log.Printf("failed to get primary email: %s", err) } + actor := s.oauth.GetUser(r) // no need to check for nil as this is an authenticated request + + var footers = map[string]string{ + "Pull-id": string(pull.PullAt()), + "Merged-by": actor.Did, + } + ksClient, err := knotclient.NewSignedClient(f.Knot, secret, s.config.Core.Dev) if err != nil { log.Printf("failed to create signed client for %s: %s", f.Knot, err) @@ -1542,7 +1549,7 @@ func (s *State) MergePull(w http.ResponseWriter, r *http.Request) { } // Merge the pull request - resp, err := ksClient.Merge([]byte(pull.LatestPatch()), f.OwnerDid(), f.RepoName, pull.TargetBranch, pull.Title, pull.Body, ident.Handle.String(), email.Address) + resp, err := ksClient.Merge([]byte(pull.LatestPatch()), f.OwnerDid(), f.RepoName, pull.TargetBranch, pull.Title, pull.Body, ident.Handle.String(), email.Address, footers) if err != nil { log.Printf("failed to merge pull request: %s", err) s.pages.Notice(w, "pull-merge-error", "Failed to merge pull request. Try again later.") -- 2.47.2 From 0d7a3dd9090165fa6e4e1f20384c41f38e1a07fb Mon Sep 17 00:00:00 2001 From: Divya Jain Date: Thu, 15 May 2025 23:31:07 +0530 Subject: [PATCH 3/3] knotserver: git/merge: add footers in commit message on merge more changes --- knotserver/git/merge.go | 45 +++++++++++++++++++++++++++++++++++++++++ knotserver/routes.go | 1 + 2 files changed, 46 insertions(+) diff --git a/knotserver/git/merge.go b/knotserver/git/merge.go index 9057650..f620eae 100644 --- a/knotserver/git/merge.go +++ b/knotserver/git/merge.go @@ -32,6 +32,7 @@ type MergeOptions struct { AuthorName string AuthorEmail string FormatPatch bool + CommitFooters map[string]string } func (e ErrMerge) Error() string { @@ -88,16 +89,58 @@ func (g *GitRepo) applyPatch(tmpDir, patchFile string, checkOnly bool, opts *Mer var stderr bytes.Buffer var cmd *exec.Cmd + footers := "" + for k, v := range opts.CommitFooters { + footers += fmt.Sprintf("\n%s: %s", k, v) + } + if checkOnly { cmd = exec.Command("git", "-C", tmpDir, "apply", "--check", "-v", patchFile) } else { // if patch is a format-patch, apply using 'git am' if opts.FormatPatch { + // find head before the change + var beforeHash bytes.Buffer + hashCmd := exec.Command("git", "-C", tmpDir, "rev-parse", "HEAD") + hashCmd.Stdout = &beforeHash + if err := hashCmd.Run(); err != nil { + return fmt.Errorf("failed to get hash before patch: %w", err) + } + beforeCommit := strings.TrimSpace(beforeHash.String()) + amCmd := exec.Command("git", "-C", tmpDir, "am", patchFile) amCmd.Stderr = &stderr if err := amCmd.Run(); err != nil { return fmt.Errorf("patch application failed: %s", stderr.String()) } + + // command to ammend commit with required footers + amendCmdString := fmt.Sprintf("git commit --amend -m \"$(git log --format=%%B -n1)\" -m \"%s\"", footers) + + // create a temporary script file to hold the amend command (exec + // command can't contain newlines, so we need to create a script) + scriptFile, err := os.CreateTemp("", "rebase-exec-*.sh") + if err != nil { + return fmt.Errorf("failed to create temporary script for rebase: %w", err) + } + defer os.Remove(scriptFile.Name()) + + _, err = scriptFile.WriteString(amendCmdString) + defer scriptFile.Close() + if err != nil { + return fmt.Errorf("failed to write to temporary rebase script: %w", err) + } + + if err := scriptFile.Chmod(0755); err != nil { + return fmt.Errorf("failed to make temporary rebase script executable: %w", err) + } + + rebaseCmd := exec.Command("git", "-C", tmpDir, "rebase", "--exec", "sh "+scriptFile.Name(), beforeCommit) + rebaseCmd.Stderr = &stderr + if err := rebaseCmd.Run(); err != nil { + return fmt.Errorf("rebase failed: %s", stderr.String()) + } + return nil } @@ -139,6 +182,8 @@ func (g *GitRepo) applyPatch(tmpDir, patchFile string, checkOnly bool, opts *Mer commitArgs = append(commitArgs, "-m", opts.CommitBody) } + commitArgs = append(commitArgs, "-m", footers) + cmd = exec.Command("git", commitArgs...) } else { // If no commit message specified, use git-am which automatically creates a commit diff --git a/knotserver/routes.go b/knotserver/routes.go index 3bc24a2..bbe5406 100644 --- a/knotserver/routes.go +++ b/knotserver/routes.go @@ -732,6 +732,7 @@ func (h *Handle) Merge(w http.ResponseWriter, r *http.Request) { AuthorEmail: data.AuthorEmail, CommitBody: data.CommitBody, CommitMessage: data.CommitMessage, + CommitFooters: data.CommitFooters, } patch := data.Patch -- 2.47.2