aboutsummaryrefslogtreecommitdiffstats
path: root/repository
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-11 22:04:16 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-11 22:14:46 +0200
commit3605887345792d2f981f971c6c4a2cb7f86a343e (patch)
treeafd525b6e3a638e4c619a5a986fcb2811c297444 /repository
parent7b05983c19af4da70f2a9a5062913f4e4f5d5faa (diff)
downloadgit-bug-3605887345792d2f981f971c6c4a2cb7f86a343e.tar.gz
reorganize package for a more idomatic go
Diffstat (limited to 'repository')
-rw-r--r--repository/git.go59
-rw-r--r--repository/mock_repo.go71
-rw-r--r--repository/repo.go31
-rw-r--r--repository/tree_entry.go6
-rw-r--r--repository/tree_entry_test.go6
5 files changed, 88 insertions, 85 deletions
diff --git a/repository/git.go b/repository/git.go
index 9b5aebb5..3b5a3a1a 100644
--- a/repository/git.go
+++ b/repository/git.go
@@ -11,7 +11,8 @@ import (
"path"
"strings"
- "github.com/MichaelMure/git-bug/util"
+ "github.com/MichaelMure/git-bug/util/git"
+ "github.com/MichaelMure/git-bug/util/lamport"
)
const createClockFile = "/.git/git-bug/create-clock"
@@ -23,8 +24,8 @@ var ErrNotARepo = errors.New("not a git repository")
// GitRepo represents an instance of a (local) git repository.
type GitRepo struct {
Path string
- createClock *util.PersistedLamport
- editClock *util.PersistedLamport
+ createClock *lamport.Persisted
+ editClock *lamport.Persisted
}
// Run the given git command with the given I/O reader/writers, returning an error if it fails.
@@ -175,16 +176,16 @@ func (repo *GitRepo) PushRefs(remote string, refSpec string) (string, error) {
}
// StoreData will store arbitrary data and return the corresponding hash
-func (repo *GitRepo) StoreData(data []byte) (util.Hash, error) {
+func (repo *GitRepo) StoreData(data []byte) (git.Hash, error) {
var stdin = bytes.NewReader(data)
stdout, err := repo.runGitCommandWithStdin(stdin, "hash-object", "--stdin", "-w")
- return util.Hash(stdout), err
+ return git.Hash(stdout), err
}
// ReadData will attempt to read arbitrary data from the given hash
-func (repo *GitRepo) ReadData(hash util.Hash) ([]byte, error) {
+func (repo *GitRepo) ReadData(hash git.Hash) ([]byte, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
@@ -198,7 +199,7 @@ func (repo *GitRepo) ReadData(hash util.Hash) ([]byte, error) {
}
// StoreTree will store a mapping key-->Hash as a Git tree
-func (repo *GitRepo) StoreTree(entries []TreeEntry) (util.Hash, error) {
+func (repo *GitRepo) StoreTree(entries []TreeEntry) (git.Hash, error) {
buffer := prepareTreeEntries(entries)
stdout, err := repo.runGitCommandWithStdin(&buffer, "mktree")
@@ -207,22 +208,22 @@ func (repo *GitRepo) StoreTree(entries []TreeEntry) (util.Hash, error) {
return "", err
}
- return util.Hash(stdout), nil
+ return git.Hash(stdout), nil
}
// StoreCommit will store a Git commit with the given Git tree
-func (repo *GitRepo) StoreCommit(treeHash util.Hash) (util.Hash, error) {
+func (repo *GitRepo) StoreCommit(treeHash git.Hash) (git.Hash, error) {
stdout, err := repo.runGitCommand("commit-tree", string(treeHash))
if err != nil {
return "", err
}
- return util.Hash(stdout), nil
+ return git.Hash(stdout), nil
}
// StoreCommitWithParent will store a Git commit with the given Git tree
-func (repo *GitRepo) StoreCommitWithParent(treeHash util.Hash, parent util.Hash) (util.Hash, error) {
+func (repo *GitRepo) StoreCommitWithParent(treeHash git.Hash, parent git.Hash) (git.Hash, error) {
stdout, err := repo.runGitCommand("commit-tree", string(treeHash),
"-p", string(parent))
@@ -230,11 +231,11 @@ func (repo *GitRepo) StoreCommitWithParent(treeHash util.Hash, parent util.Hash)
return "", err
}
- return util.Hash(stdout), nil
+ return git.Hash(stdout), nil
}
// UpdateRef will create or update a Git reference
-func (repo *GitRepo) UpdateRef(ref string, hash util.Hash) error {
+func (repo *GitRepo) UpdateRef(ref string, hash git.Hash) error {
_, err := repo.runGitCommand("update-ref", ref, string(hash))
return err
@@ -276,7 +277,7 @@ func (repo *GitRepo) CopyRef(source string, dest string) error {
}
// ListCommits will return the list of commit hashes of a ref, in chronological order
-func (repo *GitRepo) ListCommits(ref string) ([]util.Hash, error) {
+func (repo *GitRepo) ListCommits(ref string) ([]git.Hash, error) {
stdout, err := repo.runGitCommand("rev-list", "--first-parent", "--reverse", ref)
if err != nil {
@@ -285,9 +286,9 @@ func (repo *GitRepo) ListCommits(ref string) ([]util.Hash, error) {
split := strings.Split(stdout, "\n")
- casted := make([]util.Hash, len(split))
+ casted := make([]git.Hash, len(split))
for i, line := range split {
- casted[i] = util.Hash(line)
+ casted[i] = git.Hash(line)
}
return casted, nil
@@ -295,7 +296,7 @@ func (repo *GitRepo) ListCommits(ref string) ([]util.Hash, error) {
}
// ListEntries will return the list of entries in a Git tree
-func (repo *GitRepo) ListEntries(hash util.Hash) ([]TreeEntry, error) {
+func (repo *GitRepo) ListEntries(hash git.Hash) ([]TreeEntry, error) {
stdout, err := repo.runGitCommand("ls-tree", string(hash))
if err != nil {
@@ -306,25 +307,25 @@ func (repo *GitRepo) ListEntries(hash util.Hash) ([]TreeEntry, error) {
}
// FindCommonAncestor will return the last common ancestor of two chain of commit
-func (repo *GitRepo) FindCommonAncestor(hash1 util.Hash, hash2 util.Hash) (util.Hash, error) {
+func (repo *GitRepo) FindCommonAncestor(hash1 git.Hash, hash2 git.Hash) (git.Hash, error) {
stdout, err := repo.runGitCommand("merge-base", string(hash1), string(hash2))
if err != nil {
return "", nil
}
- return util.Hash(stdout), nil
+ return git.Hash(stdout), nil
}
// GetTreeHash return the git tree hash referenced in a commit
-func (repo *GitRepo) GetTreeHash(commit util.Hash) (util.Hash, error) {
+func (repo *GitRepo) GetTreeHash(commit git.Hash) (git.Hash, error) {
stdout, err := repo.runGitCommand("rev-parse", string(commit)+"^{tree}")
if err != nil {
return "", nil
}
- return util.Hash(stdout), nil
+ return git.Hash(stdout), nil
}
// AddRemote add a new remote to the repository
@@ -337,19 +338,19 @@ func (repo *GitRepo) AddRemote(name string, url string) error {
func (repo *GitRepo) createClocks() {
createPath := path.Join(repo.Path, createClockFile)
- repo.createClock = util.NewPersistedLamport(createPath)
+ repo.createClock = lamport.NewPersisted(createPath)
editPath := path.Join(repo.Path, editClockFile)
- repo.editClock = util.NewPersistedLamport(editPath)
+ repo.editClock = lamport.NewPersisted(editPath)
}
func (repo *GitRepo) LoadClocks() error {
- createClock, err := util.LoadPersistedLamport(repo.GetPath() + createClockFile)
+ createClock, err := lamport.LoadPersisted(repo.GetPath() + createClockFile)
if err != nil {
return err
}
- editClock, err := util.LoadPersistedLamport(repo.GetPath() + editClockFile)
+ editClock, err := lamport.LoadPersisted(repo.GetPath() + editClockFile)
if err != nil {
return err
}
@@ -373,18 +374,18 @@ func (repo *GitRepo) WriteClocks() error {
return nil
}
-func (repo *GitRepo) CreateTimeIncrement() (util.LamportTime, error) {
+func (repo *GitRepo) CreateTimeIncrement() (lamport.Time, error) {
return repo.createClock.Increment()
}
-func (repo *GitRepo) EditTimeIncrement() (util.LamportTime, error) {
+func (repo *GitRepo) EditTimeIncrement() (lamport.Time, error) {
return repo.editClock.Increment()
}
-func (repo *GitRepo) CreateWitness(time util.LamportTime) error {
+func (repo *GitRepo) CreateWitness(time lamport.Time) error {
return repo.createClock.Witness(time)
}
-func (repo *GitRepo) EditWitness(time util.LamportTime) error {
+func (repo *GitRepo) EditWitness(time lamport.Time) error {
return repo.editClock.Witness(time)
}
diff --git a/repository/mock_repo.go b/repository/mock_repo.go
index 50907876..23d3ef7d 100644
--- a/repository/mock_repo.go
+++ b/repository/mock_repo.go
@@ -4,32 +4,33 @@ import (
"crypto/sha1"
"fmt"
- "github.com/MichaelMure/git-bug/util"
+ "github.com/MichaelMure/git-bug/util/git"
+ "github.com/MichaelMure/git-bug/util/lamport"
)
// mockRepoForTest defines an instance of Repo that can be used for testing.
type mockRepoForTest struct {
- blobs map[util.Hash][]byte
- trees map[util.Hash]string
- commits map[util.Hash]commit
- refs map[string]util.Hash
- createClock util.LamportClock
- editClock util.LamportClock
+ blobs map[git.Hash][]byte
+ trees map[git.Hash]string
+ commits map[git.Hash]commit
+ refs map[string]git.Hash
+ createClock lamport.Clock
+ editClock lamport.Clock
}
type commit struct {
- treeHash util.Hash
- parent util.Hash
+ treeHash git.Hash
+ parent git.Hash
}
func NewMockRepoForTest() Repo {
return &mockRepoForTest{
- blobs: make(map[util.Hash][]byte),
- trees: make(map[util.Hash]string),
- commits: make(map[util.Hash]commit),
- refs: make(map[string]util.Hash),
- createClock: util.NewLamportClock(),
- editClock: util.NewLamportClock(),
+ blobs: make(map[git.Hash][]byte),
+ trees: make(map[git.Hash]string),
+ commits: make(map[git.Hash]commit),
+ refs: make(map[string]git.Hash),
+ createClock: lamport.NewClock(),
+ editClock: lamport.NewClock(),
}
}
@@ -61,14 +62,14 @@ func (r *mockRepoForTest) FetchRefs(remote string, refSpec string) (string, erro
return "", nil
}
-func (r *mockRepoForTest) StoreData(data []byte) (util.Hash, error) {
+func (r *mockRepoForTest) StoreData(data []byte) (git.Hash, error) {
rawHash := sha1.Sum(data)
- hash := util.Hash(fmt.Sprintf("%x", rawHash))
+ hash := git.Hash(fmt.Sprintf("%x", rawHash))
r.blobs[hash] = data
return hash, nil
}
-func (r *mockRepoForTest) ReadData(hash util.Hash) ([]byte, error) {
+func (r *mockRepoForTest) ReadData(hash git.Hash) ([]byte, error) {
data, ok := r.blobs[hash]
if !ok {
@@ -78,27 +79,27 @@ func (r *mockRepoForTest) ReadData(hash util.Hash) ([]byte, error) {
return data, nil
}
-func (r *mockRepoForTest) StoreTree(entries []TreeEntry) (util.Hash, error) {
+func (r *mockRepoForTest) StoreTree(entries []TreeEntry) (git.Hash, error) {
buffer := prepareTreeEntries(entries)
rawHash := sha1.Sum(buffer.Bytes())
- hash := util.Hash(fmt.Sprintf("%x", rawHash))
+ hash := git.Hash(fmt.Sprintf("%x", rawHash))
r.trees[hash] = buffer.String()
return hash, nil
}
-func (r *mockRepoForTest) StoreCommit(treeHash util.Hash) (util.Hash, error) {
+func (r *mockRepoForTest) StoreCommit(treeHash git.Hash) (git.Hash, error) {
rawHash := sha1.Sum([]byte(treeHash))
- hash := util.Hash(fmt.Sprintf("%x", rawHash))
+ hash := git.Hash(fmt.Sprintf("%x", rawHash))
r.commits[hash] = commit{
treeHash: treeHash,
}
return hash, nil
}
-func (r *mockRepoForTest) StoreCommitWithParent(treeHash util.Hash, parent util.Hash) (util.Hash, error) {
+func (r *mockRepoForTest) StoreCommitWithParent(treeHash git.Hash, parent git.Hash) (git.Hash, error) {
rawHash := sha1.Sum([]byte(treeHash + parent))
- hash := util.Hash(fmt.Sprintf("%x", rawHash))
+ hash := git.Hash(fmt.Sprintf("%x", rawHash))
r.commits[hash] = commit{
treeHash: treeHash,
parent: parent,
@@ -106,7 +107,7 @@ func (r *mockRepoForTest) StoreCommitWithParent(treeHash util.Hash, parent util.
return hash, nil
}
-func (r *mockRepoForTest) UpdateRef(ref string, hash util.Hash) error {
+func (r *mockRepoForTest) UpdateRef(ref string, hash git.Hash) error {
r.refs[ref] = hash
return nil
}
@@ -139,8 +140,8 @@ func (r *mockRepoForTest) ListRefs(refspec string) ([]string, error) {
return keys, nil
}
-func (r *mockRepoForTest) ListCommits(ref string) ([]util.Hash, error) {
- var hashes []util.Hash
+func (r *mockRepoForTest) ListCommits(ref string) ([]git.Hash, error) {
+ var hashes []git.Hash
hash := r.refs[ref]
@@ -151,14 +152,14 @@ func (r *mockRepoForTest) ListCommits(ref string) ([]util.Hash, error) {
break
}
- hashes = append([]util.Hash{hash}, hashes...)
+ hashes = append([]git.Hash{hash}, hashes...)
hash = commit.parent
}
return hashes, nil
}
-func (r *mockRepoForTest) ListEntries(hash util.Hash) ([]TreeEntry, error) {
+func (r *mockRepoForTest) ListEntries(hash git.Hash) ([]TreeEntry, error) {
var data string
data, ok := r.trees[hash]
@@ -181,11 +182,11 @@ func (r *mockRepoForTest) ListEntries(hash util.Hash) ([]TreeEntry, error) {
return readTreeEntries(data)
}
-func (r *mockRepoForTest) FindCommonAncestor(hash1 util.Hash, hash2 util.Hash) (util.Hash, error) {
+func (r *mockRepoForTest) FindCommonAncestor(hash1 git.Hash, hash2 git.Hash) (git.Hash, error) {
panic("implement me")
}
-func (r *mockRepoForTest) GetTreeHash(commit util.Hash) (util.Hash, error) {
+func (r *mockRepoForTest) GetTreeHash(commit git.Hash) (git.Hash, error) {
panic("implement me")
}
@@ -197,20 +198,20 @@ func (r *mockRepoForTest) WriteClocks() error {
return nil
}
-func (r *mockRepoForTest) CreateTimeIncrement() (util.LamportTime, error) {
+func (r *mockRepoForTest) CreateTimeIncrement() (lamport.Time, error) {
return r.createClock.Increment(), nil
}
-func (r *mockRepoForTest) EditTimeIncrement() (util.LamportTime, error) {
+func (r *mockRepoForTest) EditTimeIncrement() (lamport.Time, error) {
return r.editClock.Increment(), nil
}
-func (r *mockRepoForTest) CreateWitness(time util.LamportTime) error {
+func (r *mockRepoForTest) CreateWitness(time lamport.Time) error {
r.createClock.Witness(time)
return nil
}
-func (r *mockRepoForTest) EditWitness(time util.LamportTime) error {
+func (r *mockRepoForTest) EditWitness(time lamport.Time) error {
r.editClock.Witness(time)
return nil
}
diff --git a/repository/repo.go b/repository/repo.go
index 355c4cf9..3d18431d 100644
--- a/repository/repo.go
+++ b/repository/repo.go
@@ -5,7 +5,8 @@ import (
"bytes"
"strings"
- "github.com/MichaelMure/git-bug/util"
+ "github.com/MichaelMure/git-bug/util/git"
+ "github.com/MichaelMure/git-bug/util/lamport"
)
// Repo represents a source code repository.
@@ -29,22 +30,22 @@ type Repo interface {
PushRefs(remote string, refSpec string) (string, error)
// StoreData will store arbitrary data and return the corresponding hash
- StoreData(data []byte) (util.Hash, error)
+ StoreData(data []byte) (git.Hash, error)
// ReadData will attempt to read arbitrary data from the given hash
- ReadData(hash util.Hash) ([]byte, error)
+ ReadData(hash git.Hash) ([]byte, error)
// StoreTree will store a mapping key-->Hash as a Git tree
- StoreTree(mapping []TreeEntry) (util.Hash, error)
+ StoreTree(mapping []TreeEntry) (git.Hash, error)
// StoreCommit will store a Git commit with the given Git tree
- StoreCommit(treeHash util.Hash) (util.Hash, error)
+ StoreCommit(treeHash git.Hash) (git.Hash, error)
// StoreCommit will store a Git commit with the given Git tree
- StoreCommitWithParent(treeHash util.Hash, parent util.Hash) (util.Hash, error)
+ StoreCommitWithParent(treeHash git.Hash, parent git.Hash) (git.Hash, error)
// UpdateRef will create or update a Git reference
- UpdateRef(ref string, hash util.Hash) error
+ UpdateRef(ref string, hash git.Hash) error
// ListRefs will return a list of Git ref matching the given refspec
ListRefs(refspec string) ([]string, error)
@@ -56,28 +57,28 @@ type Repo interface {
CopyRef(source string, dest string) error
// ListCommits will return the list of tree hashes of a ref, in chronological order
- ListCommits(ref string) ([]util.Hash, error)
+ ListCommits(ref string) ([]git.Hash, error)
// ListEntries will return the list of entries in a Git tree
- ListEntries(hash util.Hash) ([]TreeEntry, error)
+ ListEntries(hash git.Hash) ([]TreeEntry, error)
// FindCommonAncestor will return the last common ancestor of two chain of commit
- FindCommonAncestor(hash1 util.Hash, hash2 util.Hash) (util.Hash, error)
+ FindCommonAncestor(hash1 git.Hash, hash2 git.Hash) (git.Hash, error)
// GetTreeHash return the git tree hash referenced in a commit
- GetTreeHash(commit util.Hash) (util.Hash, error)
+ GetTreeHash(commit git.Hash) (git.Hash, error)
LoadClocks() error
WriteClocks() error
- CreateTimeIncrement() (util.LamportTime, error)
+ CreateTimeIncrement() (lamport.Time, error)
- EditTimeIncrement() (util.LamportTime, error)
+ EditTimeIncrement() (lamport.Time, error)
- CreateWitness(time util.LamportTime) error
+ CreateWitness(time lamport.Time) error
- EditWitness(time util.LamportTime) error
+ EditWitness(time lamport.Time) error
}
func prepareTreeEntries(entries []TreeEntry) bytes.Buffer {
diff --git a/repository/tree_entry.go b/repository/tree_entry.go
index 8b6b5e1b..3e532a30 100644
--- a/repository/tree_entry.go
+++ b/repository/tree_entry.go
@@ -4,12 +4,12 @@ import (
"fmt"
"strings"
- "github.com/MichaelMure/git-bug/util"
+ "github.com/MichaelMure/git-bug/util/git"
)
type TreeEntry struct {
ObjectType ObjectType
- Hash util.Hash
+ Hash git.Hash
Name string
}
@@ -34,7 +34,7 @@ func ParseTreeEntry(line string) (TreeEntry, error) {
return TreeEntry{}, err
}
- hash := util.Hash(fields[2])
+ hash := git.Hash(fields[2])
name := strings.Join(fields[3:], "")
return TreeEntry{
diff --git a/repository/tree_entry_test.go b/repository/tree_entry_test.go
index f798a8a9..b5f774d3 100644
--- a/repository/tree_entry_test.go
+++ b/repository/tree_entry_test.go
@@ -1,15 +1,15 @@
package repository
import (
- "github.com/MichaelMure/git-bug/util"
+ "github.com/MichaelMure/git-bug/util/git"
"testing"
)
func TestTreeEntryFormat(t *testing.T) {
entries := []TreeEntry{
- {Blob, util.Hash("a85730cf5287d40a1e32d3a671ba2296c73387cb"), "name"},
- {Tree, util.Hash("a85730cf5287d40a1e32d3a671ba2296c73387cb"), "name"},
+ {Blob, git.Hash("a85730cf5287d40a1e32d3a671ba2296c73387cb"), "name"},
+ {Tree, git.Hash("a85730cf5287d40a1e32d3a671ba2296c73387cb"), "name"},
}
for _, entry := range entries {