2015-08-24 11:19:30 +00:00
|
|
|
package lfs_test // to avoid import cycles
|
|
|
|
|
|
|
|
// This is for doing complete git-level tests using test utils
|
|
|
|
// Needs to be a separate file from scanner_test so that we can use a diff package
|
|
|
|
// which avoids import cycles with testutils
|
|
|
|
|
|
|
|
import (
|
2016-11-29 16:40:50 +00:00
|
|
|
"fmt"
|
2015-08-24 14:15:16 +00:00
|
|
|
"sort"
|
2015-08-24 11:19:30 +00:00
|
|
|
"testing"
|
2015-08-24 14:15:16 +00:00
|
|
|
"time"
|
2015-08-24 11:19:30 +00:00
|
|
|
|
2016-11-15 17:01:18 +00:00
|
|
|
. "github.com/git-lfs/git-lfs/lfs"
|
|
|
|
"github.com/git-lfs/git-lfs/test"
|
2016-05-25 16:43:10 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-08-24 11:19:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestScanUnpushed(t *testing.T) {
|
|
|
|
repo := test.NewRepo(t)
|
|
|
|
repo.Pushd()
|
|
|
|
defer func() {
|
|
|
|
repo.Popd()
|
|
|
|
repo.Cleanup()
|
|
|
|
}()
|
|
|
|
|
|
|
|
inputs := []*test.CommitInput{
|
|
|
|
{ // 0
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 20},
|
2015-08-24 11:19:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // 1
|
|
|
|
NewBranch: "branch2",
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 25},
|
2015-08-24 11:19:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // 2
|
|
|
|
ParentBranches: []string{"master"}, // back on master
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 30},
|
2015-08-24 11:19:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // 3
|
|
|
|
NewBranch: "branch3",
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 32},
|
2015-08-24 11:19:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
repo.AddCommits(inputs)
|
|
|
|
|
|
|
|
// Add a couple of remotes and test state depending on what's pushed
|
|
|
|
repo.AddRemote("origin")
|
|
|
|
repo.AddRemote("upstream")
|
|
|
|
|
2016-11-18 00:36:38 +00:00
|
|
|
pointers, err := scanUnpushed("")
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.Nil(t, err, "Should be no error calling ScanUnpushed")
|
|
|
|
assert.Len(t, pointers, 4, "Should be 4 pointers because none pushed")
|
2015-08-24 11:19:30 +00:00
|
|
|
|
|
|
|
test.RunGitCommand(t, true, "push", "origin", "branch2")
|
|
|
|
// Branch2 will have pushed 2 commits
|
2016-11-18 00:36:38 +00:00
|
|
|
pointers, err = scanUnpushed("")
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.Nil(t, err, "Should be no error calling ScanUnpushed")
|
|
|
|
assert.Len(t, pointers, 2, "Should be 2 pointers")
|
2015-08-24 11:19:30 +00:00
|
|
|
|
|
|
|
test.RunGitCommand(t, true, "push", "upstream", "master")
|
|
|
|
// Master pushes 1 more commit
|
2016-11-18 00:36:38 +00:00
|
|
|
pointers, err = scanUnpushed("")
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.Nil(t, err, "Should be no error calling ScanUnpushed")
|
|
|
|
assert.Len(t, pointers, 1, "Should be 1 pointer")
|
2015-08-24 11:19:30 +00:00
|
|
|
|
|
|
|
test.RunGitCommand(t, true, "push", "origin", "branch3")
|
|
|
|
// All pushed (somewhere)
|
2016-11-18 00:36:38 +00:00
|
|
|
pointers, err = scanUnpushed("")
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.Nil(t, err, "Should be no error calling ScanUnpushed")
|
|
|
|
assert.Empty(t, pointers, "Should be 0 pointers unpushed")
|
2015-08-24 11:19:30 +00:00
|
|
|
|
2015-09-11 16:48:44 +00:00
|
|
|
// Check origin
|
2016-11-18 00:36:38 +00:00
|
|
|
pointers, err = scanUnpushed("origin")
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.Nil(t, err, "Should be no error calling ScanUnpushed")
|
|
|
|
assert.Empty(t, pointers, "Should be 0 pointers unpushed to origin")
|
2015-09-11 16:48:44 +00:00
|
|
|
|
|
|
|
// Check upstream
|
2016-11-18 00:36:38 +00:00
|
|
|
pointers, err = scanUnpushed("upstream")
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.Nil(t, err, "Should be no error calling ScanUnpushed")
|
|
|
|
assert.Len(t, pointers, 2, "Should be 2 pointers unpushed to upstream")
|
2015-08-24 11:19:30 +00:00
|
|
|
}
|
2015-08-24 14:15:16 +00:00
|
|
|
|
2016-11-18 00:36:38 +00:00
|
|
|
func scanUnpushed(remoteName string) ([]*WrappedPointer, error) {
|
|
|
|
pointers := make([]*WrappedPointer, 0, 10)
|
2016-11-29 16:40:50 +00:00
|
|
|
var multiErr error
|
|
|
|
|
|
|
|
gitscanner := NewGitScanner(func(p *WrappedPointer, err error) {
|
|
|
|
if err != nil {
|
|
|
|
if multiErr != nil {
|
|
|
|
multiErr = fmt.Errorf("%v\n%v", multiErr, err)
|
|
|
|
} else {
|
|
|
|
multiErr = err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-18 00:36:38 +00:00
|
|
|
pointers = append(pointers, p)
|
2016-11-29 16:40:50 +00:00
|
|
|
})
|
|
|
|
|
2016-11-29 17:11:23 +00:00
|
|
|
if err := gitscanner.ScanUnpushed(remoteName, nil); err != nil {
|
2016-11-29 16:40:50 +00:00
|
|
|
return nil, err
|
2016-11-18 00:36:38 +00:00
|
|
|
}
|
2016-11-29 16:40:50 +00:00
|
|
|
|
|
|
|
gitscanner.Close()
|
|
|
|
return pointers, multiErr
|
2016-11-18 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 14:15:16 +00:00
|
|
|
func TestScanPreviousVersions(t *testing.T) {
|
|
|
|
repo := test.NewRepo(t)
|
|
|
|
repo.Pushd()
|
|
|
|
defer func() {
|
|
|
|
repo.Popd()
|
|
|
|
repo.Cleanup()
|
|
|
|
}()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
inputs := []*test.CommitInput{
|
|
|
|
{ // 0
|
|
|
|
CommitDate: now.AddDate(0, 0, -20),
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 20},
|
|
|
|
{Filename: "file2.txt", Size: 30},
|
|
|
|
{Filename: "folder/nested.txt", Size: 40},
|
|
|
|
{Filename: "folder/nested2.txt", Size: 31},
|
2015-08-24 14:15:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // 1
|
|
|
|
CommitDate: now.AddDate(0, 0, -10),
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file2.txt", Size: 22},
|
2015-08-24 14:15:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // 2
|
|
|
|
NewBranch: "excluded",
|
|
|
|
CommitDate: now.AddDate(0, 0, -6),
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file2.txt", Size: 12},
|
|
|
|
{Filename: "folder/nested2.txt", Size: 16},
|
2015-08-24 14:15:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // 3
|
|
|
|
ParentBranches: []string{"master"},
|
|
|
|
CommitDate: now.AddDate(0, 0, -4),
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "folder/nested.txt", Size: 42},
|
|
|
|
{Filename: "folder/nested2.txt", Size: 6},
|
2015-08-24 14:15:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // 4
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "folder/nested.txt", Size: 22},
|
2015-08-24 14:15:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
outputs := repo.AddCommits(inputs)
|
|
|
|
|
|
|
|
// Previous commits excludes final state of each file, which is:
|
|
|
|
// file1.txt [0] (unchanged since first commit so excluded)
|
|
|
|
// file2.txt [1] (because [2] is on another branch so excluded)
|
|
|
|
// folder/nested.txt [4] (updated at last commit)
|
|
|
|
// folder/nested2.txt [3]
|
|
|
|
|
|
|
|
// The only changes which will be included are changes prior to final state
|
|
|
|
// where the '-' side of the diff is inside the date range
|
|
|
|
|
|
|
|
// 7 day limit excludes [0] commit, but includes state from that if there
|
2016-11-29 17:27:42 +00:00
|
|
|
// was a subsequent chang
|
|
|
|
pointers, err := scanPreviousVersions(t, "master", now.AddDate(0, 0, -7))
|
2015-08-24 14:15:16 +00:00
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
|
|
|
|
// Includes the following 'before' state at commits:
|
|
|
|
// folder/nested.txt [-diff at 4, ie 3, -diff at 3 ie 0]
|
|
|
|
// folder/nested2.txt [-diff at 3 ie 0]
|
|
|
|
// others are either on diff branches, before this window, or unchanged
|
|
|
|
expected := []*WrappedPointer{
|
2016-11-18 00:55:06 +00:00
|
|
|
{Name: "folder/nested.txt", Pointer: outputs[3].Files[0]},
|
|
|
|
{Name: "folder/nested.txt", Pointer: outputs[0].Files[2]},
|
|
|
|
{Name: "folder/nested2.txt", Pointer: outputs[0].Files[3]},
|
2015-08-24 14:15:16 +00:00
|
|
|
}
|
|
|
|
// Need to sort to compare equality
|
|
|
|
sort.Sort(test.WrappedPointersByOid(expected))
|
|
|
|
sort.Sort(test.WrappedPointersByOid(pointers))
|
|
|
|
assert.Equal(t, expected, pointers)
|
2016-11-18 00:36:38 +00:00
|
|
|
}
|
2015-08-24 14:15:16 +00:00
|
|
|
|
2016-11-29 17:27:42 +00:00
|
|
|
func scanPreviousVersions(t *testing.T, ref string, since time.Time) ([]*WrappedPointer, error) {
|
2016-11-18 00:36:38 +00:00
|
|
|
pointers := make([]*WrappedPointer, 0, 10)
|
2016-11-29 17:27:42 +00:00
|
|
|
gitscanner := NewGitScanner(func(p *WrappedPointer, err error) {
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-18 00:36:38 +00:00
|
|
|
pointers = append(pointers, p)
|
2016-11-29 17:27:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err := gitscanner.ScanPreviousVersions(ref, since, nil)
|
|
|
|
return pointers, err
|
2015-08-24 14:15:16 +00:00
|
|
|
}
|