Skip to content

Commit 157db58

Browse files
committed
Add some explanatory comments.
1 parent 006592d commit 157db58

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

subtrac.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import (
1313
"strings"
1414
)
1515

16+
// A Trac represents a commit or tree somewhere in the project's hierarchy,
17+
// including submodules. If one of its children contains submodules,
18+
// subHeads and tracCommit will be populated.
1619
type Trac struct {
17-
name string
18-
hash plumbing.Hash
19-
parents []*Trac
20-
subHeads []*Trac
21-
tracCommit *object.Commit
20+
name string // a human-readable path to this object
21+
hash plumbing.Hash // the git hash of this object
22+
parents []*Trac // parent commits (if this is a commit)
23+
subHeads []*Trac // submodule commits contained by this
24+
tracCommit *object.Commit // synthetic commit with parents+subHeads
2225
}
2326

2427
func (t Trac) String() string {
@@ -39,11 +42,11 @@ func (t Trac) String() string {
3942
type Cache struct {
4043
debugf func(fmt string, args ...interface{})
4144
infof func(fmt string, args ...interface{})
42-
repoDir string
43-
repo *git.Repository
44-
autoexclude bool
45-
excludes map[plumbing.Hash]bool
46-
tracs map[plumbing.Hash]*Trac
45+
repoDir string // toplevel repo dir
46+
repo *git.Repository // open copy of the toplevel repo
47+
autoexclude bool // --auto-exclude enabled
48+
excludes map[plumbing.Hash]bool // specifically excluded objects
49+
tracs map[plumbing.Hash]*Trac // object lookup cache
4750
}
4851

4952
func NewCache(rdir string, r *git.Repository, excludes []string,
@@ -82,13 +85,15 @@ func (c *Cache) String() string {
8285
return strings.Join(out, "\n")
8386
}
8487

88+
// Add one commit to the exclusion list.
8589
func (c *Cache) exclude(hash plumbing.Hash) {
8690
if !c.excludes[hash] {
8791
c.excludes[hash] = true
8892
c.infof("Excluding %v\n", hash)
8993
}
9094
}
9195

96+
// Load all branches into the cache, and update a .trac ref for each one.
9297
func (c *Cache) UpdateBranchRefs() error {
9398
branchIter, err := c.repo.Branches()
9499
if err != nil {
@@ -132,6 +137,7 @@ func (c *Cache) UpdateBranchRefs() error {
132137
return nil
133138
}
134139

140+
// Generate a synthetic commit for the given ref.
135141
func (c *Cache) TracByRef(refname string) (*object.Commit, error) {
136142
h, err := c.repo.ResolveRevision(plumbing.Revision(refname))
137143
if err != nil {
@@ -148,6 +154,12 @@ func (c *Cache) TracByRef(refname string) (*object.Commit, error) {
148154
return tc.tracCommit, nil
149155
}
150156

157+
// Starting at the given commit, load all its recursive parents and
158+
// submodule references into the cache, returning the cache entry.
159+
//
160+
// This doesn't update any references in the repo itself, it just returns a
161+
// new object representing the commit, including its synthetic trac commit.
162+
//
151163
// Mercifully, git's content-addressable storage means there are never
152164
// any cycles when traversing the commit+submodule hierarchy, although the
153165
// same sub-objects may occur many times at different points in the tree.
@@ -222,6 +234,7 @@ func (c *Cache) tracCommit(path string, commit *object.Commit) (*Trac, error) {
222234
return trac, nil
223235
}
224236

237+
// True if a and b are equal.
225238
func equalSubs(a, b []*Trac) bool {
226239
if len(a) != len(b) {
227240
return false
@@ -234,6 +247,9 @@ func equalSubs(a, b []*Trac) bool {
234247
return true
235248
}
236249

250+
// Given a list of parents and submodules for a given real git commit,
251+
// produce a synthetic trac commit that includes all parents and submodules,
252+
// but not the commit itself.
237253
func (c *Cache) newTracCommit(commit *object.Commit, tracs []*object.Commit, heads []*Trac) (*object.Commit, error) {
238254
var parents []plumbing.Hash
239255

@@ -283,6 +299,13 @@ func (c *Cache) newTracCommit(commit *object.Commit, tracs []*object.Commit, hea
283299
return tc, nil
284300
}
285301

302+
// Update a "commit path" which represents how we get to a given commit
303+
// from a starting point. So if the starting point is "master^2~25, and sub is
304+
// 1, the result is master^2~26. If sub is 3, the result is master^2~25^3, and
305+
// so on.
306+
//
307+
// These paths look weird but are valid git syntax, and are somewhat human-
308+
// friendly once you get used to them.
286309
func commitPath(path string, sub int) string {
287310
if sub != 1 {
288311
return fmt.Sprintf("%s^%d", path, sub)
@@ -298,6 +321,9 @@ func commitPath(path string, sub int) string {
298321
return fmt.Sprintf("%s~%d", path[:ix], v+1)
299322
}
300323

324+
// Try to find a given commit object in all submodule repositories. If it
325+
// exists, 'git fetch' it into the main repository so we can refer to it
326+
// as a parent of our synthetic commits.
301327
func (c *Cache) tryFetchFromSubmodules(path string, hash plumbing.Hash) error {
302328
c.infof("Searching submodules for: %v\n", path)
303329
wt, err := c.repo.Worktree()
@@ -360,6 +386,9 @@ func (c *Cache) tryFetchFromSubmodules(path string, hash plumbing.Hash) error {
360386
return fmt.Errorf("%v: %v not found.", path, hash)
361387
}
362388

389+
// Starting from a given git tree object, recursively add all its subtree
390+
// and submodules into the cache, returning the cache object representing
391+
// this tree.
363392
func (c *Cache) tracTree(path string, tree *object.Tree) (*Trac, error) {
364393
trac := c.tracs[tree.Hash]
365394
if trac != nil {
@@ -421,6 +450,7 @@ func (c *Cache) tracTree(path string, tree *object.Tree) (*Trac, error) {
421450
return trac, nil
422451
}
423452

453+
// Add a given entry into the cache.
424454
func (c *Cache) add(trac *Trac) {
425455
c.debugf(" add %.10v %v\n", trac.hash, trac.name)
426456
c.tracs[trac.hash] = trac

0 commit comments

Comments
 (0)