| 1 | package git_test // to avoid import cycles |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "sort" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | . "github.com/git-lfs/git-lfs/v3/git" |
| 11 | test "github.com/git-lfs/git-lfs/v3/t/cmd/util" |
| 12 | "github.com/git-lfs/git-lfs/v3/tools" |
| 13 | "github.com/stretchr/testify/assert" |
| 14 | ) |
| 15 | |
| 16 | func TestRefString(t *testing.T) { |
| 17 | const sha = "0000000000000000000000000000000000000000" |
| 18 | for s, r := range map[string]*Ref{ |
| 19 | "refs/heads/master": { |
| 20 | Name: "master", |
| 21 | Type: RefTypeLocalBranch, |
| 22 | Sha: sha, |
| 23 | }, |
| 24 | "refs/remotes/origin/master": { |
| 25 | Name: "origin/master", |
| 26 | Type: RefTypeRemoteBranch, |
| 27 | Sha: sha, |
| 28 | }, |
| 29 | "refs/tags/v1.0.0": { |
| 30 | Name: "v1.0.0", |
| 31 | Type: RefTypeLocalTag, |
| 32 | Sha: sha, |
| 33 | }, |
| 34 | "HEAD": { |
| 35 | Name: "HEAD", |
| 36 | Type: RefTypeHEAD, |
| 37 | Sha: sha, |
| 38 | }, |
| 39 | "other": { |
| 40 | Name: "other", |
| 41 | Type: RefTypeOther, |
| 42 | Sha: sha, |
| 43 | }, |
| 44 | } { |
| 45 | assert.Equal(t, s, r.Refspec()) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestParseRefs(t *testing.T) { |
| 50 | tests := map[string]RefType{ |
| 51 | "refs/heads": RefTypeLocalBranch, |
| 52 | "refs/tags": RefTypeLocalTag, |
| 53 | "refs/remotes": RefTypeRemoteBranch, |
| 54 | } |
| 55 | |
| 56 | for prefix, expectedType := range tests { |
| 57 | r := ParseRef(prefix+"/branch", "abc123") |
| 58 | assert.Equal(t, "abc123", r.Sha, "prefix: "+prefix) |
| 59 | assert.Equal(t, "branch", r.Name, "prefix: "+prefix) |
| 60 | assert.Equal(t, expectedType, r.Type, "prefix: "+prefix) |
| 61 | } |
| 62 | |
| 63 | r := ParseRef("refs/foo/branch", "abc123") |
| 64 | assert.Equal(t, "abc123", r.Sha, "prefix: refs/foo") |
| 65 | assert.Equal(t, "refs/foo/branch", r.Name, "prefix: refs/foo") |
| 66 | assert.Equal(t, RefTypeOther, r.Type, "prefix: refs/foo") |
| 67 | |
| 68 | r = ParseRef("HEAD", "abc123") |
| 69 | assert.Equal(t, "abc123", r.Sha, "prefix: HEAD") |
| 70 | assert.Equal(t, "HEAD", r.Name, "prefix: HEAD") |
| 71 | assert.Equal(t, RefTypeHEAD, r.Type, "prefix: HEAD") |
| 72 | } |
| 73 | |
| 74 | func TestCurrentRefAndCurrentRemoteRef(t *testing.T) { |
| 75 | repo := test.NewRepo(t) |
| 76 | repo.Pushd() |
| 77 | defer func() { |
| 78 | repo.Popd() |
| 79 | repo.Cleanup() |
| 80 | }() |
| 81 | |
| 82 | // test commits; we'll just modify the same file each time since we're |
| 83 | // only interested in branches |
| 84 | inputs := []*test.CommitInput{ |
| 85 | { // 0 |
| 86 | Files: []*test.FileInput{ |
| 87 | {Filename: "file1.txt", Size: 20}, |
| 88 | }, |
| 89 | }, |
| 90 | { // 1 |
| 91 | NewBranch: "branch2", |
| 92 | Files: []*test.FileInput{ |
| 93 | {Filename: "file1.txt", Size: 25}, |
| 94 | }, |
| 95 | }, |
| 96 | { // 2 |
| 97 | ParentBranches: []string{"master"}, // back on master |
| 98 | Files: []*test.FileInput{ |
| 99 | {Filename: "file1.txt", Size: 30}, |
| 100 | }, |
| 101 | }, |
| 102 | { // 3 |
| 103 | NewBranch: "branch3", |
| 104 | Files: []*test.FileInput{ |
| 105 | {Filename: "file1.txt", Size: 32}, |
| 106 | }, |
| 107 | }, |
| 108 | } |
| 109 | |
| 110 | outputs := repo.AddCommits(inputs) |
| 111 | |
| 112 | // last commit was on branch3 |
| 113 | gitConf := repo.GitConfig() |
| 114 | ref, err := CurrentRef() |
| 115 | assert.Nil(t, err) |
| 116 | assert.Equal(t, &Ref{ |
| 117 | Name: "branch3", |
| 118 | Type: RefTypeLocalBranch, |
| 119 | Sha: outputs[3].Sha, |
| 120 | }, ref) |
| 121 | test.RunGitCommand(t, true, "checkout", "master") |
| 122 | ref, err = CurrentRef() |
| 123 | assert.Nil(t, err) |
| 124 | assert.Equal(t, &Ref{ |
| 125 | Name: "master", |
| 126 | Type: RefTypeLocalBranch, |
| 127 | Sha: outputs[2].Sha, |
| 128 | }, ref) |
| 129 | // Check remote |
| 130 | repo.AddRemote("origin") |
| 131 | test.RunGitCommand(t, true, "push", "-u", "origin", "master:someremotebranch") |
| 132 | ref, err = gitConf.CurrentRemoteRef() |
| 133 | assert.Nil(t, err) |
| 134 | assert.Equal(t, &Ref{ |
| 135 | Name: "origin/someremotebranch", |
| 136 | Type: RefTypeRemoteBranch, |
| 137 | Sha: outputs[2].Sha, |
| 138 | }, ref) |
| 139 | |
| 140 | refname, err := gitConf.RemoteRefNameForCurrentBranch() |
| 141 | assert.Nil(t, err) |
| 142 | assert.Equal(t, "refs/remotes/origin/someremotebranch", refname) |
| 143 | |
| 144 | ref, err = ResolveRef(outputs[2].Sha) |
| 145 | assert.Nil(t, err) |
| 146 | assert.Equal(t, &Ref{ |
| 147 | Name: outputs[2].Sha, |
| 148 | Type: RefTypeOther, |
| 149 | Sha: outputs[2].Sha, |
| 150 | }, ref) |
| 151 | } |
| 152 | |
| 153 | func TestRecentBranches(t *testing.T) { |
| 154 | repo := test.NewRepo(t) |
| 155 | repo.Pushd() |
| 156 | defer func() { |
| 157 | repo.Popd() |
| 158 | repo.Cleanup() |
| 159 | }() |
| 160 | |
| 161 | now := time.Now() |
| 162 | // test commits; we'll just modify the same file each time since we're |
| 163 | // only interested in branches & dates |
| 164 | inputs := []*test.CommitInput{ |
| 165 | { // 0 |
| 166 | CommitDate: now.AddDate(0, 0, -20), |
| 167 | Files: []*test.FileInput{ |
| 168 | {Filename: "file1.txt", Size: 20}, |
| 169 | }, |
| 170 | }, |
| 171 | { // 1 |
| 172 | CommitDate: now.AddDate(0, 0, -15), |
| 173 | NewBranch: "excluded_branch", // new branch & tag but too old |
| 174 | Tags: []string{"excluded_tag"}, |
| 175 | Files: []*test.FileInput{ |
| 176 | {Filename: "file1.txt", Size: 25}, |
| 177 | }, |
| 178 | }, |
| 179 | { // 2 |
| 180 | CommitDate: now.AddDate(0, 0, -12), |
| 181 | ParentBranches: []string{"master"}, // back on master |
| 182 | Files: []*test.FileInput{ |
| 183 | {Filename: "file1.txt", Size: 30}, |
| 184 | }, |
| 185 | }, |
| 186 | { // 3 |
| 187 | CommitDate: now.AddDate(0, 0, -6), |
| 188 | NewBranch: "included_branch", // new branch within 7 day limit |
| 189 | Files: []*test.FileInput{ |
| 190 | {Filename: "file1.txt", Size: 32}, |
| 191 | }, |
| 192 | }, |
| 193 | { // 4 |
| 194 | CommitDate: now.AddDate(0, 0, -3), |
| 195 | NewBranch: "included_branch_2", // new branch within 7 day limit |
| 196 | Files: []*test.FileInput{ |
| 197 | {Filename: "file1.txt", Size: 36}, |
| 198 | }, |
| 199 | }, |
| 200 | { // 5 |
| 201 | // Final commit, current date/time |
| 202 | ParentBranches: []string{"master"}, // back on master |
| 203 | Files: []*test.FileInput{ |
| 204 | {Filename: "file1.txt", Size: 21}, |
| 205 | }, |
| 206 | }, |
| 207 | } |
| 208 | outputs := repo.AddCommits(inputs) |
| 209 | |
| 210 | // Add a couple of remotes and push some branches |
| 211 | repo.AddRemote("origin") |
| 212 | repo.AddRemote("upstream") |
| 213 | |
| 214 | test.RunGitCommand(t, true, "push", "origin", "master") |
| 215 | test.RunGitCommand(t, true, "push", "origin", "excluded_branch") |
| 216 | test.RunGitCommand(t, true, "push", "origin", "included_branch") |
| 217 | test.RunGitCommand(t, true, "push", "upstream", "master") |
| 218 | test.RunGitCommand(t, true, "push", "upstream", "included_branch_2") |
| 219 | |
| 220 | // Recent, local only |
| 221 | refs, err := RecentBranches(now.AddDate(0, 0, -7), false, "") |
| 222 | assert.Equal(t, nil, err) |
| 223 | expectedRefs := []*Ref{ |
| 224 | { |
| 225 | Name: "master", |
| 226 | Type: RefTypeLocalBranch, |
| 227 | Sha: outputs[5].Sha, |
| 228 | }, |
| 229 | { |
| 230 | Name: "included_branch_2", |
| 231 | Type: RefTypeLocalBranch, |
| 232 | Sha: outputs[4].Sha, |
| 233 | }, |
| 234 | { |
| 235 | Name: "included_branch", |
| 236 | Type: RefTypeLocalBranch, |
| 237 | Sha: outputs[3].Sha, |
| 238 | }, |
| 239 | } |
| 240 | assert.Equal(t, expectedRefs, refs, "Refs should be correct") |
| 241 | |
| 242 | // Recent, remotes too (all of them) |
| 243 | refs, err = RecentBranches(now.AddDate(0, 0, -7), true, "") |
| 244 | assert.Equal(t, nil, err) |
| 245 | expectedRefs = []*Ref{ |
| 246 | { |
| 247 | Name: "master", |
| 248 | Type: RefTypeLocalBranch, |
| 249 | Sha: outputs[5].Sha, |
| 250 | }, |
| 251 | { |
| 252 | Name: "included_branch_2", |
| 253 | Type: RefTypeLocalBranch, |
| 254 | Sha: outputs[4].Sha, |
| 255 | }, |
| 256 | { |
| 257 | Name: "included_branch", |
| 258 | Type: RefTypeLocalBranch, |
| 259 | Sha: outputs[3].Sha, |
| 260 | }, |
| 261 | { |
| 262 | Name: "upstream/master", |
| 263 | Type: RefTypeRemoteBranch, |
| 264 | Sha: outputs[5].Sha, |
| 265 | }, |
| 266 | { |
| 267 | Name: "upstream/included_branch_2", |
| 268 | Type: RefTypeRemoteBranch, |
| 269 | Sha: outputs[4].Sha, |
| 270 | }, |
| 271 | { |
| 272 | Name: "origin/master", |
| 273 | Type: RefTypeRemoteBranch, |
| 274 | Sha: outputs[5].Sha, |
| 275 | }, |
| 276 | { |
| 277 | Name: "origin/included_branch", |
| 278 | Type: RefTypeRemoteBranch, |
| 279 | Sha: outputs[3].Sha, |
| 280 | }, |
| 281 | } |
| 282 | // Need to sort for consistent comparison |
| 283 | sort.Sort(test.RefsByName(expectedRefs)) |
| 284 | sort.Sort(test.RefsByName(refs)) |
| 285 | assert.Equal(t, expectedRefs, refs, "Refs should be correct") |
| 286 | |
| 287 | // Recent, only single remote |
| 288 | refs, err = RecentBranches(now.AddDate(0, 0, -7), true, "origin") |
| 289 | assert.Equal(t, nil, err) |
| 290 | expectedRefs = []*Ref{ |
| 291 | { |
| 292 | Name: "master", |
| 293 | Type: RefTypeLocalBranch, |
| 294 | Sha: outputs[5].Sha, |
| 295 | }, |
| 296 | { |
| 297 | Name: "origin/master", |
| 298 | Type: RefTypeRemoteBranch, |
| 299 | Sha: outputs[5].Sha, |
| 300 | }, |
| 301 | { |
| 302 | Name: "included_branch_2", |
| 303 | Type: RefTypeLocalBranch, |
| 304 | Sha: outputs[4].Sha, |
| 305 | }, |
| 306 | { |
| 307 | Name: "included_branch", |
| 308 | Type: RefTypeLocalBranch, |
| 309 | Sha: outputs[3].Sha, |
| 310 | }, |
| 311 | { |
| 312 | Name: "origin/included_branch", |
| 313 | Type: RefTypeRemoteBranch, |
| 314 | Sha: outputs[3].Sha, |
| 315 | }, |
| 316 | } |
| 317 | // Need to sort for consistent comparison |
| 318 | sort.Sort(test.RefsByName(expectedRefs)) |
| 319 | sort.Sort(test.RefsByName(refs)) |
| 320 | assert.Equal(t, expectedRefs, refs, "Refs should be correct") |
| 321 | } |
| 322 | |
| 323 | func TestResolveEmptyCurrentRef(t *testing.T) { |
| 324 | repo := test.NewRepo(t) |
| 325 | repo.Pushd() |
| 326 | defer func() { |
| 327 | repo.Popd() |
| 328 | repo.Cleanup() |
| 329 | }() |
| 330 | |
| 331 | _, err := CurrentRef() |
| 332 | assert.NotEqual(t, nil, err) |
| 333 | } |
| 334 | |
| 335 | func TestWorktrees(t *testing.T) { |
| 336 | // Only git 2.5+ |
| 337 | if !IsGitVersionAtLeast("2.5.0") { |
| 338 | return |
| 339 | } |
| 340 | |
| 341 | repo := test.NewRepo(t) |
| 342 | repo.Pushd() |
| 343 | defer func() { |
| 344 | repo.Popd() |
| 345 | repo.Cleanup() |
| 346 | }() |
| 347 | |
| 348 | repoDir, _ := tools.CanonicalizePath(repo.Path, true) |
| 349 | |
| 350 | // test commits; we'll just modify the same file each time since we're |
| 351 | // only interested in branches & dates |
| 352 | inputs := []*test.CommitInput{ |
| 353 | { // 0 |
| 354 | Files: []*test.FileInput{ |
| 355 | {Filename: "file1.txt", Size: 10}, |
| 356 | }, |
| 357 | Tags: []string{"tag1"}, |
| 358 | }, |
| 359 | { // 1 |
| 360 | Files: []*test.FileInput{ |
| 361 | {Filename: "file1.txt", Size: 20}, |
| 362 | }, |
| 363 | }, |
| 364 | { // 2 |
| 365 | NewBranch: "branch2", |
| 366 | Files: []*test.FileInput{ |
| 367 | {Filename: "file1.txt", Size: 25}, |
| 368 | }, |
| 369 | }, |
| 370 | { // 3 |
| 371 | NewBranch: "branch3", |
| 372 | ParentBranches: []string{"master"}, // back on master |
| 373 | Files: []*test.FileInput{ |
| 374 | {Filename: "file1.txt", Size: 30}, |
| 375 | }, |
| 376 | }, |
| 377 | { // 4 |
| 378 | NewBranch: "branch4", |
| 379 | ParentBranches: []string{"master"}, // back on master |
| 380 | Files: []*test.FileInput{ |
| 381 | {Filename: "file1.txt", Size: 40}, |
| 382 | }, |
| 383 | }, |
| 384 | { // 5 |
| 385 | NewBranch: "branch5", |
| 386 | ParentBranches: []string{"master"}, // back on master |
| 387 | Files: []*test.FileInput{ |
| 388 | {Filename: "file1.txt", Size: 50}, |
| 389 | }, |
| 390 | }, |
| 391 | } |
| 392 | outputs := repo.AddCommits(inputs) |
| 393 | // Checkout master again otherwise can't create a worktree from branch4 if we're on it here |
| 394 | test.RunGitCommand(t, true, "checkout", "master") |
| 395 | |
| 396 | // We can create worktrees as subfolders for convenience |
| 397 | // Each one is checked out to a different branch |
| 398 | // Note that we *won't* create one for branch3 |
| 399 | test.RunGitCommand(t, true, "worktree", "add", "tag1_wt", "tag1") |
| 400 | test.RunGitCommand(t, true, "worktree", "add", "branch2_wt", "branch2") |
| 401 | test.RunGitCommand(t, true, "worktree", "add", "branch4_wt", "branch4") |
| 402 | test.RunGitCommand(t, true, "worktree", "add", "branch5_wt", "branch5") |
| 403 | |
| 404 | os.RemoveAll(filepath.Join(repoDir, "branch5_wt")) |
| 405 | |
| 406 | worktrees, err := GetAllWorktrees(filepath.Join(repo.Path, ".git")) |
| 407 | assert.NoError(t, err) |
| 408 | expectedWorktrees := []*Worktree{ |
| 409 | { |
| 410 | Ref: Ref{ |
| 411 | Name: outputs[0].Sha, |
| 412 | Type: RefTypeOther, |
| 413 | Sha: outputs[0].Sha, |
| 414 | }, |
| 415 | Dir: filepath.Join(repoDir, "tag1_wt"), |
| 416 | Prunable: false, |
| 417 | }, |
| 418 | { |
| 419 | Ref: Ref{ |
| 420 | Name: "master", |
| 421 | Type: RefTypeLocalBranch, |
| 422 | Sha: outputs[1].Sha, |
| 423 | }, |
| 424 | Dir: repoDir, |
| 425 | Prunable: false, |
| 426 | }, |
| 427 | { |
| 428 | Ref: Ref{ |
| 429 | Name: "branch2", |
| 430 | Type: RefTypeLocalBranch, |
| 431 | Sha: outputs[2].Sha, |
| 432 | }, |
| 433 | Dir: filepath.Join(repoDir, "branch2_wt"), |
| 434 | Prunable: false, |
| 435 | }, |
| 436 | { |
| 437 | Ref: Ref{ |
| 438 | Name: "branch4", |
| 439 | Type: RefTypeLocalBranch, |
| 440 | Sha: outputs[4].Sha, |
| 441 | }, |
| 442 | Dir: filepath.Join(repoDir, "branch4_wt"), |
| 443 | Prunable: false, |
| 444 | }, |
| 445 | { |
| 446 | Ref: Ref{ |
| 447 | Name: "branch5", |
| 448 | Type: RefTypeLocalBranch, |
| 449 | Sha: outputs[5].Sha, |
| 450 | }, |
| 451 | Dir: filepath.Join(repoDir, "branch5_wt"), |
| 452 | Prunable: true, |
| 453 | }, |
| 454 | } |
| 455 | // Need to sort for consistent comparison |
| 456 | sort.Sort(test.WorktreesByName(expectedWorktrees)) |
| 457 | sort.Sort(test.WorktreesByName(worktrees)) |
| 458 | assert.Equal(t, expectedWorktrees, worktrees, "Worktrees should be correct") |
| 459 | } |
| 460 | |
| 461 | func TestWorktreesBareRepo(t *testing.T) { |
| 462 | // Only git 2.5+ |
| 463 | if !IsGitVersionAtLeast("2.5.0") { |
| 464 | return |
| 465 | } |
| 466 | |
| 467 | repo := test.NewBareRepo(t) |
| 468 | repo.Pushd() |
| 469 | defer func() { |
| 470 | repo.Popd() |
| 471 | repo.Cleanup() |
| 472 | }() |
| 473 | |
| 474 | worktrees, err := GetAllWorktrees(repo.Path) |
| 475 | assert.NoError(t, err) |
| 476 | assert.Nil(t, worktrees) |
| 477 | } |
| 478 | |
| 479 | func TestVersionCompare(t *testing.T) { |
| 480 | assert.True(t, IsVersionAtLeast("2.6.0", "2.6.0")) |
| 481 | assert.True(t, IsVersionAtLeast("2.6.0", "2.6")) |
| 482 | assert.True(t, IsVersionAtLeast("2.6.0", "2")) |
| 483 | assert.True(t, IsVersionAtLeast("2.6.10", "2.6.5")) |
| 484 | assert.True(t, IsVersionAtLeast("2.8.1", "2.7.2")) |
| 485 | |
| 486 | assert.False(t, IsVersionAtLeast("1.6.0", "2")) |
| 487 | assert.False(t, IsVersionAtLeast("2.5.0", "2.6")) |
| 488 | assert.False(t, IsVersionAtLeast("2.5.0", "2.5.1")) |
| 489 | assert.False(t, IsVersionAtLeast("2.5.2", "2.5.10")) |
| 490 | } |
| 491 | |
| 492 | func TestGitAndRootDirs(t *testing.T) { |
| 493 | repo := test.NewRepo(t) |
| 494 | repo.Pushd() |
| 495 | defer func() { |
| 496 | repo.Popd() |
| 497 | repo.Cleanup() |
| 498 | }() |
| 499 | |
| 500 | git, root, err := GitAndRootDirs() |
| 501 | if err != nil { |
| 502 | t.Fatal(err) |
| 503 | } |
| 504 | |
| 505 | expected, err := os.Stat(git) |
| 506 | if err != nil { |
| 507 | t.Fatal(err) |
| 508 | } |
| 509 | |
| 510 | actual, err := os.Stat(filepath.Join(root, ".git")) |
| 511 | if err != nil { |
| 512 | t.Fatal(err) |
| 513 | } |
| 514 | |
| 515 | assert.True(t, os.SameFile(expected, actual)) |
| 516 | } |
| 517 | |
| 518 | func TestGetTrackedFiles(t *testing.T) { |
| 519 | repo := test.NewRepo(t) |
| 520 | repo.Pushd() |
| 521 | defer func() { |
| 522 | repo.Popd() |
| 523 | repo.Cleanup() |
| 524 | }() |
| 525 | |
| 526 | // test commits; we'll just modify the same file each time since we're |
| 527 | // only interested in branches |
| 528 | inputs := []*test.CommitInput{ |
| 529 | { // 0 |
| 530 | Files: []*test.FileInput{ |
| 531 | {Filename: "file1.txt", Size: 20}, |
| 532 | {Filename: "file2.txt", Size: 20}, |
| 533 | {Filename: "folder1/file10.txt", Size: 20}, |
| 534 | {Filename: "folder1/anotherfile.txt", Size: 20}, |
| 535 | }, |
| 536 | }, |
| 537 | { // 1 |
| 538 | Files: []*test.FileInput{ |
| 539 | {Filename: "file3.txt", Size: 20}, |
| 540 | {Filename: "file4.txt", Size: 20}, |
| 541 | {Filename: "folder2/something.txt", Size: 20}, |
| 542 | {Filename: "folder2/folder3/deep.txt", Size: 20}, |
| 543 | }, |
| 544 | }, |
| 545 | } |
| 546 | repo.AddCommits(inputs) |
| 547 | |
| 548 | tracked, err := GetTrackedFiles("*.txt") |
| 549 | assert.Nil(t, err) |
| 550 | sort.Strings(tracked) // for direct comparison |
| 551 | fulllist := []string{"file1.txt", "file2.txt", "file3.txt", "file4.txt", "folder1/anotherfile.txt", "folder1/file10.txt", "folder2/folder3/deep.txt", "folder2/something.txt"} |
| 552 | assert.Equal(t, fulllist, tracked) |
| 553 | |
| 554 | tracked, err = GetTrackedFiles("*file*.txt") |
| 555 | assert.Nil(t, err) |
| 556 | sort.Strings(tracked) |
| 557 | sublist := []string{"file1.txt", "file2.txt", "file3.txt", "file4.txt", "folder1/anotherfile.txt", "folder1/file10.txt"} |
| 558 | assert.Equal(t, sublist, tracked) |
| 559 | |
| 560 | tracked, err = GetTrackedFiles("folder1/*") |
| 561 | assert.Nil(t, err) |
| 562 | sort.Strings(tracked) |
| 563 | sublist = []string{"folder1/anotherfile.txt", "folder1/file10.txt"} |
| 564 | assert.Equal(t, sublist, tracked) |
| 565 | |
| 566 | tracked, err = GetTrackedFiles("folder2/*") |
| 567 | assert.Nil(t, err) |
| 568 | sort.Strings(tracked) |
| 569 | sublist = []string{"folder2/folder3/deep.txt", "folder2/something.txt"} |
| 570 | assert.Equal(t, sublist, tracked) |
| 571 | |
| 572 | // relative dir |
| 573 | os.Chdir("folder1") |
| 574 | tracked, err = GetTrackedFiles("*.txt") |
| 575 | assert.Nil(t, err) |
| 576 | sort.Strings(tracked) |
| 577 | sublist = []string{"anotherfile.txt", "file10.txt"} |
| 578 | assert.Equal(t, sublist, tracked) |
| 579 | os.Chdir("..") |
| 580 | |
| 581 | // absolute paths only includes matches in repo root |
| 582 | tracked, err = GetTrackedFiles("/*.txt") |
| 583 | assert.Nil(t, err) |
| 584 | sort.Strings(tracked) |
| 585 | assert.Equal(t, []string{"file1.txt", "file2.txt", "file3.txt", "file4.txt"}, tracked) |
| 586 | |
| 587 | // Test includes staged but uncommitted files |
| 588 | os.WriteFile("z_newfile.txt", []byte("Hello world"), 0644) |
| 589 | test.RunGitCommand(t, true, "add", "z_newfile.txt") |
| 590 | tracked, err = GetTrackedFiles("*.txt") |
| 591 | assert.Nil(t, err) |
| 592 | sort.Strings(tracked) |
| 593 | fulllist = append(fulllist, "z_newfile.txt") |
| 594 | assert.Equal(t, fulllist, tracked) |
| 595 | |
| 596 | // Test includes modified files (not staged) |
| 597 | os.WriteFile("file1.txt", []byte("Modifications"), 0644) |
| 598 | tracked, err = GetTrackedFiles("*.txt") |
| 599 | assert.Nil(t, err) |
| 600 | sort.Strings(tracked) |
| 601 | assert.Equal(t, fulllist, tracked) |
| 602 | |
| 603 | // Test includes modified files (staged) |
| 604 | test.RunGitCommand(t, true, "add", "file1.txt") |
| 605 | tracked, err = GetTrackedFiles("*.txt") |
| 606 | assert.Nil(t, err) |
| 607 | sort.Strings(tracked) |
| 608 | assert.Equal(t, fulllist, tracked) |
| 609 | |
| 610 | // Test excludes deleted files (not committed) |
| 611 | test.RunGitCommand(t, true, "rm", "file2.txt") |
| 612 | tracked, err = GetTrackedFiles("*.txt") |
| 613 | assert.Nil(t, err) |
| 614 | sort.Strings(tracked) |
| 615 | deletedlist := []string{"file1.txt", "file3.txt", "file4.txt", "folder1/anotherfile.txt", "folder1/file10.txt", "folder2/folder3/deep.txt", "folder2/something.txt", "z_newfile.txt"} |
| 616 | assert.Equal(t, deletedlist, tracked) |
| 617 | |
| 618 | } |
| 619 | |
| 620 | func TestLocalRefs(t *testing.T) { |
| 621 | repo := test.NewRepo(t) |
| 622 | repo.Pushd() |
| 623 | defer func() { |
| 624 | repo.Popd() |
| 625 | repo.Cleanup() |
| 626 | }() |
| 627 | |
| 628 | repo.AddCommits([]*test.CommitInput{ |
| 629 | { |
| 630 | Files: []*test.FileInput{ |
| 631 | {Filename: "file1.txt", Size: 20}, |
| 632 | }, |
| 633 | }, |
| 634 | { |
| 635 | NewBranch: "branch", |
| 636 | ParentBranches: []string{"master"}, |
| 637 | Files: []*test.FileInput{ |
| 638 | {Filename: "file1.txt", Size: 20}, |
| 639 | }, |
| 640 | }, |
| 641 | }) |
| 642 | |
| 643 | test.RunGitCommand(t, true, "tag", "v1") |
| 644 | |
| 645 | refs, err := LocalRefs() |
| 646 | if err != nil { |
| 647 | t.Fatal(err) |
| 648 | } |
| 649 | |
| 650 | actual := make(map[string]bool) |
| 651 | for _, r := range refs { |
| 652 | t.Logf("REF: %s", r.Name) |
| 653 | switch r.Type { |
| 654 | case RefTypeHEAD: |
| 655 | t.Errorf("Local HEAD ref: %v", r) |
| 656 | case RefTypeOther: |
| 657 | t.Errorf("Stash or unknown ref: %v", r) |
| 658 | default: |
| 659 | actual[r.Name] = true |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | expected := []string{"master", "branch", "v1"} |
| 664 | found := 0 |
| 665 | for _, refname := range expected { |
| 666 | if actual[refname] { |
| 667 | found += 1 |
| 668 | } else { |
| 669 | t.Errorf("could not find ref %q", refname) |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | if found != len(expected) { |
| 674 | t.Errorf("Unexpected local refs: %v", actual) |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | func TestGetFilesChanges(t *testing.T) { |
| 679 | repo := test.NewRepo(t) |
| 680 | repo.Pushd() |
| 681 | defer func() { |
| 682 | repo.Popd() |
| 683 | repo.Cleanup() |
| 684 | }() |
| 685 | |
| 686 | commits := repo.AddCommits([]*test.CommitInput{ |
| 687 | { |
| 688 | Files: []*test.FileInput{ |
| 689 | {Filename: "file1.txt", Size: 20}, |
| 690 | }, |
| 691 | }, |
| 692 | { |
| 693 | Files: []*test.FileInput{ |
| 694 | {Filename: "file1.txt", Size: 25}, |
| 695 | {Filename: "file2.txt", Size: 20}, |
| 696 | {Filename: "folder/file3.txt", Size: 10}, |
| 697 | }, |
| 698 | Tags: []string{"tag1"}, |
| 699 | }, |
| 700 | { |
| 701 | NewBranch: "abranch", |
| 702 | ParentBranches: []string{"master"}, |
| 703 | Files: []*test.FileInput{ |
| 704 | {Filename: "file1.txt", Size: 30}, |
| 705 | {Filename: "file4.txt", Size: 40}, |
| 706 | }, |
| 707 | }, |
| 708 | }) |
| 709 | |
| 710 | expected0to1 := []string{"file1.txt", "file2.txt", "folder/file3.txt"} |
| 711 | expected1to2 := []string{"file1.txt", "file4.txt"} |
| 712 | expected0to2 := []string{"file1.txt", "file2.txt", "file4.txt", "folder/file3.txt"} |
| 713 | // Test 2 SHAs |
| 714 | changes, err := GetFilesChanged(commits[0].Sha, commits[1].Sha) |
| 715 | assert.Nil(t, err) |
| 716 | assert.Equal(t, expected0to1, changes) |
| 717 | // Test SHA & tag |
| 718 | changes, err = GetFilesChanged(commits[0].Sha, "tag1") |
| 719 | assert.Nil(t, err) |
| 720 | assert.Equal(t, expected0to1, changes) |
| 721 | // Test SHA & branch |
| 722 | changes, err = GetFilesChanged(commits[0].Sha, "abranch") |
| 723 | assert.Nil(t, err) |
| 724 | assert.Equal(t, expected0to2, changes) |
| 725 | // Test tag & branch |
| 726 | changes, err = GetFilesChanged("tag1", "abranch") |
| 727 | assert.Nil(t, err) |
| 728 | assert.Equal(t, expected1to2, changes) |
| 729 | // Test fail |
| 730 | _, err = GetFilesChanged("tag1", "nonexisting") |
| 731 | assert.NotNil(t, err) |
| 732 | _, err = GetFilesChanged("nonexisting", "tag1") |
| 733 | assert.NotNil(t, err) |
| 734 | // Test Single arg version |
| 735 | changes, err = GetFilesChanged(commits[1].Sha, "") |
| 736 | assert.Nil(t, err) |
| 737 | assert.Equal(t, expected0to1, changes) |
| 738 | changes, err = GetFilesChanged("abranch", "") |
| 739 | assert.Nil(t, err) |
| 740 | assert.Equal(t, expected1to2, changes) |
| 741 | |
| 742 | } |
| 743 | |
| 744 | func TestValidateRemoteURL(t *testing.T) { |
| 745 | assert.Nil(t, ValidateRemoteURL("https://github.com/git-lfs/git-lfs")) |
| 746 | assert.Nil(t, ValidateRemoteURL("http://github.com/git-lfs/git-lfs")) |
| 747 | assert.Nil(t, ValidateRemoteURL("git://github.com/git-lfs/git-lfs")) |
| 748 | assert.Nil(t, ValidateRemoteURL("ssh://[email protected]/git-lfs/git-lfs")) |
| 749 | assert.Nil(t, ValidateRemoteURL("ssh://[email protected]:22/git-lfs/git-lfs")) |
| 750 | assert.Nil(t, ValidateRemoteURL("[email protected]:git-lfs/git-lfs")) |
| 751 | assert.Nil(t, ValidateRemoteURL("git@server:/absolute/path.git")) |
| 752 | assert.NotNil(t, ValidateRemoteURL("ftp://[email protected]/git-lfs/git-lfs")) |
| 753 | } |
| 754 | |
| 755 | func TestRefTypeKnownPrefixes(t *testing.T) { |
| 756 | for typ, expected := range map[RefType]struct { |
| 757 | Prefix string |
| 758 | Ok bool |
| 759 | }{ |
| 760 | RefTypeLocalBranch: {"refs/heads", true}, |
| 761 | RefTypeRemoteBranch: {"refs/remotes", true}, |
| 762 | RefTypeLocalTag: {"refs/tags", true}, |
| 763 | RefTypeHEAD: {"", false}, |
| 764 | RefTypeOther: {"", false}, |
| 765 | } { |
| 766 | prefix, ok := typ.Prefix() |
| 767 | |
| 768 | assert.Equal(t, expected.Prefix, prefix) |
| 769 | assert.Equal(t, expected.Ok, ok) |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | func TestRemoteURLs(t *testing.T) { |
| 774 | repo := test.NewRepo(t) |
| 775 | repo.Pushd() |
| 776 | defer func() { |
| 777 | repo.Popd() |
| 778 | repo.Cleanup() |
| 779 | }() |
| 780 | cfg := repo.GitConfig() |
| 781 | cfg.SetLocal("remote.foo.url", "https://github.com/git-lfs/git-lfs.git") |
| 782 | cfg.SetLocal("remote.bar.url", "https://github.com/git-lfs/wildmatch.git") |
| 783 | cfg.SetLocal("remote.bar.pushurl", "https://github.com/git-lfs/pktline.git") |
| 784 | |
| 785 | expected := make(map[string][]string) |
| 786 | expected["foo"] = []string{"https://github.com/git-lfs/git-lfs.git"} |
| 787 | expected["bar"] = []string{"https://github.com/git-lfs/wildmatch.git"} |
| 788 | actual, err := RemoteURLs(false) |
| 789 | assert.Nil(t, err) |
| 790 | assert.Equal(t, expected, actual) |
| 791 | |
| 792 | expected["bar"] = []string{"https://github.com/git-lfs/pktline.git"} |
| 793 | actual, err = RemoteURLs(true) |
| 794 | assert.Nil(t, err) |
| 795 | assert.Equal(t, expected, actual) |
| 796 | } |
| 797 | |
| 798 | func TestMapRemoteURL(t *testing.T) { |
| 799 | repo := test.NewRepo(t) |
| 800 | repo.Pushd() |
| 801 | defer func() { |
| 802 | repo.Popd() |
| 803 | repo.Cleanup() |
| 804 | }() |
| 805 | cfg := repo.GitConfig() |
| 806 | cfg.SetLocal("remote.foo.url", "https://github.com/git-lfs/git-lfs.git") |
| 807 | cfg.SetLocal("remote.bar.url", "https://github.com/git-lfs/wildmatch.git") |
| 808 | cfg.SetLocal("remote.bar.pushurl", "https://github.com/git-lfs/pktline.git") |
| 809 | |
| 810 | tests := []struct { |
| 811 | url string |
| 812 | push bool |
| 813 | match bool |
| 814 | val string |
| 815 | }{ |
| 816 | { |
| 817 | "https://github.com/git-lfs/git-lfs.git", |
| 818 | false, |
| 819 | true, |
| 820 | "foo", |
| 821 | }, |
| 822 | { |
| 823 | "https://github.com/git-lfs/git-lfs.git", |
| 824 | true, |
| 825 | true, |
| 826 | "foo", |
| 827 | }, |
| 828 | { |
| 829 | "https://github.com/git-lfs/wildmatch.git", |
| 830 | false, |
| 831 | true, |
| 832 | "bar", |
| 833 | }, |
| 834 | { |
| 835 | "https://github.com/git-lfs/pktline.git", |
| 836 | true, |
| 837 | true, |
| 838 | "bar", |
| 839 | }, |
| 840 | { |
| 841 | "https://github.com/git-lfs/pktline.git", |
| 842 | false, |
| 843 | false, |
| 844 | "https://github.com/git-lfs/pktline.git", |
| 845 | }, |
| 846 | { |
| 847 | "https://github.com/git/git.git", |
| 848 | true, |
| 849 | false, |
| 850 | "https://github.com/git/git.git", |
| 851 | }, |
| 852 | } |
| 853 | for _, test := range tests { |
| 854 | val, ok := MapRemoteURL(test.url, test.push) |
| 855 | assert.Equal(t, ok, test.match) |
| 856 | assert.Equal(t, val, test.val) |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | func TestIsValidObjectIDLength(t *testing.T) { |
| 861 | // Lengths are 40, 64, 39, and 12. |
| 862 | assert.Equal(t, HasValidObjectIDLength("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), true) |
| 863 | assert.Equal(t, HasValidObjectIDLength("2222222222222222222222222222222222222222222222222222222222222222"), true) |
| 864 | assert.Equal(t, HasValidObjectIDLength("555555555555555555555555555555555555555"), false) |
| 865 | assert.Equal(t, HasValidObjectIDLength("0123456789ab"), false) |
| 866 | } |
| 867 | |
| 868 | func TestIsZeroObjectID(t *testing.T) { |
| 869 | assert.Equal(t, IsZeroObjectID("0000000000000000000000000000000000000000"), true) |
| 870 | assert.Equal(t, IsZeroObjectID("0000000000000000000000000000000000000000000000000000000000000000"), true) |
| 871 | assert.Equal(t, IsZeroObjectID("000000000000000000000000000000000000000"), false) |
| 872 | assert.Equal(t, IsZeroObjectID("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"), false) |
| 873 | assert.Equal(t, IsZeroObjectID("473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813"), false) |
| 874 | } |