2015-08-19 16:23:05 +00:00
|
|
|
package git_test // to avoid import cycles
|
|
|
|
|
|
|
|
import (
|
2015-08-21 15:53:16 +00:00
|
|
|
"sort"
|
2015-08-19 16:23:05 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/github/git-lfs/git"
|
|
|
|
"github.com/github/git-lfs/test"
|
|
|
|
"github.com/github/git-lfs/vendor/_nuts/github.com/technoweenie/assert"
|
|
|
|
)
|
|
|
|
|
2015-08-20 14:36:10 +00:00
|
|
|
func TestCurrentRefAndCurrentRemoteRef(t *testing.T) {
|
|
|
|
repo := test.NewRepo(t)
|
|
|
|
repo.Pushd()
|
|
|
|
defer func() {
|
|
|
|
repo.Popd()
|
|
|
|
repo.Cleanup()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// test commits; we'll just modify the same file each time since we're
|
|
|
|
// only interested in branches
|
|
|
|
inputs := []*test.CommitInput{
|
|
|
|
{ // 0
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 20},
|
2015-08-20 14:36:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // 1
|
|
|
|
NewBranch: "branch2",
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 25},
|
2015-08-20 14:36:10 +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-20 14:36:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // 3
|
|
|
|
NewBranch: "branch3",
|
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 32},
|
2015-08-20 14:36:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
outputs := repo.AddCommits(inputs)
|
|
|
|
// last commit was on branch3
|
|
|
|
ref, err := CurrentRef()
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, &Ref{"branch3", RefTypeLocalBranch, outputs[3].Sha}, ref)
|
|
|
|
test.RunGitCommand(t, true, "checkout", "master")
|
|
|
|
ref, err = CurrentRef()
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, &Ref{"master", RefTypeLocalBranch, outputs[2].Sha}, ref)
|
|
|
|
// Check remote
|
|
|
|
repo.AddRemote("origin")
|
|
|
|
test.RunGitCommand(t, true, "push", "-u", "origin", "master")
|
|
|
|
ref, err = CurrentRemoteRef()
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, &Ref{"origin/master", RefTypeRemoteBranch, outputs[2].Sha}, ref)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-19 16:23:05 +00:00
|
|
|
func TestRecentBranches(t *testing.T) {
|
2015-08-19 16:33:13 +00:00
|
|
|
repo := test.NewRepo(t)
|
2015-08-20 09:46:08 +00:00
|
|
|
repo.Pushd()
|
2015-08-19 16:23:05 +00:00
|
|
|
defer func() {
|
2015-08-20 09:46:08 +00:00
|
|
|
repo.Popd()
|
|
|
|
repo.Cleanup()
|
2015-08-19 16:23:05 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
// test commits; we'll just modify the same file each time since we're
|
2015-08-19 16:33:13 +00:00
|
|
|
// only interested in branches & dates
|
|
|
|
inputs := []*test.CommitInput{
|
2015-08-20 13:53:39 +00:00
|
|
|
{ // 0
|
2015-08-19 16:23:05 +00:00
|
|
|
CommitDate: now.AddDate(0, 0, -20),
|
2015-08-19 16:33:13 +00:00
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 20},
|
2015-08-19 16:23:05 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 13:53:39 +00:00
|
|
|
{ // 1
|
2015-08-19 16:23:05 +00:00
|
|
|
CommitDate: now.AddDate(0, 0, -15),
|
|
|
|
NewBranch: "excluded_branch", // new branch & tag but too old
|
|
|
|
Tags: []string{"excluded_tag"},
|
2015-08-19 16:33:13 +00:00
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 25},
|
2015-08-19 16:23:05 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 13:53:39 +00:00
|
|
|
{ // 2
|
2015-08-19 16:23:05 +00:00
|
|
|
CommitDate: now.AddDate(0, 0, -12),
|
|
|
|
ParentBranches: []string{"master"}, // back on master
|
2015-08-19 16:33:13 +00:00
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 30},
|
2015-08-19 16:23:05 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 13:53:39 +00:00
|
|
|
{ // 3
|
2015-08-19 16:23:05 +00:00
|
|
|
CommitDate: now.AddDate(0, 0, -6),
|
|
|
|
NewBranch: "included_branch", // new branch within 7 day limit
|
2015-08-19 16:33:13 +00:00
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 32},
|
2015-08-19 16:23:05 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 13:53:39 +00:00
|
|
|
{ // 4
|
2015-08-19 16:23:05 +00:00
|
|
|
CommitDate: now.AddDate(0, 0, -3),
|
|
|
|
NewBranch: "included_branch_2", // new branch within 7 day limit
|
2015-08-19 16:33:13 +00:00
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 36},
|
2015-08-19 16:23:05 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 13:53:39 +00:00
|
|
|
{ // 5
|
2015-08-19 16:23:05 +00:00
|
|
|
// Final commit, current date/time
|
|
|
|
ParentBranches: []string{"master"}, // back on master
|
2015-08-19 16:33:13 +00:00
|
|
|
Files: []*test.FileInput{
|
2015-08-25 16:14:48 +00:00
|
|
|
{Filename: "file1.txt", Size: 21},
|
2015-08-19 16:23:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-08-20 09:46:08 +00:00
|
|
|
outputs := repo.AddCommits(inputs)
|
2015-08-19 16:23:05 +00:00
|
|
|
|
2015-08-20 11:05:44 +00:00
|
|
|
// Add a couple of remotes and push some branches
|
|
|
|
repo.AddRemote("origin")
|
|
|
|
repo.AddRemote("upstream")
|
|
|
|
|
|
|
|
test.RunGitCommand(t, true, "push", "origin", "master")
|
|
|
|
test.RunGitCommand(t, true, "push", "origin", "excluded_branch")
|
|
|
|
test.RunGitCommand(t, true, "push", "origin", "included_branch")
|
|
|
|
test.RunGitCommand(t, true, "push", "upstream", "master")
|
|
|
|
test.RunGitCommand(t, true, "push", "upstream", "included_branch_2")
|
|
|
|
|
|
|
|
// Recent, local only
|
2015-08-19 16:23:05 +00:00
|
|
|
refs, err := RecentBranches(now.AddDate(0, 0, -7), false, "")
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
expectedRefs := []*Ref{
|
|
|
|
&Ref{"master", RefTypeLocalBranch, outputs[5].Sha},
|
|
|
|
&Ref{"included_branch_2", RefTypeLocalBranch, outputs[4].Sha},
|
|
|
|
&Ref{"included_branch", RefTypeLocalBranch, outputs[3].Sha},
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedRefs, refs, "Refs should be correct")
|
|
|
|
|
2015-08-20 11:05:44 +00:00
|
|
|
// Recent, remotes too (all of them)
|
|
|
|
refs, err = RecentBranches(now.AddDate(0, 0, -7), true, "")
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
expectedRefs = []*Ref{
|
|
|
|
&Ref{"master", RefTypeLocalBranch, outputs[5].Sha},
|
|
|
|
&Ref{"included_branch_2", RefTypeLocalBranch, outputs[4].Sha},
|
|
|
|
&Ref{"included_branch", RefTypeLocalBranch, outputs[3].Sha},
|
2015-08-21 15:53:16 +00:00
|
|
|
&Ref{"upstream/master", RefTypeRemoteBranch, outputs[5].Sha},
|
|
|
|
&Ref{"upstream/included_branch_2", RefTypeRemoteBranch, outputs[4].Sha},
|
|
|
|
&Ref{"origin/master", RefTypeRemoteBranch, outputs[5].Sha},
|
2015-08-20 11:05:44 +00:00
|
|
|
&Ref{"origin/included_branch", RefTypeRemoteBranch, outputs[3].Sha},
|
|
|
|
}
|
2015-08-21 15:53:16 +00:00
|
|
|
// Need to sort for consistent comparison
|
|
|
|
sort.Sort(test.RefsByName(expectedRefs))
|
|
|
|
sort.Sort(test.RefsByName(refs))
|
2015-08-20 11:05:44 +00:00
|
|
|
assert.Equal(t, expectedRefs, refs, "Refs should be correct")
|
|
|
|
|
|
|
|
// Recent, only single remote
|
|
|
|
refs, err = RecentBranches(now.AddDate(0, 0, -7), true, "origin")
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
expectedRefs = []*Ref{
|
|
|
|
&Ref{"master", RefTypeLocalBranch, outputs[5].Sha},
|
|
|
|
&Ref{"origin/master", RefTypeRemoteBranch, outputs[5].Sha},
|
|
|
|
&Ref{"included_branch_2", RefTypeLocalBranch, outputs[4].Sha},
|
|
|
|
&Ref{"included_branch", RefTypeLocalBranch, outputs[3].Sha},
|
|
|
|
&Ref{"origin/included_branch", RefTypeRemoteBranch, outputs[3].Sha},
|
|
|
|
}
|
2015-08-21 15:53:16 +00:00
|
|
|
// Need to sort for consistent comparison
|
|
|
|
sort.Sort(test.RefsByName(expectedRefs))
|
|
|
|
sort.Sort(test.RefsByName(refs))
|
2015-08-20 11:05:44 +00:00
|
|
|
assert.Equal(t, expectedRefs, refs, "Refs should be correct")
|
2015-08-19 16:23:05 +00:00
|
|
|
}
|
2015-09-17 16:43:42 +00:00
|
|
|
|
|
|
|
func TestResolveEmptyCurrentRef(t *testing.T) {
|
|
|
|
repo := test.NewRepo(t)
|
|
|
|
repo.Pushd()
|
|
|
|
defer func() {
|
|
|
|
repo.Popd()
|
|
|
|
repo.Cleanup()
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, err := CurrentRef()
|
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
}
|