Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bridge/github/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestPushPull(t *testing.T) {

// create repo backend
repo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repo)
defer repository.CleanupTestRepos(repo)

backend, err := cache.NewRepoCache(repo)
require.NoError(t, err)
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestPushPull(t *testing.T) {
fmt.Printf("test repository exported in %f seconds\n", time.Since(start).Seconds())

repoTwo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repoTwo)
defer repository.CleanupTestRepos(repoTwo)

// create a second backend
backendTwo, err := cache.NewRepoCache(repoTwo)
Expand Down
2 changes: 1 addition & 1 deletion bridge/github/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func Test_Importer(t *testing.T) {
}

repo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repo)
defer repository.CleanupTestRepos(repo)

backend, err := cache.NewRepoCache(repo)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions bridge/gitlab/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestPushPull(t *testing.T) {

// create repo backend
repo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repo)
defer repository.CleanupTestRepos(repo)

backend, err := cache.NewRepoCache(repo)
require.NoError(t, err)
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestPushPull(t *testing.T) {
fmt.Printf("test repository exported in %f seconds\n", time.Since(start).Seconds())

repoTwo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repoTwo)
defer repository.CleanupTestRepos(repoTwo)

// create a second backend
backendTwo, err := cache.NewRepoCache(repoTwo)
Expand Down
2 changes: 1 addition & 1 deletion bridge/gitlab/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestImport(t *testing.T) {
}

repo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repo)
defer repository.CleanupTestRepos(repo)

backend, err := cache.NewRepoCache(repo)
require.NoError(t, err)
Expand Down
27 changes: 23 additions & 4 deletions bug/bug.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const createClockEntryPattern = "create-clock-%d"
const editClockEntryPrefix = "edit-clock-"
const editClockEntryPattern = "edit-clock-%d"

const creationClockName = "bug-create"
const editClockName = "bug-edit"

var ErrBugNotExist = errors.New("bug doesn't exist")

func NewErrMultipleMatchBug(matching []entity.Id) *entity.ErrMultipleMatch {
Expand Down Expand Up @@ -197,10 +200,18 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) {
}

// Update the clocks
if err := repo.WitnessCreate(bug.createTime); err != nil {
createClock, err := repo.GetOrCreateClock(creationClockName)
if err != nil {
return nil, err
}
if err := createClock.Witness(bug.createTime); err != nil {
return nil, errors.Wrap(err, "failed to update create lamport clock")
}
if err := repo.WitnessEdit(bug.editTime); err != nil {
editClock, err := repo.GetOrCreateClock(editClockName)
if err != nil {
return nil, err
}
if err := editClock.Witness(bug.editTime); err != nil {
return nil, errors.Wrap(err, "failed to update edit lamport clock")
}

Expand Down Expand Up @@ -412,7 +423,11 @@ func (bug *Bug) Commit(repo repository.ClockedRepo) error {
return err
}

bug.editTime, err = repo.EditTimeIncrement()
editClock, err := repo.GetOrCreateClock(editClockName)
if err != nil {
return err
}
bug.editTime, err = editClock.Increment()
if err != nil {
return err
}
Expand All @@ -423,7 +438,11 @@ func (bug *Bug) Commit(repo repository.ClockedRepo) error {
Name: fmt.Sprintf(editClockEntryPattern, bug.editTime),
})
if bug.lastCommit == "" {
bug.createTime, err = repo.CreateTimeIncrement()
createClock, err := repo.GetOrCreateClock(creationClockName)
if err != nil {
return err
}
bug.createTime, err = createClock.Increment()
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions bug/bug_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

func TestPushPull(t *testing.T) {
repoA, repoB, remote := repository.SetupReposAndRemote(t)
defer repository.CleanupTestRepos(t, repoA, repoB, remote)
repoA, repoB, remote := repository.SetupReposAndRemote()
defer repository.CleanupTestRepos(repoA, repoB, remote)

reneA := identity.NewIdentity("René Descartes", "[email protected]")

Expand Down Expand Up @@ -87,8 +87,8 @@ func BenchmarkRebaseTheirs(b *testing.B) {
}

func _RebaseTheirs(t testing.TB) {
repoA, repoB, remote := repository.SetupReposAndRemote(t)
defer repository.CleanupTestRepos(t, repoA, repoB, remote)
repoA, repoB, remote := repository.SetupReposAndRemote()
defer repository.CleanupTestRepos(repoA, repoB, remote)

reneA := identity.NewIdentity("René Descartes", "[email protected]")

Expand Down Expand Up @@ -165,8 +165,8 @@ func BenchmarkRebaseOurs(b *testing.B) {
}

func _RebaseOurs(t testing.TB) {
repoA, repoB, remote := repository.SetupReposAndRemote(t)
defer repository.CleanupTestRepos(t, repoA, repoB, remote)
repoA, repoB, remote := repository.SetupReposAndRemote()
defer repository.CleanupTestRepos(repoA, repoB, remote)

reneA := identity.NewIdentity("René Descartes", "[email protected]")

Expand Down Expand Up @@ -254,8 +254,8 @@ func BenchmarkRebaseConflict(b *testing.B) {
}

func _RebaseConflict(t testing.TB) {
repoA, repoB, remote := repository.SetupReposAndRemote(t)
defer repository.CleanupTestRepos(t, repoA, repoB, remote)
repoA, repoB, remote := repository.SetupReposAndRemote()
defer repository.CleanupTestRepos(repoA, repoB, remote)

reneA := identity.NewIdentity("René Descartes", "[email protected]")

Expand Down
42 changes: 26 additions & 16 deletions bug/clocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,34 @@ import (
"github.com/MichaelMure/git-bug/repository"
)

// Witnesser will read all the available Bug to recreate the different logical
// clocks
func Witnesser(repo repository.ClockedRepo) error {
for b := range ReadAllLocalBugs(repo) {
if b.Err != nil {
return b.Err
}
// ClockLoader is the repository.ClockLoader for the Bug entity
var ClockLoader = repository.ClockLoader{
Clocks: []string{creationClockName, editClockName},
Witnesser: func(repo repository.ClockedRepo) error {
for b := range ReadAllLocalBugs(repo) {
if b.Err != nil {
return b.Err
}

err := repo.WitnessCreate(b.Bug.createTime)
if err != nil {
return err
}
createClock, err := repo.GetOrCreateClock(creationClockName)
if err != nil {
return err
}
err = createClock.Witness(b.Bug.createTime)
if err != nil {
return err
}

err = repo.WitnessEdit(b.Bug.editTime)
if err != nil {
return err
editClock, err := repo.GetOrCreateClock(editClockName)
if err != nil {
return err
}
err = editClock.Witness(b.Bug.editTime)
if err != nil {
return err
}
}
}

return nil
return nil
},
}
2 changes: 1 addition & 1 deletion bug/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestMetadata(t *testing.T) {

func TestID(t *testing.T) {
repo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repo)
defer repository.CleanupTestRepos(repo)

repos := []repository.ClockedRepo{
repository.NewMockRepoForTest(),
Expand Down
6 changes: 6 additions & 0 deletions cache/repo_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"sync"
Expand Down Expand Up @@ -149,6 +150,11 @@ func (c *RepoCache) lock() error {
return err
}

err = os.MkdirAll(filepath.Dir(lockPath), 0777)
if err != nil {
return err
}

f, err := os.Create(lockPath)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cache/repo_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestCache(t *testing.T) {
repo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repo)
defer repository.CleanupTestRepos(repo)

cache, err := NewRepoCache(repo)
require.NoError(t, err)
Expand Down Expand Up @@ -104,8 +104,8 @@ func TestCache(t *testing.T) {
}

func TestPushPull(t *testing.T) {
repoA, repoB, remote := repository.SetupReposAndRemote(t)
defer repository.CleanupTestRepos(t, repoA, repoB, remote)
repoA, repoB, remote := repository.SetupReposAndRemote()
defer repository.CleanupTestRepos(repoA, repoB, remote)

cacheA, err := NewRepoCache(repoA)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func loadRepo(cmd *cobra.Command, args []string) error {
return fmt.Errorf("unable to get the current working directory: %q", err)
}

repo, err = repository.NewGitRepo(cwd, bug.Witnesser)
repo, err = repository.NewGitRepo(cwd, []repository.ClockLoader{bug.ClockLoader})
if err == repository.ErrNotARepo {
return fmt.Errorf("%s must be run from within a git repo", rootCommandName)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/select/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestSelect(t *testing.T) {
repo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repo)
defer repository.CleanupTestRepos(repo)

repoCache, err := cache.NewRepoCache(repo)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestQueries(t *testing.T) {
repo := repository.CreateTestRepo(false)
defer repository.CleanupTestRepos(t, repo)
defer repository.CleanupTestRepos(repo)

random_bugs.FillRepoWithSeed(repo, 10, 42)

Expand Down
9 changes: 8 additions & 1 deletion identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,14 @@ func (i *Identity) Commit(repo repository.ClockedRepo) error {
}

// get the times where new versions starts to be valid
v.time = repo.EditTime()
// TODO: instead of this hardcoded clock for bugs only, this need to be
// a vector of edit clock, one for each entity (bug, PR, config ..)
bugEditClock, err := repo.GetOrCreateClock("bug-edit")
if err != nil {
return err
}

v.time = bugEditClock.Time()
v.unixTime = time.Now().Unix()

blobHash, err := v.Write(repo)
Expand Down
4 changes: 2 additions & 2 deletions identity/identity_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func TestPushPull(t *testing.T) {
repoA, repoB, remote := repository.SetupReposAndRemote(t)
defer repository.CleanupTestRepos(t, repoA, repoB, remote)
repoA, repoB, remote := repository.SetupReposAndRemote()
defer repository.CleanupTestRepos(repoA, repoB, remote)

identity1 := NewIdentity("name1", "email1")
err := identity1.Commit(repoA)
Expand Down
3 changes: 3 additions & 0 deletions identity/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Version struct {
// The lamport time at which this version become effective
// The reference time is the bug edition lamport clock
// It must be the first field in this struct due to https://github.com/golang/go/issues/599
//
// TODO: BREAKING CHANGE - this need to actually be one edition lamport time **per entity**
// This is not a problem right now but will be when more entities are added (pull-request, config ...)
time lamport.Time
unixTime int64

Expand Down
4 changes: 1 addition & 3 deletions misc/random_bugs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ func main() {
panic(err)
}

repo, err := repository.NewGitRepo(dir, func(repo repository.ClockedRepo) error {
return nil
})
repo, err := repository.NewGitRepo(dir)
if err != nil {
panic(err)
}
Expand Down
8 changes: 8 additions & 0 deletions repository/config_mem.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repository

import (
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -77,10 +78,17 @@ func (mc *MemConfig) ReadTimestamp(key string) (time.Time, error) {

// RmConfigs remove all key/value pair matching the key prefix
func (mc *MemConfig) RemoveAll(keyPrefix string) error {
found := false
for key := range mc.config {
if strings.HasPrefix(key, keyPrefix) {
delete(mc.config, key)
found = true
}
}

if !found {
return fmt.Errorf("section not found")
}

return nil
}
7 changes: 7 additions & 0 deletions repository/config_mem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package repository

import "testing"

func TestNewMemConfig(t *testing.T) {
testConfig(t, NewMemConfig())
}
Loading