Honor ctx cancellation in object translation · Entire
Honor ctx cancellation in object translation
45dfdb3→main·
nodo·1mo ago·2 files·+16 added/-5 removed
translate() is the single recursive entry point invoked from the tree-entry, commit-parent, tag-target, and message-ref loops. Check ctx.Err() at the top so Ctrl-C during a kernel-scale conversion returns promptly instead of running the whole DFS to completion. ctx is plumbed through newTranslator and stored on the translator (signatures of the many translateX helpers stay untouched).
Sessions
Transcript data is unavailable for this checkpoint.
Changes
2
cmd/git-sync/internal/sha256convert
- Msha256convert.go+13/-2
Msha256convert_test.go+3/-3
293 unmodified lines
294
295
296
297
297
298
299
300
377 unmodified lines
678
679
680
681
682
683
684
685
686
687
44 unmodified lines
732
733
734
731
735
736
737
738
6 unmodified lines
745
746
747
748
749
750
751
101 unmodified lines
853
854
855
856
857
858
859
860
861
862
863
864
293 unmodified lines
return Result{}, fmt.Errorf("init SHA256 target at %s: %w", req.TargetDir, err)
}
tr, err := newTranslator(srcRepo.Storer, dstRepo.Storer, req.TargetDir, !req.SkipMessageRewrite, reachable)
tr, err := newTranslator(ctx, srcRepo.Storer, dstRepo.Storer, req.TargetDir, !req.SkipMessageRewrite, reachable)
if err != nil {
return Result{}, err
}
377 unmodified lines
// plumbing/format/objfile/writer.go:68), which would store every SHA256
// object at a SHA1-derived path.
type translator struct {
// ctx is checked at the top of every translate() call so a Ctrl-C
// during a million-object conversion is responsive. It is the same
// context passed to Run() and is not stored to outlive its caller.
ctx context.Context //nolint:containedctx // translate() is recursive and not directly called by Run; threading ctx through every signature is noisier than a single field used for cancellation only.
src *filesystem.Storage
dst *filesystem.Storage
objectsDir string
44 unmodified lines
}
}
func newTranslator(src, dst storer.Storer, targetDir string, rewriteMessages bool, reachable map[plumbing.Hash]plumbing.ObjectType) (*translator, error) {
func newTranslator(ctx context.Context, src, dst storer.Storer, targetDir string, rewriteMessages bool, reachable map[plumbing.Hash]plumbing.ObjectType) (*translator, error) {
srcFS, ok := src.(*filesystem.Storage)
if !ok {
return nil, fmt.Errorf("source storage is not filesystem-backed (%T)", src)
}
6 unmodified lines
reachable = make(map[plumbing.Hash]plumbing.ObjectType)
}
return &translator{
ctx: ctx,
src: srcFS,
dst: dstFS,
objectsDir: filepath.Join(targetDir, "objects"),
101 unmodified lines
}
func (t *translator) translate(sha1 plumbing.Hash) (plumbing.Hash, error) {
// Cheap per-object cancellation check so Ctrl-C during a long
// conversion (kernel-scale: ~10M objects) returns promptly rather
// than running the whole DFS to completion.
if err := t.ctx.Err(); err != nil {
return plumbing.ZeroHash, fmt.Errorf("translate %s: %w", sha1, err)
}
if newH, ok := t.mapping[sha1]; ok {
return newH, nil
}
Mcmd/git-sync/internal/sha256convert/sha256convert.go+13/-2
85 unmodified lines
86
87
88
89
89
90
91
92
108 unmodified lines
201
202
203
204
204
205
206
207
368 unmodified lines
576
577
578
579
579
580
581
582
85 unmodified lines
if err != nil {
t.Fatalf("discoverReachable: %v", err)
}
tr, err := newTranslator(srcRepo.Storer, dstRepo.Storer, dstDir, false, reachable)
tr, err := newTranslator(t.Context(), srcRepo.Storer, dstRepo.Storer, dstDir, false, reachable)
if err != nil {
t.Fatalf("newTranslator: %v", err)
}
108 unmodified lines
if err != nil {
t.Fatalf("discoverReachable: %v", err)
}
tr, err := newTranslator(srcRepo.Storer, dstRepo.Storer, dstDir, true, reachable)
tr, err := newTranslator(t.Context(), srcRepo.Storer, dstRepo.Storer, dstDir, true, reachable)
if err != nil {
t.Fatalf("newTranslator: %v", err)
}
368 unmodified lines
func mustTranslator(t *testing.T, src, dst gogitstorer.Storer, dir string, rewrite bool, reachable map[plumbing.Hash]plumbing.ObjectType) *translator {
t.Helper()
tr, err := newTranslator(src, dst, dir, rewrite, reachable)
tr, err := newTranslator(t.Context(), src, dst, dir, rewrite, reachable)
if err != nil {
t.Fatalf("newTranslator: %v", err)
}