| 1 | package lfs_test // to avoid import cycles |
| 2 | |
| 3 | // This is for doing complete git-level tests using test utils |
| 4 | // Needs to be a separate file from scanner_test so that we can use a diff package |
| 5 | // which avoids import cycles with testutils |
| 6 | |
| 7 | import ( |
| 8 | "sort" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/git-lfs/git-lfs/v3/config" |
| 13 | "github.com/git-lfs/git-lfs/v3/errors" |
| 14 | . "github.com/git-lfs/git-lfs/v3/lfs" |
| 15 | test "github.com/git-lfs/git-lfs/v3/t/cmd/util" |
| 16 | "github.com/stretchr/testify/assert" |
| 17 | ) |
| 18 | |
| 19 | func TestScanUnpushed(t *testing.T) { |
| 20 | repo := test.NewRepo(t) |
| 21 | repo.Pushd() |
| 22 | defer func() { |
| 23 | repo.Popd() |
| 24 | repo.Cleanup() |
| 25 | }() |
| 26 | |
| 27 | inputs := []*test.CommitInput{ |
| 28 | { // 0 |
| 29 | Files: []*test.FileInput{ |
| 30 | {Filename: "file1.txt", Size: 20}, |
| 31 | }, |
| 32 | }, |
| 33 | { // 1 |
| 34 | NewBranch: "branch2", |
| 35 | Files: []*test.FileInput{ |
| 36 | {Filename: "file1.txt", Size: 25}, |
| 37 | }, |
| 38 | }, |
| 39 | { // 2 |
| 40 | ParentBranches: []string{"master"}, // back on master |
| 41 | Files: []*test.FileInput{ |
| 42 | {Filename: "file1.txt", Size: 30}, |
| 43 | }, |
| 44 | }, |
| 45 | { // 3 |
| 46 | NewBranch: "branch3", |
| 47 | Files: []*test.FileInput{ |
| 48 | {Filename: "file1.txt", Size: 32}, |
| 49 | }, |
| 50 | }, |
| 51 | } |
| 52 | repo.AddCommits(inputs) |
| 53 | |
| 54 | // Add a couple of remotes and test state depending on what's pushed |
| 55 | repo.AddRemote("origin") |
| 56 | repo.AddRemote("upstream") |
| 57 | |
| 58 | pointers, err := scanUnpushed("") |
| 59 | assert.Nil(t, err, "Should be no error calling ScanUnpushed") |
| 60 | assert.Len(t, pointers, 4, "Should be 4 pointers because none pushed") |
| 61 | |
| 62 | test.RunGitCommand(t, true, "push", "origin", "branch2") |
| 63 | // Branch2 will have pushed 2 commits |
| 64 | pointers, err = scanUnpushed("") |
| 65 | assert.Nil(t, err, "Should be no error calling ScanUnpushed") |
| 66 | assert.Len(t, pointers, 2, "Should be 2 pointers") |
| 67 | |
| 68 | test.RunGitCommand(t, true, "push", "upstream", "master") |
| 69 | // Master pushes 1 more commit |
| 70 | pointers, err = scanUnpushed("") |
| 71 | assert.Nil(t, err, "Should be no error calling ScanUnpushed") |
| 72 | assert.Len(t, pointers, 1, "Should be 1 pointer") |
| 73 | |
| 74 | test.RunGitCommand(t, true, "push", "origin", "branch3") |
| 75 | // All pushed (somewhere) |
| 76 | pointers, err = scanUnpushed("") |
| 77 | assert.Nil(t, err, "Should be no error calling ScanUnpushed") |
| 78 | assert.Empty(t, pointers, "Should be 0 pointers unpushed") |
| 79 | |
| 80 | // Check origin |
| 81 | pointers, err = scanUnpushed("origin") |
| 82 | assert.Nil(t, err, "Should be no error calling ScanUnpushed") |
| 83 | assert.Empty(t, pointers, "Should be 0 pointers unpushed to origin") |
| 84 | |
| 85 | // Check upstream |
| 86 | pointers, err = scanUnpushed("upstream") |
| 87 | assert.Nil(t, err, "Should be no error calling ScanUnpushed") |
| 88 | assert.Len(t, pointers, 2, "Should be 2 pointers unpushed to upstream") |
| 89 | } |
| 90 | |
| 91 | func scanUnpushed(remoteName string) ([]*WrappedPointer, error) { |
| 92 | pointers := make([]*WrappedPointer, 0, 10) |
| 93 | var multiErr error |
| 94 | |
| 95 | gitscanner := NewGitScanner(config.New(), func(p *WrappedPointer, err error) { |
| 96 | if err != nil { |
| 97 | multiErr = errors.Join(multiErr, err) |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | pointers = append(pointers, p) |
| 102 | }) |
| 103 | |
| 104 | if err := gitscanner.ScanUnpushed(remoteName, nil); err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | |
| 108 | return pointers, multiErr |
| 109 | } |
| 110 | |
| 111 | func TestScanPreviousVersions(t *testing.T) { |
| 112 | repo := test.NewRepo(t) |
| 113 | repo.Pushd() |
| 114 | defer func() { |
| 115 | repo.Popd() |
| 116 | repo.Cleanup() |
| 117 | }() |
| 118 | |
| 119 | now := time.Now() |
| 120 | |
| 121 | inputs := []*test.CommitInput{ |
| 122 | { // 0 |
| 123 | CommitDate: now.AddDate(0, 0, -20), |
| 124 | Files: []*test.FileInput{ |
| 125 | {Filename: "file1.txt", Size: 20}, |
| 126 | {Filename: "file2.txt", Size: 30}, |
| 127 | {Filename: "folder/nested.txt", Size: 40}, |
| 128 | {Filename: "folder/nested2.txt", Size: 31}, |
| 129 | }, |
| 130 | }, |
| 131 | { // 1 |
| 132 | CommitDate: now.AddDate(0, 0, -10), |
| 133 | Files: []*test.FileInput{ |
| 134 | {Filename: "file2.txt", Size: 22}, |
| 135 | }, |
| 136 | }, |
| 137 | { // 2 |
| 138 | NewBranch: "excluded", |
| 139 | CommitDate: now.AddDate(0, 0, -6), |
| 140 | Files: []*test.FileInput{ |
| 141 | {Filename: "file2.txt", Size: 12}, |
| 142 | {Filename: "folder/nested2.txt", Size: 16}, |
| 143 | }, |
| 144 | }, |
| 145 | { // 3 |
| 146 | ParentBranches: []string{"master"}, |
| 147 | CommitDate: now.AddDate(0, 0, -4), |
| 148 | Files: []*test.FileInput{ |
| 149 | {Filename: "folder/nested.txt", Size: 42}, |
| 150 | {Filename: "folder/nested2.txt", Size: 6}, |
| 151 | }, |
| 152 | }, |
| 153 | { // 4 |
| 154 | Files: []*test.FileInput{ |
| 155 | {Filename: "folder/nested.txt", Size: 22}, |
| 156 | }, |
| 157 | }, |
| 158 | } |
| 159 | outputs := repo.AddCommits(inputs) |
| 160 | |
| 161 | // Previous commits excludes final state of each file, which is: |
| 162 | // file1.txt [0] (unchanged since first commit so excluded) |
| 163 | // file2.txt [1] (because [2] is on another branch so excluded) |
| 164 | // folder/nested.txt [4] (updated at last commit) |
| 165 | // folder/nested2.txt [3] |
| 166 | |
| 167 | // The only changes which will be included are changes prior to final state |
| 168 | // where the '-' side of the diff is inside the date range |
| 169 | |
| 170 | // 7 day limit excludes [0] commit, but includes state from that if there |
| 171 | // was a subsequent chang |
| 172 | pointers, err := scanPreviousVersions(t, "master", now.AddDate(0, 0, -7)) |
| 173 | assert.Equal(t, nil, err) |
| 174 | |
| 175 | // Includes the following 'before' state at commits: |
| 176 | // folder/nested.txt [-diff at 4, ie 3, -diff at 3 ie 0] |
| 177 | // folder/nested2.txt [-diff at 3 ie 0] |
| 178 | // others are either on diff branches, before this window, or unchanged |
| 179 | expected := []*WrappedPointer{ |
| 180 | {Name: "folder/nested.txt", Pointer: outputs[3].Files[0]}, |
| 181 | {Name: "folder/nested.txt", Pointer: outputs[0].Files[2]}, |
| 182 | {Name: "folder/nested2.txt", Pointer: outputs[0].Files[3]}, |
| 183 | } |
| 184 | // Need to sort to compare equality |
| 185 | sort.Sort(test.WrappedPointersByOid(expected)) |
| 186 | sort.Sort(test.WrappedPointersByOid(pointers)) |
| 187 | assert.Equal(t, expected, pointers) |
| 188 | } |
| 189 | |
| 190 | func scanPreviousVersions(t *testing.T, ref string, since time.Time) ([]*WrappedPointer, error) { |
| 191 | pointers := make([]*WrappedPointer, 0, 10) |
| 192 | gitscanner := NewGitScanner(config.New(), func(p *WrappedPointer, err error) { |
| 193 | if err != nil { |
| 194 | t.Error(err) |
| 195 | return |
| 196 | } |
| 197 | pointers = append(pointers, p) |
| 198 | }) |
| 199 | |
| 200 | err := gitscanner.ScanPreviousVersions(ref, since, nil) |
| 201 | return pointers, err |
| 202 | } |