|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "gopkg.in/src-d/go-git.v4/plumbing" |
| 7 | + "gopkg.in/src-d/go-git.v4/plumbing/filemode" |
| 8 | + "gopkg.in/src-d/go-git.v4/plumbing/object" |
| 9 | + "gopkg.in/src-d/go-git.v4/storage" |
| 10 | +) |
| 11 | + |
| 12 | +type objectWalker struct { |
| 13 | + Storer storage.Storer |
| 14 | + // seen is the set of objects seen in the repo. |
| 15 | + // seen map can become huge if walking over large |
| 16 | + // repos. Thus using struct{} as the value type. |
| 17 | + seen map[plumbing.Hash]struct{} |
| 18 | +} |
| 19 | + |
| 20 | +func newObjectWalker(s storage.Storer) *objectWalker { |
| 21 | + return &objectWalker{s, map[plumbing.Hash]struct{}{}} |
| 22 | +} |
| 23 | + |
| 24 | +// walkAllRefs walks all (hash) refererences from the repo. |
| 25 | +func (p *objectWalker) walkAllRefs() error { |
| 26 | + // Walk over all the references in the repo. |
| 27 | + it, err := p.Storer.IterReferences() |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + defer it.Close() |
| 32 | + err = it.ForEach(func(ref *plumbing.Reference) error { |
| 33 | + // Exit this iteration early for non-hash references. |
| 34 | + if ref.Type() != plumbing.HashReference { |
| 35 | + return nil |
| 36 | + } |
| 37 | + return p.walkObjectTree(ref.Hash()) |
| 38 | + }) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +func (p *objectWalker) isSeen(hash plumbing.Hash) bool { |
| 46 | + _, seen := p.seen[hash] |
| 47 | + return seen |
| 48 | +} |
| 49 | + |
| 50 | +func (p *objectWalker) add(hash plumbing.Hash) { |
| 51 | + p.seen[hash] = struct{}{} |
| 52 | +} |
| 53 | + |
| 54 | +// walkObjectTree walks over all objects and remembers references |
| 55 | +// to them in the objectWalker. This is used instead of the revlist |
| 56 | +// walks because memory usage is tight with huge repos. |
| 57 | +func (p *objectWalker) walkObjectTree(hash plumbing.Hash) error { |
| 58 | + // Check if we have already seen, and mark this object |
| 59 | + if p.isSeen(hash) { |
| 60 | + return nil |
| 61 | + } |
| 62 | + p.add(hash) |
| 63 | + // Fetch the object. |
| 64 | + obj, err := object.GetObject(p.Storer, hash) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("Getting object %s failed: %v", hash, err) |
| 67 | + } |
| 68 | + // Walk all children depending on object type. |
| 69 | + switch obj := obj.(type) { |
| 70 | + case *object.Commit: |
| 71 | + err = p.walkObjectTree(obj.TreeHash) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + for _, h := range obj.ParentHashes { |
| 76 | + err = p.walkObjectTree(h) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + } |
| 81 | + case *object.Tree: |
| 82 | + for i := range obj.Entries { |
| 83 | + // Shortcut for blob objects: |
| 84 | + // 'or' the lower bits of a mode and check that it |
| 85 | + // it matches a filemode.Executable. The type information |
| 86 | + // is in the higher bits, but this is the cleanest way |
| 87 | + // to handle plain files with different modes. |
| 88 | + // Other non-tree objects are somewhat rare, so they |
| 89 | + // are not special-cased. |
| 90 | + if obj.Entries[i].Mode|0755 == filemode.Executable { |
| 91 | + p.add(obj.Entries[i].Hash) |
| 92 | + continue |
| 93 | + } |
| 94 | + // Normal walk for sub-trees (and symlinks etc). |
| 95 | + err = p.walkObjectTree(obj.Entries[i].Hash) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + } |
| 100 | + default: |
| 101 | + // Error out on unhandled object types. |
| 102 | + return fmt.Errorf("Unknown object %X %s %T\n", obj.ID(), obj.Type(), obj) |
| 103 | + } |
| 104 | + return nil |
| 105 | +} |
0 commit comments