Skip to content
Open
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: 4 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,10 @@ func (w *stagedWriter) Commit(ctx context.Context, message string, author Author
return nil, fmt.Errorf("build pending trees: %w", err)
}

if w.lastTree.Hash.Is(w.lastCommit.Tree) {
return nil, ErrNothingToCommit
}

if !w.writer.HasObjects() {
return nil, ErrNothingToCommit
}
Expand Down
51 changes: 49 additions & 2 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,53 @@ func (m *mockSigner) Sign(data []byte) (string, error) {
return m.signature, nil
}

func TestStagedWriter_Commit_UnchangedTree(t *testing.T) {
t.Parallel()
ctx := context.Background()
content := []byte("unchanged")

blobHash, err := protocol.Object(crypto.SHA1, protocol.ObjectTypeBlob, content)
require.NoError(t, err)
treeObj, err := protocol.BuildTreeObject(crypto.SHA1, []protocol.PackfileTreeEntry{{
FileMode: 0o100644,
FileName: "dashboard.json",
Hash: blobHash.String(),
}})
require.NoError(t, err)

parentHash := hash.MustFromHex("1234567890123456789012345678901234567890")
writer := &stagedWriter{
client: &httpClient{RawClient: &mockRawClient{}},
ref: Ref{Name: "refs/heads/main", Hash: parentHash},
writer: protocol.NewPackfileWriter(crypto.SHA1, protocol.PackfileStorageMemory),
objStorage: storage.NewInMemoryStorage(ctx),
treeEntries: map[string]*FlatTreeEntry{
"dashboard.json": {
Path: "dashboard.json",
Hash: blobHash,
Type: protocol.ObjectTypeBlob,
Mode: 0o100644,
},
},
dirtyPaths: make(map[string]bool),
storageMode: protocol.PackfileStorageMemory,
lastTree: &treeObj,
lastCommit: &Commit{Hash: parentHash, Tree: treeObj.Hash, Parent: hash.Zero},
}

updatedHash, err := writer.UpdateBlob(ctx, "dashboard.json", content)
require.NoError(t, err)
require.Equal(t, blobHash, updatedHash)

when := time.Unix(1234567890, 0).UTC()
commit, err := writer.Commit(ctx, "no-op update",
Author{Name: "A", Email: "a@b", Time: when},
Committer{Name: "A", Email: "a@b", Time: when})
require.ErrorIs(t, err, ErrNothingToCommit)
require.Nil(t, commit)
require.Equal(t, parentHash, writer.lastCommit.Hash)
}

func TestStagedWriter_Commit_SignerError(t *testing.T) {
t.Parallel()
ctx := context.Background()
Expand All @@ -305,7 +352,7 @@ func TestStagedWriter_Commit_SignerError(t *testing.T) {
dirtyPaths: make(map[string]bool),
storageMode: protocol.PackfileStorageMemory,
lastTree: &treeObj,
lastCommit: &Commit{Hash: hash.Zero, Tree: treeObj.Hash, Parent: hash.Zero},
lastCommit: &Commit{Hash: hash.Zero, Tree: hash.Zero, Parent: hash.Zero},
signer: &mockSigner{err: sentinel},
}

Expand Down Expand Up @@ -336,7 +383,7 @@ func TestStagedWriter_Commit_SignerInvoked(t *testing.T) {
dirtyPaths: make(map[string]bool),
storageMode: protocol.PackfileStorageMemory,
lastTree: &treeObj,
lastCommit: &Commit{Hash: hash.Zero, Tree: treeObj.Hash, Parent: hash.Zero},
lastCommit: &Commit{Hash: hash.Zero, Tree: hash.Zero, Parent: hash.Zero},
signer: m,
}

Expand Down