diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2019-10-13 11:49:15 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2019-10-15 10:21:43 +0200 |
commit | e17ae6247480acd411801a96edda9ce38d850b9f (patch) | |
tree | 2d1fb28b7e38fcf70f1290e76c0eb496e334242f /src | |
parent | 3a049eb69c9b2b04d0e55b6240883dc38a2e5154 (diff) |
Make it possible to create new git indices
The API allows for creating brand new index/tree objects, but the
implementation didn't take into account that git update-index expects
the file to be non-existent instead of being zero-sized.
Change-Id: I4b380154626d037da62bf05a90b62d61bc92f8ee
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/qtmoduleupdater/repo.go | 7 | ||||
-rw-r--r-- | src/qtmoduleupdater/repo_test.go | 33 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/qtmoduleupdater/repo.go b/src/qtmoduleupdater/repo.go index 2f6e3efd..26cca03e 100644 --- a/src/qtmoduleupdater/repo.go +++ b/src/qtmoduleupdater/repo.go @@ -280,6 +280,7 @@ type Index struct { file *os.File repo Repository cachedEntries []IndexEntry + populated bool } // NewIndex creates a new git index based on a temporary file. Unless you'd like to @@ -293,6 +294,7 @@ func (repo Repository) NewIndex() (result *Index, err error) { if err != nil { return nil, err } + result.populated = false return result, nil } @@ -395,15 +397,20 @@ func (idx *Index) ReadTree(tree OID) error { if err != nil { return err } + idx.populated = true return idx.updateCachedEntries() } // Add adds a new entry to the index or updates an existing one if already present. func (idx *Index) Add(entry *IndexEntry) error { + if !idx.populated { + os.Remove(idx.file.Name()) + } _, err := idx.gitCommandWithIndex("update-index", "--add", "--cacheinfo", fmt.Sprintf("%s,%s,%s", entry.Permissions, entry.ID, entry.Path)).Run() if err != nil { return err } + idx.populated = true return idx.updateCachedEntries() } diff --git a/src/qtmoduleupdater/repo_test.go b/src/qtmoduleupdater/repo_test.go index 4b478ffc..8a77eec1 100644 --- a/src/qtmoduleupdater/repo_test.go +++ b/src/qtmoduleupdater/repo_test.go @@ -30,6 +30,8 @@ package main import ( "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestRepo(t *testing.T) { @@ -127,6 +129,37 @@ func TestIndex(t *testing.T) { } } +func TestNewIndex(t *testing.T) { + repo, err := OpenRepository("qt/qtbase") + assert.Nilf(t, err, "Unexpected error opening qtbase repo: %s", err) + + index, err := repo.NewIndex() + if err != nil { + t.Fatalf("Could not get index.") + } + defer index.Free() + + sampleContent := []byte("Hello World") + + indexEntry := &IndexEntry{ + Permissions: "100644", + Path: "test.txt", + } + + err = index.HashObject(indexEntry, sampleContent) + assert.Nilf(t, err, "should be able to add data to git database: %s", err) + + assert.Equal(t, OID("5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689"), indexEntry.ID) + + err = index.Add(indexEntry) + assert.Nilf(t, err, "should be able to add entry to new index: %s", err) + + tree, err := index.WriteTree() + assert.Nilf(t, err, "should be able to write tree: %s", err) + + assert.Equal(t, OID("4f11af3e4e067fc319abd053205f39bc40652f05"), tree) +} + func TestLog(t *testing.T) { repo, err := OpenRepository("qt/qtbase") if err != nil { |