Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit b65d94e

Browse files
authored
Merge pull request #259 from smola/docs
Improve documentation
2 parents 431af32 + 84b6bd8 commit b65d94e

File tree

9 files changed

+110
-56
lines changed

9 files changed

+110
-56
lines changed

plumbing/object/blob.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"srcd.works/go-git.v4/utils/ioutil"
99
)
1010

11-
// Blob is used to store file data - it is generally a file.
11+
// Blob is used to store arbitrary data - it is generally a file.
1212
type Blob struct {
13+
// Hash of the blob.
1314
Hash plumbing.Hash
15+
// Size of the (uncompressed) blob.
1416
Size int64
1517

1618
obj plumbing.EncodedObject
@@ -26,6 +28,7 @@ func GetBlob(s storer.EncodedObjectStorer, h plumbing.Hash) (*Blob, error) {
2628
return DecodeBlob(o)
2729
}
2830

31+
// DecodeObject decodes an encoded object into a *Blob.
2932
func DecodeBlob(o plumbing.EncodedObject) (*Blob, error) {
3033
b := &Blob{}
3134
if err := b.Decode(o); err != nil {
@@ -91,16 +94,17 @@ type BlobIter struct {
9194
s storer.EncodedObjectStorer
9295
}
9396

94-
// NewBlobIter returns a BlobIter for the given repository and underlying
95-
// object iterator.
97+
// NewBlobIter takes a storer.EncodedObjectStorer and a
98+
// storer.EncodedObjectIter and returns a *BlobIter that iterates over all
99+
// blobs contained in the storer.EncodedObjectIter.
96100
//
97-
// The returned BlobIter will automatically skip over non-blob objects.
101+
// Any non-blob object returned by the storer.EncodedObjectIter is skipped.
98102
func NewBlobIter(s storer.EncodedObjectStorer, iter storer.EncodedObjectIter) *BlobIter {
99103
return &BlobIter{iter, s}
100104
}
101105

102-
// Next moves the iterator to the next blob and returns a pointer to it. If it
103-
// has reached the end of the set it will return io.EOF.
106+
// Next moves the iterator to the next blob and returns a pointer to it. If
107+
// there are no more blobs, it returns io.EOF.
104108
func (iter *BlobIter) Next() (*Blob, error) {
105109
for {
106110
obj, err := iter.EncodedObjectIter.Next()

plumbing/object/commit.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ type Hash plumbing.Hash
2222
// commit, a pointer to the previous commit(s), etc.
2323
// http://schacon.github.io/gitbook/1_the_git_object_model.html
2424
type Commit struct {
25-
Hash plumbing.Hash
26-
Author Signature
25+
// Hash of the commit object.
26+
Hash plumbing.Hash
27+
// Author is the original author of the commit.
28+
Author Signature
29+
// Committer is the one performing the commit, might be different from
30+
// Author.
2731
Committer Signature
28-
Message string
32+
// Message is the commit message, contains arbitrary text.
33+
Message string
2934

3035
tree plumbing.Hash
3136
parents []plumbing.Hash
@@ -53,12 +58,12 @@ func DecodeCommit(s storer.EncodedObjectStorer, o plumbing.EncodedObject) (*Comm
5358
return c, nil
5459
}
5560

56-
// Tree returns the Tree from the commit
61+
// Tree returns the Tree from the commit.
5762
func (c *Commit) Tree() (*Tree, error) {
5863
return GetTree(c.s, c.tree)
5964
}
6065

61-
// Parents return a CommitIter to the parent Commits
66+
// Parents return a CommitIter to the parent Commits.
6267
func (c *Commit) Parents() *CommitIter {
6368
return NewCommitIter(c.s,
6469
storer.NewEncodedObjectLookupIter(c.s, plumbing.CommitObject, c.parents),
@@ -158,7 +163,8 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
158163
}
159164
}
160165

161-
// History return a slice with the previous commits in the history of this commit
166+
// History returns a slice with the previous commits in the history of this
167+
// commit, sorted in reverse chronological order.
162168
func (c *Commit) History() ([]*Commit, error) {
163169
var commits []*Commit
164170
err := WalkCommitHistory(c, func(commit *Commit) error {
@@ -231,16 +237,17 @@ type CommitIter struct {
231237
s storer.EncodedObjectStorer
232238
}
233239

234-
// NewCommitIter returns a CommitIter for the given object storer and underlying
235-
// object iterator.
240+
// NewCommitIter takes a storer.EncodedObjectStorer and a
241+
// storer.EncodedObjectIter and returns a *CommitIter that iterates over all
242+
// commits contained in the storer.EncodedObjectIter.
236243
//
237-
// The returned CommitIter will automatically skip over non-commit objects.
244+
// Any non-commit object returned by the storer.EncodedObjectIter is skipped.
238245
func NewCommitIter(s storer.EncodedObjectStorer, iter storer.EncodedObjectIter) *CommitIter {
239246
return &CommitIter{iter, s}
240247
}
241248

242-
// Next moves the iterator to the next commit and returns a pointer to it. If it
243-
// has reached the end of the set it will return io.EOF.
249+
// Next moves the iterator to the next commit and returns a pointer to it. If
250+
// there are no more commits, it returns io.EOF.
244251
func (iter *CommitIter) Next() (*Commit, error) {
245252
obj, err := iter.EncodedObjectIter.Next()
246253
if err != nil {

plumbing/object/commit_walker.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ type commitWalker struct {
1313
cb func(*Commit) error
1414
}
1515

16-
// WalkCommitHistory walks the commit history
16+
// WalkCommitHistory walks the commit history, starting at the given commit and
17+
// visiting its parents in pre-order. The given callback will be called for each
18+
// visited commit. Each commit will be visited only once. If the callback returns
19+
// an error, walking will stop and will return the error. Other errors might be
20+
// returned if the history cannot be traversed (e.g. missing objects).
1721
func WalkCommitHistory(c *Commit, cb func(*Commit) error) error {
1822
w := &commitWalker{
1923
seen: make(map[plumbing.Hash]bool),

plumbing/object/file.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ import (
1212

1313
// File represents git file objects.
1414
type File struct {
15+
// Name is the path of the file. It might be relative to a tree,
16+
// depending of the function that generates it.
1517
Name string
18+
// Mode is the file mode.
1619
Mode os.FileMode
20+
// Blob with the contents of the file.
1721
Blob
1822
}
1923

@@ -56,15 +60,20 @@ func (f *File) Lines() ([]string, error) {
5660
return splits, nil
5761
}
5862

63+
// FileIter provides an iterator for the files in a tree.
5964
type FileIter struct {
6065
s storer.EncodedObjectStorer
6166
w TreeWalker
6267
}
6368

69+
// NewFileIter takes a storer.EncodedObjectStorer and a Tree and returns a
70+
// *FileIter that iterates over all files contained in the tree, recursively.
6471
func NewFileIter(s storer.EncodedObjectStorer, t *Tree) *FileIter {
6572
return &FileIter{s: s, w: *NewTreeWalker(t, true)}
6673
}
6774

75+
// Next moves the iterator to the next file and returns a pointer to it. If
76+
// there are no more files, it returns io.EOF.
6877
func (iter *FileIter) Next() (*File, error) {
6978
for {
7079
name, entry, err := iter.w.Next()

plumbing/object/object.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package object contains implementations of all Git objects and utility
2+
// functions to work with them.
13
package object
24

35
import (
@@ -35,8 +37,9 @@ var ErrUnsupportedObject = errors.New("unsupported object type")
3537
// }
3638
// }
3739
//
38-
// This interface is intentionally different from plumbing.EncodedObject, which is a lower
39-
// level interface used by storage implementations to read and write objects.
40+
// This interface is intentionally different from plumbing.EncodedObject, which
41+
// is a lower level interface used by storage implementations to read and write
42+
// objects in its encoded form.
4043
type Object interface {
4144
ID() plumbing.Hash
4245
Type() plumbing.ObjectType
@@ -74,11 +77,14 @@ func DecodeObject(s storer.EncodedObjectStorer, o plumbing.EncodedObject) (Objec
7477
// DateFormat is the format being used in the original git implementation
7578
const DateFormat = "Mon Jan 02 15:04:05 2006 -0700"
7679

77-
// Signature represents an action signed by a person
80+
// Signature is used to identify who and when created a commit or tag.
7881
type Signature struct {
79-
Name string
82+
// Name represents a person name. It is an arbitrary string.
83+
Name string
84+
// Email is an email, but it cannot be assumed to be well-formed.
8085
Email string
81-
When time.Time
86+
// When is the timestamp of the signature.
87+
When time.Time
8288
}
8389

8490
// Decode decodes a byte slice into a signature
@@ -155,14 +161,15 @@ type ObjectIter struct {
155161
s storer.EncodedObjectStorer
156162
}
157163

158-
// NewObjectIter returns a ObjectIter for the given repository and underlying
159-
// object iterator.
164+
// NewObjectIter takes a storer.EncodedObjectStorer and a
165+
// storer.EncodedObjectIter and returns an *ObjectIter that iterates over all
166+
// objects contained in the storer.EncodedObjectIter.
160167
func NewObjectIter(s storer.EncodedObjectStorer, iter storer.EncodedObjectIter) *ObjectIter {
161168
return &ObjectIter{iter, s}
162169
}
163170

164-
// Next moves the iterator to the next object and returns a pointer to it. If it
165-
// has reached the end of the set it will return io.EOF.
171+
// Next moves the iterator to the next object and returns a pointer to it. If
172+
// there are no more objects, it returns io.EOF.
166173
func (iter *ObjectIter) Next() (Object, error) {
167174
for {
168175
obj, err := iter.EncodedObjectIter.Next()

plumbing/object/tag.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ import (
1818
// contains meta-information about the tag, including the tagger, tag date and
1919
// message.
2020
//
21+
// Note that this is not used for lightweight tags.
22+
//
2123
// https://git-scm.com/book/en/v2/Git-Internals-Git-References#Tags
2224
type Tag struct {
23-
Hash plumbing.Hash
24-
Name string
25-
Tagger Signature
26-
Message string
25+
// Hash of the tag.
26+
Hash plumbing.Hash
27+
// Name of the tag.
28+
Name string
29+
// Tagger is the one who created the tag.
30+
Tagger Signature
31+
// Message is an arbitrary text message.
32+
Message string
33+
// TargetType is the object type of the target.
2734
TargetType plumbing.ObjectType
28-
Target plumbing.Hash
35+
// Target is the hash of the target object.
36+
Target plumbing.Hash
2937

3038
s storer.EncodedObjectStorer
3139
}
@@ -223,16 +231,17 @@ type TagIter struct {
223231
s storer.EncodedObjectStorer
224232
}
225233

226-
// NewTagIter returns a TagIter for the given object storer and underlying
227-
// object iterator.
234+
// NewTagIter takes a storer.EncodedObjectStorer and a
235+
// storer.EncodedObjectIter and returns a *TagIter that iterates over all
236+
// tags contained in the storer.EncodedObjectIter.
228237
//
229-
// The returned TagIter will automatically skip over non-tag objects.
238+
// Any non-tag object returned by the storer.EncodedObjectIter is skipped.
230239
func NewTagIter(s storer.EncodedObjectStorer, iter storer.EncodedObjectIter) *TagIter {
231240
return &TagIter{iter, s}
232241
}
233242

234-
// Next moves the iterator to the next tag and returns a pointer to it. If it
235-
// has reached the end of the set it will return io.EOF.
243+
// Next moves the iterator to the next tag and returns a pointer to it. If
244+
// there are no more tags, it returns io.EOF.
236245
func (iter *TagIter) Next() (*Tag, error) {
237246
obj, err := iter.EncodedObjectIter.Next()
238247
if err != nil {

plumbing/object/tree.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,16 +431,17 @@ type TreeIter struct {
431431
s storer.EncodedObjectStorer
432432
}
433433

434-
// NewTreeIter returns a TreeIter for the given repository and underlying
435-
// object iterator.
434+
// NewTreeIter takes a storer.EncodedObjectStorer and a
435+
// storer.EncodedObjectIter and returns a *TreeIter that iterates over all
436+
// tree contained in the storer.EncodedObjectIter.
436437
//
437-
// The returned TreeIter will automatically skip over non-tree objects.
438+
// Any non-tree object returned by the storer.EncodedObjectIter is skipped.
438439
func NewTreeIter(s storer.EncodedObjectStorer, iter storer.EncodedObjectIter) *TreeIter {
439440
return &TreeIter{iter, s}
440441
}
441442

442-
// Next moves the iterator to the next tree and returns a pointer to it. If it
443-
// has reached the end of the set it will return io.EOF.
443+
// Next moves the iterator to the next tree and returns a pointer to it. If
444+
// there are no more trees, it returns io.EOF.
444445
func (iter *TreeIter) Next() (*Tree, error) {
445446
for {
446447
obj, err := iter.EncodedObjectIter.Next()

utils/ioutil/common.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package ioutil implements some I/O utility functions.
12
package ioutil
23

34
import (
@@ -63,20 +64,9 @@ func WriteNopCloser(w io.Writer) io.WriteCloser {
6364
return writeNopCloser{w}
6465
}
6566

66-
// CheckClose is used with defer to close the given io.Closer and check its
67-
// returned error value. If Close returns an error and the given *error
68-
// is not nil, *error is set to the error returned by Close.
69-
//
70-
// CheckClose is typically used with named return values like so:
71-
//
72-
// func do(obj *Object) (err error) {
73-
// w, err := obj.Writer()
74-
// if err != nil {
75-
// return nil
76-
// }
77-
// defer CheckClose(w, &err)
78-
// // work with w
79-
// }
67+
// CheckClose calls Close on the given io.Closer. If the given *error points to
68+
// nil, it will be assigned the error returned by Close. Otherwise, any error
69+
// returned by Close will be ignored. CheckClose is usually called with defer.
8070
func CheckClose(c io.Closer, err *error) {
8171
if cerr := c.Close(); cerr != nil && *err == nil {
8272
*err = cerr

utils/ioutil/common_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ioutil
33
import (
44
"bytes"
55
"io/ioutil"
6+
"strings"
67
"testing"
78

89
. "gopkg.in/check.v1"
@@ -53,3 +54,25 @@ func (s *CommonSuite) TestNewReadCloser(c *C) {
5354
c.Assert(r.Close(), IsNil)
5455
c.Assert(closer.called, Equals, 1)
5556
}
57+
58+
func ExampleCheckClose() {
59+
// CheckClose is commonly used with named return values
60+
f := func() (err error) {
61+
// Get a io.ReadCloser
62+
r := ioutil.NopCloser(strings.NewReader("foo"))
63+
64+
// defer CheckClose call with an io.Closer and pointer to error
65+
defer CheckClose(r, &err)
66+
67+
// ... work with r ...
68+
69+
// if err is not nil, CheckClose will assign any close errors to it
70+
return err
71+
72+
}
73+
74+
err := f()
75+
if err != nil {
76+
panic(err)
77+
}
78+
}

0 commit comments

Comments
 (0)