|
| 1 | +package gitbase |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/sha1" |
| 6 | + "encoding/base64" |
| 7 | + "io" |
| 8 | + |
| 9 | + git "gopkg.in/src-d/go-git.v4" |
| 10 | + "gopkg.in/src-d/go-git.v4/plumbing" |
| 11 | +) |
| 12 | + |
| 13 | +type checksumable struct { |
| 14 | + pool *RepositoryPool |
| 15 | +} |
| 16 | + |
| 17 | +func (c *checksumable) Checksum() (string, error) { |
| 18 | + hash := sha1.New() |
| 19 | + for _, id := range c.pool.idOrder { |
| 20 | + repo := c.pool.repositories[id] |
| 21 | + hash.Write([]byte(id)) |
| 22 | + |
| 23 | + bytes, err := readChecksum(repo) |
| 24 | + if err != nil { |
| 25 | + return "", err |
| 26 | + } |
| 27 | + |
| 28 | + if _, err = hash.Write(bytes); err != nil { |
| 29 | + return "", err |
| 30 | + } |
| 31 | + |
| 32 | + bytes, err = readRefs(repo) |
| 33 | + if err != nil { |
| 34 | + return "", err |
| 35 | + } |
| 36 | + |
| 37 | + if _, err = hash.Write(bytes); err != nil { |
| 38 | + return "", err |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return base64.StdEncoding.EncodeToString(hash.Sum(nil)), nil |
| 43 | +} |
| 44 | + |
| 45 | +func readChecksum(r repository) ([]byte, error) { |
| 46 | + fs, err := r.FS() |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + dot, packfiles, err := repositoryPackfiles(fs) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + var result []byte |
| 57 | + for _, p := range packfiles { |
| 58 | + f, err := dot.ObjectPack(p) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + if _, err = f.Seek(-20, io.SeekEnd); err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + var checksum = make([]byte, 20) |
| 68 | + if _, err = io.ReadFull(f, checksum); err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + |
| 72 | + if err = f.Close(); err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + result = append(result, checksum...) |
| 77 | + } |
| 78 | + |
| 79 | + return result, nil |
| 80 | +} |
| 81 | + |
| 82 | +func readRefs(r repository) ([]byte, error) { |
| 83 | + repo, err := r.Repo() |
| 84 | + if err != nil { |
| 85 | + if err == git.ErrRepositoryNotExists { |
| 86 | + return nil, nil |
| 87 | + } |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + buf := bytes.NewBuffer(nil) |
| 92 | + |
| 93 | + head, err := repo.Head() |
| 94 | + if err != nil && err != plumbing.ErrReferenceNotFound { |
| 95 | + return nil, err |
| 96 | + } else { |
| 97 | + buf.WriteString("HEAD") |
| 98 | + buf.WriteString(head.Hash().String()) |
| 99 | + } |
| 100 | + |
| 101 | + refs, err := repo.References() |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + err = refs.ForEach(func(r *plumbing.Reference) error { |
| 107 | + buf.WriteString(string(r.Name())) |
| 108 | + buf.WriteString(r.Hash().String()) |
| 109 | + return nil |
| 110 | + }) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + return buf.Bytes(), nil |
| 116 | +} |
0 commit comments