diff options
author | Michael Muré <batolettre@gmail.com> | 2020-11-08 19:15:06 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2021-02-14 12:17:44 +0100 |
commit | 7163b2283b4542a4d4abfe9a71963f122322bde7 (patch) | |
tree | 93a52a65c5ba1fd3e4112b2aa53c2c438bb5400c /bug/git_tree.go | |
parent | 5ae8a132772385c903a62de2ceec02a97f108a01 (diff) | |
download | git-bug-7163b2283b4542a4d4abfe9a71963f122322bde7.tar.gz |
bug: Id from first operation data, not git + remove root link
Diffstat (limited to 'bug/git_tree.go')
-rw-r--r-- | bug/git_tree.go | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/bug/git_tree.go b/bug/git_tree.go new file mode 100644 index 00000000..a5583bda --- /dev/null +++ b/bug/git_tree.go @@ -0,0 +1,84 @@ +package bug + +import ( + "fmt" + "strings" + + "github.com/pkg/errors" + + "github.com/MichaelMure/git-bug/repository" + "github.com/MichaelMure/git-bug/util/lamport" +) + +type gitTree struct { + opsEntry repository.TreeEntry + createTime lamport.Time + editTime lamport.Time +} + +func readTree(repo repository.RepoData, hash repository.Hash) (*gitTree, error) { + tree := &gitTree{} + + entries, err := repo.ReadTree(hash) + if err != nil { + return nil, errors.Wrap(err, "can't list git tree entries") + } + + opsFound := false + + for _, entry := range entries { + if entry.Name == opsEntryName { + tree.opsEntry = entry + opsFound = true + continue + } + if strings.HasPrefix(entry.Name, createClockEntryPrefix) { + n, err := fmt.Sscanf(entry.Name, createClockEntryPattern, &tree.createTime) + if err != nil { + return nil, errors.Wrap(err, "can't read create lamport time") + } + if n != 1 { + return nil, fmt.Errorf("could not parse create time lamport value") + } + } + if strings.HasPrefix(entry.Name, editClockEntryPrefix) { + n, err := fmt.Sscanf(entry.Name, editClockEntryPattern, &tree.editTime) + if err != nil { + return nil, errors.Wrap(err, "can't read edit lamport time") + } + if n != 1 { + return nil, fmt.Errorf("could not parse edit time lamport value") + } + } + } + + if !opsFound { + return nil, errors.New("invalid tree, missing the ops entry") + } + + return tree, nil +} + +func makeMediaTree(pack OperationPack) []repository.TreeEntry { + var tree []repository.TreeEntry + counter := 0 + added := make(map[repository.Hash]interface{}) + + for _, ops := range pack.Operations { + for _, file := range ops.GetFiles() { + if _, has := added[file]; !has { + tree = append(tree, repository.TreeEntry{ + ObjectType: repository.Blob, + Hash: file, + // The name is not important here, we only need to + // reference the blob. + Name: fmt.Sprintf("file%d", counter), + }) + counter++ + added[file] = struct{}{} + } + } + } + + return tree +} |