Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

knotserver: git/merge: separate `applyPatch` and `checkPatch`

- we don't need `MergeOptions` for checking a patch

Signed-off-by: Seongmin Lee <boltlessengineer@proton.me>

authored by

Seongmin Lee and committed by
Tangled
3c066500 cdac8683

+72 -71
+72 -71
knotserver/git/merge.go
··· 12 12 "github.com/dgraph-io/ristretto" 13 13 "github.com/go-git/go-git/v5" 14 14 "github.com/go-git/go-git/v5/plumbing" 15 - "tangled.sh/tangled.sh/core/patchutil" 16 15 ) 17 16 18 17 type MergeCheckCache struct { ··· 142 143 return tmpDir, nil 143 144 } 144 145 145 - func (g *GitRepo) applyPatch(tmpDir, patchFile string, checkOnly bool, opts *MergeOptions) error { 146 + func (g *GitRepo) checkPatch(tmpDir, patchFile string) error { 147 + var stderr bytes.Buffer 148 + 149 + cmd := exec.Command("git", "-C", tmpDir, "apply", "--check", "-v", patchFile) 150 + cmd.Stderr = &stderr 151 + 152 + if err := cmd.Run(); err != nil { 153 + conflicts := parseGitApplyErrors(stderr.String()) 154 + return &ErrMerge{ 155 + Message: "patch cannot be applied cleanly", 156 + Conflicts: conflicts, 157 + HasConflict: len(conflicts) > 0, 158 + OtherError: err, 159 + } 160 + } 161 + return nil 162 + } 163 + 164 + func (g *GitRepo) applyPatch(tmpDir, patchFile string, opts *MergeOptions) error { 146 165 var stderr bytes.Buffer 147 166 var cmd *exec.Cmd 148 167 149 - if checkOnly { 150 - cmd = exec.Command("git", "-C", tmpDir, "apply", "--check", "-v", patchFile) 168 + // if patch is a format-patch, apply using 'git am' 169 + if opts.FormatPatch { 170 + amCmd := exec.Command("git", "-C", tmpDir, "am", patchFile) 171 + amCmd.Stderr = &stderr 172 + if err := amCmd.Run(); err != nil { 173 + return fmt.Errorf("patch application failed: %s", stderr.String()) 174 + } 175 + return nil 176 + } 177 + 178 + // else, apply using 'git apply' and commit it manually 179 + exec.Command("git", "-C", tmpDir, "config", "advice.mergeConflict", "false").Run() 180 + if opts != nil { 181 + applyCmd := exec.Command("git", "-C", tmpDir, "apply", patchFile) 182 + applyCmd.Stderr = &stderr 183 + if err := applyCmd.Run(); err != nil { 184 + return fmt.Errorf("patch application failed: %s", stderr.String()) 185 + } 186 + 187 + stageCmd := exec.Command("git", "-C", tmpDir, "add", ".") 188 + if err := stageCmd.Run(); err != nil { 189 + return fmt.Errorf("failed to stage changes: %w", err) 190 + } 191 + 192 + commitArgs := []string{"-C", tmpDir, "commit"} 193 + 194 + // Set author if provided 195 + authorName := opts.AuthorName 196 + authorEmail := opts.AuthorEmail 197 + 198 + if authorEmail == "" { 199 + authorEmail = "noreply@tangled.sh" 200 + } 201 + 202 + if authorName == "" { 203 + authorName = "Tangled" 204 + } 205 + 206 + if authorName != "" { 207 + commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail)) 208 + } 209 + 210 + commitArgs = append(commitArgs, "-m", opts.CommitMessage) 211 + 212 + if opts.CommitBody != "" { 213 + commitArgs = append(commitArgs, "-m", opts.CommitBody) 214 + } 215 + 216 + cmd = exec.Command("git", commitArgs...) 151 217 } else { 152 - // if patch is a format-patch, apply using 'git am' 153 - if opts.FormatPatch { 154 - amCmd := exec.Command("git", "-C", tmpDir, "am", patchFile) 155 - amCmd.Stderr = &stderr 156 - if err := amCmd.Run(); err != nil { 157 - return fmt.Errorf("patch application failed: %s", stderr.String()) 158 - } 159 - return nil 160 - } 161 - 162 - // else, apply using 'git apply' and commit it manually 163 - exec.Command("git", "-C", tmpDir, "config", "advice.mergeConflict", "false").Run() 164 - if opts != nil { 165 - applyCmd := exec.Command("git", "-C", tmpDir, "apply", patchFile) 166 - applyCmd.Stderr = &stderr 167 - if err := applyCmd.Run(); err != nil { 168 - return fmt.Errorf("patch application failed: %s", stderr.String()) 169 - } 170 - 171 - stageCmd := exec.Command("git", "-C", tmpDir, "add", ".") 172 - if err := stageCmd.Run(); err != nil { 173 - return fmt.Errorf("failed to stage changes: %w", err) 174 - } 175 - 176 - commitArgs := []string{"-C", tmpDir, "commit"} 177 - 178 - // Set author if provided 179 - authorName := opts.AuthorName 180 - authorEmail := opts.AuthorEmail 181 - 182 - if authorEmail == "" { 183 - authorEmail = "noreply@tangled.sh" 184 - } 185 - 186 - if authorName == "" { 187 - authorName = "Tangled" 188 - } 189 - 190 - if authorName != "" { 191 - commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", authorName, authorEmail)) 192 - } 193 - 194 - commitArgs = append(commitArgs, "-m", opts.CommitMessage) 195 - 196 - if opts.CommitBody != "" { 197 - commitArgs = append(commitArgs, "-m", opts.CommitBody) 198 - } 199 - 200 - cmd = exec.Command("git", commitArgs...) 201 - } else { 202 - // If no commit message specified, use git-am which automatically creates a commit 203 - cmd = exec.Command("git", "-C", tmpDir, "am", patchFile) 204 - } 218 + // If no commit message specified, use git-am which automatically creates a commit 219 + cmd = exec.Command("git", "-C", tmpDir, "am", patchFile) 205 220 } 206 221 207 222 cmd.Stderr = &stderr 208 223 209 224 if err := cmd.Run(); err != nil { 210 - if checkOnly { 211 - conflicts := parseGitApplyErrors(stderr.String()) 212 - return &ErrMerge{ 213 - Message: "patch cannot be applied cleanly", 214 - Conflicts: conflicts, 215 - HasConflict: len(conflicts) > 0, 216 - OtherError: err, 217 - } 218 - } 219 225 return fmt.Errorf("patch application failed: %s", stderr.String()) 220 226 } 221 227 ··· 231 227 if val, ok := mergeCheckCache.Get(g, patchData, targetBranch); ok { 232 228 return val 233 229 } 234 - 235 - var opts MergeOptions 236 - opts.FormatPatch = patchutil.IsFormatPatch(string(patchData)) 237 230 238 231 patchFile, err := g.createTempFileWithPatch(patchData) 239 232 if err != nil { ··· 250 249 } 251 250 defer os.RemoveAll(tmpDir) 252 251 253 - result := g.applyPatch(tmpDir, patchFile, true, &opts) 252 + result := g.checkPatch(tmpDir, patchFile) 254 253 mergeCheckCache.Set(g, patchData, targetBranch, result) 255 254 return result 256 255 } ··· 278 277 } 279 278 defer os.RemoveAll(tmpDir) 280 279 281 - if err := g.applyPatch(tmpDir, patchFile, false, opts); err != nil { 280 + if err := g.applyPatch(tmpDir, patchFile, opts); err != nil { 282 281 return err 283 282 } 284 283