Skip to content

Commit c265e6e

Browse files
authored
commit: make some fields public (#49)
1 parent 0cbb2f7 commit c265e6e

9 files changed

+60
-101
lines changed

commit.go

+19-35
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,26 @@ import (
1515

1616
// Commit contains information of a Git commit.
1717
type Commit struct {
18-
id *SHA1
19-
parents []*SHA1
20-
author *Signature
21-
committer *Signature
22-
message string
23-
18+
// The SHA-1 hash of the commit.
19+
ID *SHA1
20+
// The author of the commit.
21+
Author *Signature
22+
// The committer of the commit.
23+
Committer *Signature
24+
// The full commit message.
25+
Message string
26+
27+
parents []*SHA1
2428
*Tree
2529

2630
submodules Submodules
2731
submodulesOnce sync.Once
2832
submodulesErr error
2933
}
3034

31-
// ID returns the SHA-1 hash of the commit.
32-
func (c *Commit) ID() *SHA1 {
33-
return c.id
34-
}
35-
36-
// Author returns the author of the commit.
37-
func (c *Commit) Author() *Signature {
38-
return c.author
39-
}
40-
41-
// Committer returns the committer of the commit.
42-
func (c *Commit) Committer() *Signature {
43-
return c.committer
44-
}
45-
46-
// Message returns the full commit message.
47-
func (c *Commit) Message() string {
48-
return c.message
49-
}
50-
5135
// Summary returns first line of commit message.
5236
func (c *Commit) Summary() string {
53-
return strings.Split(c.message, "\n")[0]
37+
return strings.Split(c.Message, "\n")[0]
5438
}
5539

5640
// ParentsCount returns number of parents of the commit.
@@ -81,40 +65,40 @@ func (c *Commit) Parent(n int, opts ...CatFileCommitOptions) (*Commit, error) {
8165

8266
// CommitByPath returns the commit of the path in the state of this commit.
8367
func (c *Commit) CommitByPath(opts ...CommitByRevisionOptions) (*Commit, error) {
84-
return c.repo.CommitByRevision(c.id.String(), opts...)
68+
return c.repo.CommitByRevision(c.ID.String(), opts...)
8569
}
8670

8771
// CommitsByPage returns a paginated list of commits in the state of this commit.
8872
// The returned list is in reverse chronological order.
8973
func (c *Commit) CommitsByPage(page, size int, opts ...CommitsByPageOptions) ([]*Commit, error) {
90-
return c.repo.CommitsByPage(c.id.String(), page, size, opts...)
74+
return c.repo.CommitsByPage(c.ID.String(), page, size, opts...)
9175
}
9276

9377
// SearchCommits searches commit message with given pattern. The returned list is in reverse
9478
// chronological order.
9579
func (c *Commit) SearchCommits(pattern string, opts ...SearchCommitsOptions) ([]*Commit, error) {
96-
return c.repo.SearchCommits(c.id.String(), pattern, opts...)
80+
return c.repo.SearchCommits(c.ID.String(), pattern, opts...)
9781
}
9882

9983
// ShowNameStatus returns name status of the commit.
10084
func (c *Commit) ShowNameStatus(opts ...ShowNameStatusOptions) (*NameStatus, error) {
101-
return c.repo.ShowNameStatus(c.id.String(), opts...)
85+
return c.repo.ShowNameStatus(c.ID.String(), opts...)
10286
}
10387

10488
// CommitsCount returns number of total commits up to this commit.
10589
func (c *Commit) CommitsCount(opts ...RevListCountOptions) (int64, error) {
106-
return c.repo.RevListCount([]string{c.id.String()}, opts...)
90+
return c.repo.RevListCount([]string{c.ID.String()}, opts...)
10791
}
10892

10993
// FilesChangedSince returns a list of files changed after given commit ID.
11094
func (c *Commit) FilesChangedAfter(after string, opts ...DiffNameOnlyOptions) ([]string, error) {
111-
return c.repo.DiffNameOnly(after, c.id.String(), opts...)
95+
return c.repo.DiffNameOnly(after, c.ID.String(), opts...)
11296
}
11397

11498
// CommitsAfter returns a list of commits after given commit ID up to this commit. The returned
11599
// list is in reverse chronological order.
116100
func (c *Commit) CommitsAfter(after string, opts ...RevListOptions) ([]*Commit, error) {
117-
return c.repo.RevList([]string{after + "..." + c.id.String()}, opts...)
101+
return c.repo.RevList([]string{after + "..." + c.ID.String()}, opts...)
118102
}
119103

120104
// Ancestors returns a list of ancestors of this commit in reverse chronological order.
@@ -130,7 +114,7 @@ func (c *Commit) Ancestors(opts ...LogOptions) ([]*Commit, error) {
130114

131115
opt.Skip++
132116

133-
return c.repo.Log(c.id.String(), opt)
117+
return c.repo.Log(c.ID.String(), opt)
134118
}
135119

136120
type limitWriter struct {

commit_archive.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (c *Commit) CreateArchive(format ArchiveFormat, dst string) error {
2525
"--prefix="+prefix,
2626
"--format="+string(format),
2727
"-o", dst,
28-
c.id.String(),
28+
c.ID.String(),
2929
).RunInDir(c.repo.path)
3030
return err
3131
}

commit_test.go

+4-29
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package git
66

77
import (
88
"testing"
9-
"time"
109

1110
"github.com/stretchr/testify/assert"
1211
)
@@ -17,31 +16,7 @@ func TestCommit(t *testing.T) {
1716
t.Fatal(err)
1817
}
1918
t.Run("ID", func(t *testing.T) {
20-
assert.Equal(t, "435ffceb7ba576c937e922766e37d4f7abdcc122", c.ID().String())
21-
})
22-
23-
author := &Signature{
24-
Name: "Jordan McCullough",
25-
26-
When: time.Unix(1415213395, 0),
27-
}
28-
t.Run("Author", func(t *testing.T) {
29-
assert.Equal(t, author.Name, c.Author().Name)
30-
assert.Equal(t, author.Email, c.Author().Email)
31-
assert.Equal(t, author.When.Unix(), c.Author().When.Unix())
32-
})
33-
34-
t.Run("Committer", func(t *testing.T) {
35-
assert.Equal(t, author.Name, c.Committer().Name)
36-
assert.Equal(t, author.Email, c.Committer().Email)
37-
assert.Equal(t, author.When.Unix(), c.Committer().When.Unix())
38-
})
39-
40-
t.Run("Message", func(t *testing.T) {
41-
message := `Merge pull request #35 from githubtraining/travis-yml-docker
42-
43-
Add special option flag for Travis Docker use case`
44-
assert.Equal(t, message, c.Message())
19+
assert.Equal(t, "435ffceb7ba576c937e922766e37d4f7abdcc122", c.ID.String())
4520
})
4621

4722
t.Run("Summary", func(t *testing.T) {
@@ -84,7 +59,7 @@ func TestCommit_Parent(t *testing.T) {
8459
if err != nil {
8560
t.Fatal(err)
8661
}
87-
assert.Equal(t, test.expParentID, p.ID().String())
62+
assert.Equal(t, test.expParentID, p.ID.String())
8863
})
8964
}
9065
})
@@ -123,7 +98,7 @@ func TestCommit_CommitByPath(t *testing.T) {
12398
t.Fatal(err)
12499
}
125100

126-
assert.Equal(t, test.expCommitID, cc.ID().String())
101+
assert.Equal(t, test.expCommitID, cc.ID.String())
127102
})
128103
}
129104
}
@@ -132,7 +107,7 @@ func TestCommit_CommitByPath(t *testing.T) {
132107
func commitsToIDs(commits []*Commit) []string {
133108
ids := make([]string, len(commits))
134109
for i := range commits {
135-
ids[i] = commits[i].ID().String()
110+
ids[i] = commits[i].ID.String()
136111
}
137112
return ids
138113
}

repo_commit.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ loop:
4646
if err != nil {
4747
return nil, err
4848
}
49-
commit.author = sig
49+
commit.Author = sig
5050
case "committer":
5151
sig, err := parseSignature(line[spacepos+1:])
5252
if err != nil {
5353
return nil, err
5454
}
55-
commit.committer = sig
55+
commit.Committer = sig
5656
}
5757
nextline += eol + 1
5858
case eol == 0:
59-
commit.message = string(data[nextline+1:])
59+
commit.Message = string(data[nextline+1:])
6060
break loop
6161
default:
6262
break loop
@@ -102,7 +102,7 @@ func (r *Repository) CatFileCommit(rev string, opts ...CatFileCommitOptions) (*C
102102
return nil, err
103103
}
104104
c.repo = r
105-
c.id = MustIDFromString(commitID)
105+
c.ID = MustIDFromString(commitID)
106106

107107
r.cachedCommits.Set(commitID, c)
108108
return c, nil

repo_commit_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func TestRepository_CatFileCommit(t *testing.T) {
4949
t.Fatal(err)
5050
}
5151

52-
assert.Equal(t, "d58e3ef9f123eea6857161c79275ee22b228f659", c.ID().String())
53-
assert.Equal(t, "Add a symlink\n", c.Message())
52+
assert.Equal(t, "d58e3ef9f123eea6857161c79275ee22b228f659", c.ID.String())
53+
assert.Equal(t, "Add a symlink\n", c.Message)
5454
}
5555

5656
func TestRepository_BranchCommit(t *testing.T) {
@@ -65,8 +65,8 @@ func TestRepository_BranchCommit(t *testing.T) {
6565
t.Fatal(err)
6666
}
6767

68-
assert.Equal(t, "0eedd79eba4394bbef888c804e899731644367fe", c.ID().String())
69-
assert.Equal(t, "Rename shell script\n", c.Message())
68+
assert.Equal(t, "0eedd79eba4394bbef888c804e899731644367fe", c.ID.String())
69+
assert.Equal(t, "Rename shell script\n", c.Message)
7070
}
7171

7272
func TestRepository_TagCommit(t *testing.T) {
@@ -81,8 +81,8 @@ func TestRepository_TagCommit(t *testing.T) {
8181
t.Fatal(err)
8282
}
8383

84-
assert.Equal(t, "0eedd79eba4394bbef888c804e899731644367fe", c.ID().String())
85-
assert.Equal(t, "Rename shell script\n", c.Message())
84+
assert.Equal(t, "0eedd79eba4394bbef888c804e899731644367fe", c.ID.String())
85+
assert.Equal(t, "Rename shell script\n", c.Message)
8686
}
8787

8888
func TestRepository_Log(t *testing.T) {
@@ -152,7 +152,7 @@ func TestRepository_CommitByRevision(t *testing.T) {
152152
t.Fatal(err)
153153
}
154154

155-
assert.Equal(t, test.expID, c.ID().String())
155+
assert.Equal(t, test.expID, c.ID.String())
156156
})
157157
}
158158
}

repo_diff.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (r *Repository) Diff(rev string, maxFiles, maxFileLines, maxLineChars int,
4141
cmd.AddArgs("show", "--full-index", rev)
4242
} else {
4343
c, _ := commit.Parent(0)
44-
cmd.AddArgs("diff", "--full-index", "-M", c.id.String(), rev)
44+
cmd.AddArgs("diff", "--full-index", "-M", c.ID.String(), rev)
4545
}
4646
} else {
4747
cmd.AddArgs("diff", "--full-index", "-M", opt.Base, rev)
@@ -97,14 +97,14 @@ func (r *Repository) RawDiff(rev string, diffType RawDiffFormat, w io.Writer, op
9797
cmd.AddArgs("show", rev)
9898
} else {
9999
c, _ := commit.Parent(0)
100-
cmd.AddArgs("diff", "-M", c.id.String(), rev)
100+
cmd.AddArgs("diff", "-M", c.ID.String(), rev)
101101
}
102102
case RawDiffPatch:
103103
if commit.ParentsCount() == 0 {
104104
cmd.AddArgs("format-patch", "--no-signature", "--stdout", "--root", rev)
105105
} else {
106106
c, _ := commit.Parent(0)
107-
cmd.AddArgs("format-patch", "--no-signature", "--stdout", rev+"..."+c.id.String())
107+
cmd.AddArgs("format-patch", "--no-signature", "--stdout", rev+"..."+c.ID.String())
108108
}
109109
default:
110110
return fmt.Errorf("invalid diffType: %s", diffType)

repo_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,11 @@ func TestRepository_Commit(t *testing.T) {
374374
t.Fatal(err)
375375
}
376376

377-
assert.Equal(t, committer.Name, c.Committer().Name)
378-
assert.Equal(t, committer.Email, c.Committer().Email)
379-
assert.Equal(t, author.Name, c.Author().Name)
380-
assert.Equal(t, author.Email, c.Author().Email)
381-
assert.Equal(t, message+"\n", c.Message())
377+
assert.Equal(t, committer.Name, c.Committer.Name)
378+
assert.Equal(t, committer.Email, c.Committer.Email)
379+
assert.Equal(t, author.Name, c.Author.Name)
380+
assert.Equal(t, author.Email, c.Author.Email)
381+
assert.Equal(t, message+"\n", c.Message)
382382
}
383383

384384
func TestRepository_RevParse(t *testing.T) {

tag_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ func TestTag_Commit(t *testing.T) {
4141
t.Fatal(err)
4242
}
4343

44-
assert.Equal(t, "0eedd79eba4394bbef888c804e899731644367fe", c.ID().String())
44+
assert.Equal(t, "0eedd79eba4394bbef888c804e899731644367fe", c.ID.String())
4545
}

0 commit comments

Comments
 (0)