Skip to content

Commit fd34b43

Browse files
committed
backend: handle alternates properly
Previously, if our repository had an alternates file pointing to another repository, we would not honor it, leading to us reporting a missing object. Instead, handle these alternate repositories as simply other storage backends from which we can read. We continue to write any new objects into our repository alone.
1 parent 670a1fb commit fd34b43

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

backend.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package gitobj
22

33
import (
4+
"bufio"
45
"io"
6+
"os"
7+
"path"
58

69
"github.com/git-lfs/gitobj/pack"
710
"github.com/git-lfs/gitobj/storage"
@@ -15,12 +18,42 @@ func NewFilesystemBackend(root, tmp string) (storage.Backend, error) {
1518
return nil, err
1619
}
1720

21+
storage, err := findAllBackends(fsobj, packs, root)
22+
if err != nil {
23+
return nil, err
24+
}
25+
1826
return &filesystemBackend{
1927
fs: fsobj,
20-
backends: []storage.Storage{fsobj, packs},
28+
backends: storage,
2129
}, nil
2230
}
2331

32+
func findAllBackends(mainLoose *fileStorer, mainPacked *pack.Storage, root string) ([]storage.Storage, error) {
33+
storage := make([]storage.Storage, 2)
34+
storage[0] = mainLoose
35+
storage[1] = mainPacked
36+
f, err := os.Open(path.Join(root, "info", "alternates"))
37+
if err != nil {
38+
// No alternates file, no problem.
39+
if err != os.ErrNotExist {
40+
return storage, nil
41+
}
42+
return nil, err
43+
}
44+
45+
scanner := bufio.NewScanner(f)
46+
for scanner.Scan() {
47+
storage = append(storage, newFileStorer(scanner.Text(), ""))
48+
pack, err := pack.NewStorage(scanner.Text())
49+
if err != nil {
50+
return nil, err
51+
}
52+
storage = append(storage, pack)
53+
}
54+
return storage, nil
55+
}
56+
2457
// NewMemoryBackend initializes a new memory-based backend.
2558
//
2659
// A value of "nil" is acceptable and indicates that no entries should be added

0 commit comments

Comments
 (0)