| 1 |
package v2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"io" |
| 6 |
"path" |
| 7 |
|
| 8 |
"github.com/go-git/go-billy/v5" |
| 9 |
"github.com/go-git/go-git/v5/plumbing" |
| 10 |
) |
| 11 |
|
| 12 |
// OpenChainFile reads a commit chain file and returns a slice of the hashes within it |
| 13 |
// |
| 14 |
// Commit-Graph chains are described at https://git-scm.com/docs/commit-graph |
| 15 |
// and are new line separated list of graph file hashes, oldest to newest. |
| 16 |
// |
| 17 |
// This function simply reads the file and returns the hashes as a slice. |
| 18 |
func OpenChainFile(r io.Reader) ([]string, error) { |
| 19 |
if r == nil { |
| 20 |
return nil, io.ErrUnexpectedEOF |
| 21 |
} |
| 22 |
bufRd := bufio.NewReader(r) |
| 23 |
chain := make([]string, 0, 8) |
| 24 |
for { |
| 25 |
line, err := bufRd.ReadSlice('\n') |
| 26 |
if err != nil { |
| 27 |
if err == io.EOF { |
| 28 |
break |
| 29 |
} |
| 30 |
return nil, err |
| 31 |
} |
| 32 |
|
| 33 |
hashStr := string(line[:len(line)-1]) |
| 34 |
if !plumbing.IsHash(hashStr) { |
| 35 |
return nil, ErrMalformedCommitGraphFile |
| 36 |
} |
| 37 |
chain = append(chain, hashStr) |
| 38 |
} |
| 39 |
return chain, nil |
| 40 |
} |
| 41 |
|
| 42 |
// OpenChainOrFileIndex expects a billy.Filesystem representing a .git directory. |
| 43 |
// It will first attempt to read a commit-graph index file, before trying to read a |
| 44 |
// commit-graph chain file and its index files. If neither are present, an error is returned. |
| 45 |
// Otherwise an Index will be returned. |
| 46 |
// |
| 47 |
// See: https://git-scm.com/docs/commit-graph |
| 48 |
func OpenChainOrFileIndex(fs billy.Filesystem) (Index, error) { |
| 49 |
file, err := fs.Open(path.Join("objects", "info", "commit-graph")) |
| 50 |
if err != nil { |
| 51 |
// try to open a chain file |
| 52 |
return OpenChainIndex(fs) |
| 53 |
} |
| 54 |
|
| 55 |
index, err := OpenFileIndex(file) |
| 56 |
if err != nil { |
| 57 |
// Ignore any file closing errors and return the error from OpenFileIndex instead |
| 58 |
_ = file.Close() |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
return index, nil |
| 62 |
} |
| 63 |
|
| 64 |
// OpenChainIndex expects a billy.Filesystem representing a .git directory. |
| 65 |
// It will read a commit-graph chain file and return a coalesced index. |
| 66 |
// If the chain file or a graph in that chain is not present, an error is returned. |
| 67 |
// |
| 68 |
// See: https://git-scm.com/docs/commit-graph |
| 69 |
func OpenChainIndex(fs billy.Filesystem) (Index, error) { |
| 70 |
chainFile, err := fs.Open(path.Join("objects", "info", "commit-graphs", "commit-graph-chain")) |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
|
| 75 |
chain, err := OpenChainFile(chainFile) |
| 76 |
_ = chainFile.Close() |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
|
| 81 |
var index Index |
| 82 |
for _, hash := range chain { |
| 83 |
|
| 84 |
file, err := fs.Open(path.Join("objects", "info", "commit-graphs", "graph-"+hash+".graph")) |
| 85 |
if err != nil { |
| 86 |
// Ignore all other file closing errors and return the error from opening the last file in the graph |
| 87 |
_ = index.Close() |
| 88 |
return nil, err |
| 89 |
} |
| 90 |
|
| 91 |
index, err = OpenFileIndexWithParent(file, index) |
| 92 |
if err != nil { |
| 93 |
// Ignore file closing errors and return the error from OpenFileIndex instead |
| 94 |
_ = index.Close() |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return index, nil |
| 100 |
} |