diff options
author | Michael Muré <batolettre@gmail.com> | 2018-07-14 22:17:37 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-07-14 22:17:37 +0200 |
commit | da470993d13ce63087034db9b7e8ffbdf18e87a5 (patch) | |
tree | 7846ad86de6d93c51c54bf3e764a2108baa63612 /repository/git.go | |
parent | f8e07748743f7e66ff1adf101a797cb1bedfc140 (diff) | |
download | git-bug-da470993d13ce63087034db9b7e8ffbdf18e87a5.tar.gz |
complete the storage/read process + tests (!)
Diffstat (limited to 'repository/git.go')
-rw-r--r-- | repository/git.go | 69 |
1 files changed, 64 insertions, 5 deletions
diff --git a/repository/git.go b/repository/git.go index a55e451c..50806778 100644 --- a/repository/git.go +++ b/repository/git.go @@ -134,15 +134,26 @@ func (repo *GitRepo) StoreData(data []byte) (util.Hash, error) { return util.Hash(stdout), err } -// StoreTree will store a mapping key-->Hash as a Git tree -func (repo *GitRepo) StoreTree(mapping map[string]util.Hash) (util.Hash, error) { - var buffer bytes.Buffer +// ReadData will attempt to read arbitrary data from the given hash +func (repo *GitRepo) ReadData(hash util.Hash) ([]byte, error) { + var stdout bytes.Buffer + var stderr bytes.Buffer - for key, hash := range mapping { - buffer.WriteString(fmt.Sprintf("100644 blob %s\t%s\n", hash, key)) + err := repo.runGitCommandWithIO(nil, &stdout, &stderr, "cat-file", "-p", string(hash)) + + if err != nil { + return []byte{}, err } + return stdout.Bytes(), nil +} + +// StoreTree will store a mapping key-->Hash as a Git tree +func (repo *GitRepo) StoreTree(entries []TreeEntry) (util.Hash, error) { + buffer := prepareTreeEntries(entries) + stdout, err := repo.runGitCommandWithStdin(&buffer, "mktree") + if err != nil { return "", err } @@ -179,3 +190,51 @@ func (repo *GitRepo) UpdateRef(ref string, hash util.Hash) error { return err } + +// ListRefs will return a list of Git ref matching the given refspec +func (repo *GitRepo) ListRefs(refspec string) ([]string, error) { + // the format option will strip the ref name to keep only the last part (ie, the bug id) + stdout, err := repo.runGitCommand("for-each-ref", "--format=%(refname:lstrip=-1)", refspec) + + if err != nil { + return nil, err + } + + splitted := strings.Split(stdout, "\n") + + if len(splitted) == 1 && splitted[0] == "" { + return []string{}, nil + } + + return splitted, nil +} + +// ListCommits will return the list of commit hashes of a ref, in chronological order +func (repo *GitRepo) ListCommits(ref string) ([]util.Hash, error) { + stdout, err := repo.runGitCommand("rev-list", "--first-parent", "--reverse", ref) + + if err != nil { + return nil, err + } + + splitted := strings.Split(stdout, "\n") + + casted := make([]util.Hash, len(splitted)) + for i, line := range splitted { + casted[i] = util.Hash(line) + } + + return casted, nil + +} + +// ListEntries will return the list of entries in a Git tree +func (repo *GitRepo) ListEntries(hash util.Hash) ([]TreeEntry, error) { + stdout, err := repo.runGitCommand("ls-tree", string(hash)) + + if err != nil { + return nil, err + } + + return readTreeEntries(stdout) +} |