-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathcommit_test.go
More file actions
76 lines (63 loc) · 1.78 KB
/
commit_test.go
File metadata and controls
76 lines (63 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package git
import (
"testing"
)
func TestCreateCommitFromStage(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
// Configure user for the repo
cfg, err := repo.Config()
checkFatal(t, err)
defer cfg.Free()
err = cfg.SetString("user.name", "Test User")
checkFatal(t, err)
err = cfg.SetString("user.email", "test@example.com")
checkFatal(t, err)
// Stage a file
idx, err := repo.Index()
checkFatal(t, err)
err = idx.AddByPath("README")
checkFatal(t, err)
err = idx.Write()
checkFatal(t, err)
// Create commit from stage
oid, err := repo.CreateCommitFromStage("initial commit from stage", nil)
checkFatal(t, err)
if oid == nil || oid.IsZero() {
t.Fatal("expected a valid commit OID")
}
// Verify the commit
commit, err := repo.LookupCommit(oid)
checkFatal(t, err)
defer commit.Free()
if commit.Message() != "initial commit from stage" {
t.Fatalf("expected 'initial commit from stage', got %q", commit.Message())
}
}
func TestCreateCommitFromStageAllowEmpty(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
cfg, err := repo.Config()
checkFatal(t, err)
defer cfg.Free()
err = cfg.SetString("user.name", "Test User")
checkFatal(t, err)
err = cfg.SetString("user.email", "test@example.com")
checkFatal(t, err)
seedTestRepo(t, repo)
// Try empty commit without AllowEmptyCommit — should fail
_, err = repo.CreateCommitFromStage("empty commit", nil)
if err == nil {
t.Fatal("expected error for empty commit without AllowEmptyCommit")
}
// Try with AllowEmptyCommit
oid, err := repo.CreateCommitFromStage("empty commit", &CommitCreateOptions{
AllowEmptyCommit: true,
})
checkFatal(t, err)
if oid == nil || oid.IsZero() {
t.Fatal("expected a valid commit OID for empty commit")
}
}