-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Miguel Molina <[email protected]>
- Loading branch information
1 parent
c6796b7
commit 5dc3885
Showing
31 changed files
with
316 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package gitbase | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha1" | ||
"encoding/base64" | ||
"io" | ||
|
||
git "gopkg.in/src-d/go-git.v4" | ||
"gopkg.in/src-d/go-git.v4/plumbing" | ||
) | ||
|
||
type checksumable struct { | ||
pool *RepositoryPool | ||
} | ||
|
||
func (c *checksumable) Checksum() (string, error) { | ||
hash := sha1.New() | ||
for _, id := range c.pool.idOrder { | ||
repo := c.pool.repositories[id] | ||
hash.Write([]byte(id)) | ||
|
||
bytes, err := readChecksum(repo) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if _, err = hash.Write(bytes); err != nil { | ||
return "", err | ||
} | ||
|
||
bytes, err = readRefs(repo) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if _, err = hash.Write(bytes); err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(hash.Sum(nil)), nil | ||
} | ||
|
||
func readChecksum(r repository) ([]byte, error) { | ||
fs, err := r.FS() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dot, packfiles, err := repositoryPackfiles(fs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result []byte | ||
for _, p := range packfiles { | ||
f, err := dot.ObjectPack(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if _, err = f.Seek(-20, io.SeekEnd); err != nil { | ||
return nil, err | ||
} | ||
|
||
var checksum = make([]byte, 20) | ||
if _, err = io.ReadFull(f, checksum); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = f.Close(); err != nil { | ||
return nil, err | ||
} | ||
|
||
result = append(result, checksum...) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func readRefs(r repository) ([]byte, error) { | ||
repo, err := r.Repo() | ||
if err != nil { | ||
if err == git.ErrRepositoryNotExists { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
|
||
buf := bytes.NewBuffer(nil) | ||
|
||
head, err := repo.Head() | ||
if err != nil && err != plumbing.ErrReferenceNotFound { | ||
return nil, err | ||
} else { | ||
buf.WriteString("HEAD") | ||
buf.WriteString(head.Hash().String()) | ||
} | ||
|
||
refs, err := repo.References() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = refs.ForEach(func(r *plumbing.Reference) error { | ||
buf.WriteString(string(r.Name())) | ||
buf.WriteString(r.Hash().String()) | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package gitbase | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
fixtures "gopkg.in/src-d/go-git-fixtures.v3" | ||
"gopkg.in/src-d/go-git.v4/plumbing/cache" | ||
) | ||
|
||
func TestChecksum(t *testing.T) { | ||
require := require.New(t) | ||
|
||
require.NoError(fixtures.Init()) | ||
defer func() { | ||
require.NoError(fixtures.Clean()) | ||
}() | ||
|
||
pool := NewRepositoryPool(cache.DefaultMaxSize) | ||
|
||
for i, f := range fixtures.ByTag("worktree") { | ||
path := f.Worktree().Root() | ||
require.NoError(pool.AddGitWithID(fmt.Sprintf("repo_%d", i), path)) | ||
} | ||
|
||
c := &checksumable{pool} | ||
checksum, err := c.Checksum() | ||
require.NoError(err) | ||
require.Equal("ogfv7HAwFigDgtuW4tbnEP+Zl40=", checksum) | ||
|
||
pool = NewRepositoryPool(cache.DefaultMaxSize) | ||
path := fixtures.ByTag("worktree").One().Worktree().Root() | ||
require.NoError(pool.AddGitWithID("worktree", path)) | ||
|
||
c = &checksumable{pool} | ||
checksum, err = c.Checksum() | ||
require.NoError(err) | ||
require.Equal("5kfLCygyBSZFMh+nFzFNk3zAUTQ=", checksum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.