Skip to content

Commit c4e7b75

Browse files
committed
fix(doctor): preserve config file permissions and skip no-op writes
commentForkPointChange was writing .git/config with hardcoded 0644, which could loosen permissions if the file was e.g. 0600. Now preserves the existing file mode via os.Stat. Also skips the write entirely when no provenance comment was inserted.
1 parent 2bb8d3f commit c4e7b75

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

cmd/doctor.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ func commentForkPointChange(g *git.Git, branch, oldSHA string) {
250250
if err != nil {
251251
return
252252
}
253+
info, err := os.Stat(configPath)
254+
if err != nil {
255+
return
256+
}
253257
data, err := os.ReadFile(configPath)
254258
if err != nil {
255259
return
@@ -258,6 +262,7 @@ func commentForkPointChange(g *git.Git, branch, oldSHA string) {
258262
lines := strings.Split(string(data), "\n")
259263
sectionHeader := fmt.Sprintf("[branch %q]", branch)
260264
inSection := false
265+
modified := false
261266
var result []string
262267

263268
for _, line := range lines {
@@ -282,11 +287,15 @@ func commentForkPointChange(g *git.Git, branch, oldSHA string) {
282287
fmt.Sprintf("%s# doctor fix %s replaces previous value of:", indent, timestamp),
283288
fmt.Sprintf("%s# %s", indent, oldSHA),
284289
)
290+
modified = true
285291
}
286292

287293
result = append(result, line)
288294
}
289295

296+
if !modified {
297+
return
298+
}
290299
//nolint:errcheck // best-effort provenance
291-
_ = os.WriteFile(configPath, []byte(strings.Join(result, "\n")), 0644)
300+
_ = os.WriteFile(configPath, []byte(strings.Join(result, "\n")), info.Mode())
292301
}

0 commit comments

Comments
 (0)