diff options
author | Amine <hilalyamine@gmail.com> | 2019-08-13 16:47:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-13 16:47:24 +0200 |
commit | cf960bc7a5bd0b7af28d35de33131fb0b5ce5253 (patch) | |
tree | 5df133c91bb4e1ccc5f9fbeb4664416b93d23bf5 /commands/select | |
parent | 146894a5657d3b20dbaf769a950b12bd19df499c (diff) | |
parent | c809d37152ea87a66fc281730042dcb4299a8263 (diff) | |
download | git-bug-cf960bc7a5bd0b7af28d35de33131fb0b5ce5253.tar.gz |
Merge pull request #193 from MichaelMure/immutableID
Future proof the operation's ID
Diffstat (limited to 'commands/select')
-rw-r--r-- | commands/select/select.go | 15 | ||||
-rw-r--r-- | commands/select/select_test.go | 6 |
2 files changed, 11 insertions, 10 deletions
diff --git a/commands/select/select.go b/commands/select/select.go index b080d277..fdc87154 100644 --- a/commands/select/select.go +++ b/commands/select/select.go @@ -7,11 +7,12 @@ import ( "os" "path" + "github.com/pkg/errors" + "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/repository" - "github.com/MichaelMure/git-bug/util/git" - "github.com/pkg/errors" ) const selectFile = "select" @@ -69,7 +70,7 @@ func ResolveBug(repo *cache.RepoCache, args []string) (*cache.BugCache, []string } // Select will select a bug for future use -func Select(repo *cache.RepoCache, id string) error { +func Select(repo *cache.RepoCache, id entity.Id) error { selectPath := selectFilePath(repo) f, err := os.OpenFile(selectPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) @@ -77,7 +78,7 @@ func Select(repo *cache.RepoCache, id string) error { return err } - _, err = f.WriteString(id) + _, err = f.WriteString(id.String()) if err != nil { return err } @@ -112,8 +113,8 @@ func selected(repo *cache.RepoCache) (*cache.BugCache, error) { return nil, fmt.Errorf("the select file should be < 100 bytes") } - h := git.Hash(buf) - if !h.IsValid() { + id := entity.Id(buf) + if err := id.Validate(); err != nil { err = os.Remove(selectPath) if err != nil { return nil, errors.Wrap(err, "error while removing invalid select file") @@ -122,7 +123,7 @@ func selected(repo *cache.RepoCache) (*cache.BugCache, error) { return nil, fmt.Errorf("select file in invalid, removing it") } - b, err := repo.ResolveBug(string(h)) + b, err := repo.ResolveBug(id) if err != nil { return nil, err } diff --git a/commands/select/select_test.go b/commands/select/select_test.go index 393ec88b..989d6b3c 100644 --- a/commands/select/select_test.go +++ b/commands/select/select_test.go @@ -52,12 +52,12 @@ func TestSelect(t *testing.T) { require.Equal(t, b1.Id(), b3.Id()) // override selection with same id - b4, _, err := ResolveBug(repoCache, []string{b1.Id()}) + b4, _, err := ResolveBug(repoCache, []string{b1.Id().String()}) require.NoError(t, err) require.Equal(t, b1.Id(), b4.Id()) // override selection with a prefix - b5, _, err := ResolveBug(repoCache, []string{b1.HumanId()}) + b5, _, err := ResolveBug(repoCache, []string{b1.Id().Human()}) require.NoError(t, err) require.Equal(t, b1.Id(), b5.Id()) @@ -67,7 +67,7 @@ func TestSelect(t *testing.T) { require.Equal(t, b1.Id(), b6.Id()) // override with a different id - b7, _, err := ResolveBug(repoCache, []string{b2.Id()}) + b7, _, err := ResolveBug(repoCache, []string{b2.Id().String()}) require.NoError(t, err) require.Equal(t, b2.Id(), b7.Id()) |