fix(client): merge concurrent appends at end of file

This commit is contained in:
hh committed 2026-10-02 23:32:44 +00:00
1 parent 30cd772f08
commit 331c1f5b6f
2 files changed
+58 -30

No files matched your search

+16 -1
View File
@@ -254,13 +254,28 @@ func (c *Client) merge(ctx context.Context, local, base, remote protocol.File) (
cmd := exec.CommandContext(ctx, "git", append([]string{"merge-file", "-p"}, names...)...)
cmd.Dir = dir
merged, err := cmd.Output()
if err != nil || len(merged) > protocol.MaxBlobSize {
clean := err == nil
if !clean {
merged, clean = appended(versions[0], versions[1], versions[2])
}
if !clean || len(merged) > protocol.MaxBlobSize {
return protocol.File{}, false
}
hash, err := c.cacheBlob(merged)
return protocol.File{Hash: hash, Time: max(local.Time, remote.Time)}, err == nil
}
func appended(local, base, remote []byte) ([]byte, bool) {
if !bytes.HasPrefix(local, base) || !bytes.HasPrefix(remote, base) {
return nil, false
}
tail, glue := local[len(base):], []byte{}
if !bytes.HasSuffix(remote, []byte("\n")) && !bytes.HasPrefix(tail, []byte("\n")) {
glue = []byte("\n")
}
return slices.Concat(remote, glue, tail), true
}
func isText(data []byte) bool {
return len(data) <= protocol.MaxBlobSize && utf8.Valid(data) && bytes.IndexByte(data, 0) == -1
}