1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_spi(ProcessEnvironmentBlockShim)
import Basics
@testable import SourceControl
import SPMTestSupport
import XCTest
import struct TSCBasic.FileSystemError
import func TSCBasic.makeDirectories
import class Basics.AsyncProcess
import enum TSCUtility.Git
class GitRepositoryTests: XCTestCase {
override func setUp() {
// needed for submodule tests
Git.environmentBlock = ["GIT_ALLOW_PROTOCOL": "file"]
}
override func tearDown() {
Git.environmentBlock = .init(Environment.current)
}
/// Test the basic provider functions.
func testRepositorySpecifier() {
do {
let s1 = RepositorySpecifier(url: "a")
let s2 = RepositorySpecifier(url: "a")
let s3 = RepositorySpecifier(url: "b")
XCTAssertEqual(s1, s1)
XCTAssertEqual(s1, s2)
XCTAssertEqual(Set([s1]), Set([s2]))
XCTAssertNotEqual(s1, s3)
XCTAssertNotEqual(s2, s3)
}
do {
let s1 = RepositorySpecifier(path: "/A")
let s2 = RepositorySpecifier(path: "/A")
let s3 = RepositorySpecifier(path: "/B")
XCTAssertEqual(s1, s1)
XCTAssertEqual(s1, s2)
XCTAssertEqual(Set([s1]), Set([s2]))
XCTAssertNotEqual(s1, s3)
XCTAssertNotEqual(s2, s3)
}
}
/// Test the basic provider functions.
func testProvider() throws {
try testWithTemporaryDirectory { path in
let testRepoPath = path.appending("test-repo")
try! makeDirectories(testRepoPath)
initGitRepo(testRepoPath, tag: "1.2.3")
// Test the provider.
let testCheckoutPath = path.appending("checkout")
let provider = GitRepositoryProvider()
XCTAssertTrue(try provider.workingCopyExists(at: testRepoPath))
let repoSpec = RepositorySpecifier(path: testRepoPath)
try provider.fetch(repository: repoSpec, to: testCheckoutPath)
// Verify the checkout was made.
XCTAssertDirectoryExists(testCheckoutPath)
// Test the repository interface.
let repository = provider.open(repository: repoSpec, at: testCheckoutPath)
let tags = try repository.getTags()
XCTAssertEqual(try repository.getTags(), ["1.2.3"])
let revision = try repository.resolveRevision(tag: tags.first ?? "<invalid>")
// FIXME: It would be nice if we had a deterministic hash here...
XCTAssertEqual(revision.identifier,
try AsyncProcess.popen(
args: Git.tool, "-C", testRepoPath.pathString, "rev-parse", "--verify", "1.2.3").utf8Output().spm_chomp())
if let revision = try? repository.resolveRevision(tag: "<invalid>") {
XCTFail("unexpected resolution of invalid tag to \(revision)")
}
let main = try repository.resolveRevision(identifier: "main")
XCTAssertEqual(main.identifier,
try AsyncProcess.checkNonZeroExit(
args: Git.tool, "-C", testRepoPath.pathString, "rev-parse", "--verify", "main").spm_chomp())
// Check that git hashes resolve to themselves.
let mainIdentifier = try repository.resolveRevision(identifier: main.identifier)
XCTAssertEqual(main.identifier, mainIdentifier.identifier)
// Check that invalid identifier doesn't resolve.
if let revision = try? repository.resolveRevision(identifier: "invalid") {
XCTFail("unexpected resolution of invalid identifier to \(revision)")
}
}
}
/// Check hash validation.
func testGitRepositoryHash() throws {
let validHash = "0123456789012345678901234567890123456789"
XCTAssertNotEqual(GitRepository.Hash(validHash), nil)
let invalidHexHash = validHash + "1"
XCTAssertEqual(GitRepository.Hash(invalidHexHash), nil)
let invalidNonHexHash = "012345678901234567890123456789012345678!"
XCTAssertEqual(GitRepository.Hash(invalidNonHexHash), nil)
}
/// Check raw repository facilities.
///
/// In order to be stable, this test uses a static test git repository in
/// `Inputs`, which has known commit hashes. See the `construct.sh` script
/// contained within it for more information.
func testRawRepository() throws {
#if os(Windows)
try XCTSkipIf(true, "test repository has non-portable file names")
#endif
try testWithTemporaryDirectory { path in
// Unarchive the static test repository.
let inputArchivePath = AbsolutePath(#file).parentDirectory.appending(components: "Inputs", "TestRepo.tgz")
#if os(Windows)
try systemQuietly(["tar.exe", "-x", "-v", "-C", path.pathString, "-f", inputArchivePath.pathString])
#else
try systemQuietly(["tar", "-x", "-v", "-C", path.pathString, "-f", inputArchivePath.pathString])
#endif
let testRepoPath = path.appending("TestRepo")
// Check hash resolution.
let repo = GitRepository(path: testRepoPath)
XCTAssertEqual(try repo.resolveHash(treeish: "1.0", type: "commit"),
try repo.resolveHash(treeish: "master"))
// Get the initial commit.
let initialCommitHash = try repo.resolveHash(treeish: "a8b9fcb")
XCTAssertEqual(initialCommitHash, GitRepository.Hash("a8b9fcbf893b3b02c0196609059ebae37aeb7f0b"))
// Check commit loading.
let initialCommit = try repo.readCommit(hash: initialCommitHash)
XCTAssertEqual(initialCommit.hash, initialCommitHash)
XCTAssertEqual(initialCommit.tree, GitRepository.Hash("9d463c3b538619448c5d2ecac379e92f075a8976"))
// Check tree loading.
let initialTree = try repo.readTree(hash: initialCommit.tree)
guard case .hash(let initialTreeHash) = initialTree.location else {
return XCTFail("wrong pointer")
}
XCTAssertEqual(initialTreeHash, initialCommit.tree)
XCTAssertEqual(initialTree.contents.count, 1)
guard let readmeEntry = initialTree.contents.first else { return XCTFail() }
guard case .hash(let readmeEntryHash) = readmeEntry.location else {
return XCTFail("wrong pointer")
}
XCTAssertEqual(readmeEntryHash, GitRepository.Hash("92513075b3491a54c45a880be25150d92388e7bc"))
XCTAssertEqual(readmeEntry.type, .blob)
XCTAssertEqual(readmeEntry.name, "README.txt")
// Check loading of odd names.
//
// This is a commit which has a subdirectory 'funny-names' with
// paths with special characters.
let funnyNamesCommit = try repo.readCommit(hash: repo.resolveHash(treeish: "a7b19a7"))
let funnyNamesRoot = try repo.readTree(hash: funnyNamesCommit.tree)
XCTAssertEqual(funnyNamesRoot.contents.map{ $0.name }, ["README.txt", "funny-names", "subdir"])
guard funnyNamesRoot.contents.count == 3 else { return XCTFail() }
// FIXME: This isn't yet supported.
let funnyNamesSubdirEntry = funnyNamesRoot.contents[1]
XCTAssertEqual(funnyNamesSubdirEntry.type, .tree)
if let _ = try? repo.readTree(location: funnyNamesSubdirEntry.location) {
XCTFail("unexpected success reading tree with funny names")
}
}
}
func testSubmoduleRead() throws {
try testWithTemporaryDirectory { path in
let testRepoPath = path.appending("test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath)
let repoPath = path.appending("repo")
try makeDirectories(repoPath)
initGitRepo(repoPath)
try AsyncProcess.checkNonZeroExit(
args: Git.tool, "-C", repoPath.pathString, "submodule", "add", testRepoPath.pathString,
environment: .init(Git.environmentBlock)
)
let repo = GitRepository(path: repoPath)
try repo.stageEverything()
try repo.commit()
// We should be able to read a repo which as a submdoule.
_ = try repo.readTree(hash: try repo.resolveHash(treeish: "main"))
}
}
/// Test the Git file system view.
func testGitFileView() throws {
try testWithTemporaryDirectory { path in
let testRepoPath = path.appending("test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath)
// Add a few files and a directory.
let test1FileContents = "Hello, world!"
let test2FileContents = "Hello, happy world!"
let test3FileContents = """
#!/bin/sh
set -e
exit 0
"""
try localFileSystem.writeFileContents(testRepoPath.appending("test-file-1.txt"), string: test1FileContents)
try localFileSystem.createDirectory(testRepoPath.appending("subdir"))
try localFileSystem.writeFileContents(testRepoPath.appending(components: "subdir", "test-file-2.txt"), string: test2FileContents)
try localFileSystem.writeFileContents(testRepoPath.appending("test-file-3.sh"), string: test3FileContents)
try localFileSystem.chmod(.executable, path: testRepoPath.appending("test-file-3.sh"), options: [])
let testRepo = GitRepository(path: testRepoPath)
try testRepo.stage(files: "test-file-1.txt", "subdir/test-file-2.txt", "test-file-3.sh")
try testRepo.commit()
try testRepo.tag(name: "test-tag")
// Get the the repository via the provider. the provider.
let testClonePath = path.appending("clone")
let provider = GitRepositoryProvider()
let repoSpec = RepositorySpecifier(path: testRepoPath)
try provider.fetch(repository: repoSpec, to: testClonePath)
let repository = provider.open(repository: repoSpec, at: testClonePath)
// Get and test the file system view.
let view = try repository.openFileView(revision: repository.resolveRevision(tag: "test-tag"))
// Check basic predicates.
XCTAssert(view.isDirectory("/"))
XCTAssert(view.isDirectory("/subdir"))
XCTAssert(!view.isDirectory("/does-not-exist"))
XCTAssert(view.exists("/test-file-1.txt"))
XCTAssert(!view.exists("/does-not-exist"))
XCTAssert(view.isFile("/test-file-1.txt"))
XCTAssert(!view.isSymlink("/test-file-1.txt"))
XCTAssert(!view.isExecutableFile("/does-not-exist"))
#if !os(Windows)
XCTAssert(view.isExecutableFile("/test-file-3.sh"))
#endif
// Check read of a directory.
let subdirPath = AbsolutePath("/subdir")
XCTAssertEqual(try view.getDirectoryContents(AbsolutePath("/")).sorted(), ["file.swift", "subdir", "test-file-1.txt", "test-file-3.sh"])
XCTAssertEqual(try view.getDirectoryContents(subdirPath).sorted(), ["test-file-2.txt"])
XCTAssertThrows(FileSystemError(.isDirectory, subdirPath)) {
_ = try view.readFileContents(subdirPath)
}
// Check read versus root.
XCTAssertThrows(FileSystemError(.isDirectory, AbsolutePath.root)) {
_ = try view.readFileContents(.root)
}
// Check read through a non-directory.
let notDirectoryPath1 = AbsolutePath("/test-file-1.txt")
XCTAssertThrows(FileSystemError(.notDirectory, notDirectoryPath1)) {
_ = try view.getDirectoryContents(notDirectoryPath1)
}
let notDirectoryPath2 = AbsolutePath("/test-file-1.txt/thing")
XCTAssertThrows(FileSystemError(.notDirectory, notDirectoryPath2)) {
_ = try view.readFileContents(notDirectoryPath2)
}
// Check read/write into a missing directory.
let noEntryPath1 = AbsolutePath("/does-not-exist")
XCTAssertThrows(FileSystemError(.noEntry, noEntryPath1)) {
_ = try view.getDirectoryContents(noEntryPath1)
}
let noEntryPath2 = AbsolutePath("/does/not/exist")
XCTAssertThrows(FileSystemError(.noEntry, noEntryPath2)) {
_ = try view.readFileContents(noEntryPath2)
}
// Check read of a file.
XCTAssertEqual(try view.readFileContents("/test-file-1.txt"), test1FileContents)
XCTAssertEqual(try view.readFileContents("/subdir/test-file-2.txt"), test2FileContents)
XCTAssertEqual(try view.readFileContents("/test-file-3.sh"), test3FileContents)
}
}
/// Test the handling of local checkouts.
func testCheckouts() throws {
try testWithTemporaryDirectory { path in
// Create a test repository.
let testRepoPath = path.appending("test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath, tag: "initial")
let initialRevision = try GitRepository(path: testRepoPath).getCurrentRevision()
// Add a couple files and a directory.
try localFileSystem.writeFileContents(testRepoPath.appending("test.txt"), bytes: "Hi")
let testRepo = GitRepository(path: testRepoPath)
try testRepo.stage(file: "test.txt")
try testRepo.commit()
try testRepo.tag(name: "test-tag")
let currentRevision = try GitRepository(path: testRepoPath).getCurrentRevision()
// Fetch the repository using the provider.
let testClonePath = path.appending("clone")
let provider = GitRepositoryProvider()
let repoSpec = RepositorySpecifier(path: testRepoPath)
try provider.fetch(repository: repoSpec, to: testClonePath)
// Clone off a checkout.
let checkoutPath = path.appending("checkout")
_ = try provider.createWorkingCopy(repository: repoSpec, sourcePath: testClonePath, at: checkoutPath, editable: false)
// The remote of this checkout should point to the clone.
XCTAssertEqual(try GitRepository(path: checkoutPath).remotes()[0].url, testClonePath.pathString)
let editsPath = path.appending("edit")
_ = try provider.createWorkingCopy(repository: repoSpec, sourcePath: testClonePath, at: editsPath, editable: true)
// The remote of this checkout should point to the original repo.
XCTAssertEqual(try GitRepository(path: editsPath).remotes()[0].url, testRepoPath.pathString)
// Check the working copies.
for path in [checkoutPath, editsPath] {
let workingCopy = try provider.openWorkingCopy(at: path)
try workingCopy.checkout(tag: "test-tag")
XCTAssertEqual(try workingCopy.getCurrentRevision(), currentRevision)
XCTAssertFileExists(path.appending("test.txt"))
try workingCopy.checkout(tag: "initial")
XCTAssertEqual(try workingCopy.getCurrentRevision(), initialRevision)
XCTAssertNoSuchPath(path.appending("test.txt"))
}
}
}
func testFetch() throws {
try testWithTemporaryDirectory { path in
// Create a repo.
let testRepoPath = path.appending("test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath, tag: "1.2.3")
let repo = GitRepository(path: testRepoPath)
XCTAssertEqual(try repo.getTags(), ["1.2.3"])
// Clone it somewhere.
let testClonePath = path.appending("clone")
let provider = GitRepositoryProvider()
let repoSpec = RepositorySpecifier(path: testRepoPath)
try provider.fetch(repository: repoSpec, to: testClonePath)
let clonedRepo = provider.open(repository: repoSpec, at: testClonePath)
XCTAssertEqual(try clonedRepo.getTags(), ["1.2.3"])
// Clone off a checkout.
let checkoutPath = path.appending("checkout")
let checkoutRepo = try provider.createWorkingCopy(repository: repoSpec, sourcePath: testClonePath, at: checkoutPath, editable: false)
XCTAssertEqual(try checkoutRepo.getTags(), ["1.2.3"])
// Add a new file to original repo.
try localFileSystem.writeFileContents(testRepoPath.appending("test.txt"), bytes: "Hi")
let testRepo = GitRepository(path: testRepoPath)
try testRepo.stage(file: "test.txt")
try testRepo.commit()
try testRepo.tag(name: "2.0.0")
// Update the cloned repo.
try clonedRepo.fetch()
XCTAssertEqual(try clonedRepo.getTags().sorted(), ["1.2.3", "2.0.0"])
// Update the checkout.
try checkoutRepo.fetch()
XCTAssertEqual(try checkoutRepo.getTags().sorted(), ["1.2.3", "2.0.0"])
}
}
func testHasUnpushedCommits() throws {
try testWithTemporaryDirectory { path in
// Create a repo.
let testRepoPath = path.appending("test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath)
// Create a bare clone it somewhere because we want to later push into the repo.
let testBareRepoPath = path.appending("test-repo-bare")
try systemQuietly([Git.tool, "clone", "--bare", testRepoPath.pathString, testBareRepoPath.pathString])
// Clone it somewhere.
let testClonePath = path.appending("clone")
let provider = GitRepositoryProvider()
let repoSpec = RepositorySpecifier(path: testBareRepoPath)
try provider.fetch(repository: repoSpec, to: testClonePath)
// Clone off a checkout.
let checkoutPath = path.appending("checkout")
let checkoutRepo = try provider.createWorkingCopy(repository: repoSpec, sourcePath: testClonePath, at: checkoutPath, editable: true)
XCTAssertFalse(try checkoutRepo.hasUnpushedCommits())
// Add a new file to checkout.
try localFileSystem.writeFileContents(checkoutPath.appending("test.txt"), bytes: "Hi")
let checkoutTestRepo = GitRepository(path: checkoutPath)
try checkoutTestRepo.stage(file: "test.txt")
try checkoutTestRepo.commit()
// We should have commits which are not pushed.
XCTAssert(try checkoutRepo.hasUnpushedCommits())
// Push the changes and check again.
try checkoutTestRepo.push(remote: "origin", branch: "main")
XCTAssertFalse(try checkoutRepo.hasUnpushedCommits())
}
}
func testSetRemote() throws {
try testWithTemporaryDirectory { path in
// Create a repo.
let testRepoPath = path.appending("test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath)
let repo = GitRepository(path: testRepoPath)
// There should be no remotes currently.
XCTAssert(try repo.remotes().isEmpty)
// Add a remote via git cli.
try systemQuietly([Git.tool, "-C", testRepoPath.pathString, "remote", "add", "origin", "../foo"])
// Test if it was added.
XCTAssertEqual(Dictionary(uniqueKeysWithValues: try repo.remotes().map { ($0.0, $0.1) }), ["origin": "../foo"])
// Change remote.
try repo.setURL(remote: "origin", url: "../bar")
XCTAssertEqual(Dictionary(uniqueKeysWithValues: try repo.remotes().map { ($0.0, $0.1) }), ["origin": "../bar"])
// Try changing remote of non-existent remote.
do {
try repo.setURL(remote: "fake", url: "../bar")
XCTFail("unexpected success (shouldn’t have been able to set URL of missing remote)")
}
catch let error as GitRepositoryError {
XCTAssertEqual(error.path, testRepoPath)
XCTAssertNotNil(error.diagnosticLocation)
}
}
}
func testUncommittedChanges() throws {
try testWithTemporaryDirectory { path in
// Create a repo.
let testRepoPath = path.appending("test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath)
// Create a file (which we will modify later).
try localFileSystem.writeFileContents(testRepoPath.appending("test.txt"), bytes: "Hi")
let repo = GitRepository(path: testRepoPath)
XCTAssert(repo.hasUncommittedChanges())
try repo.stage(file: "test.txt")
XCTAssert(repo.hasUncommittedChanges())
try repo.commit()
XCTAssertFalse(repo.hasUncommittedChanges())
// Modify the file in the repo.
try localFileSystem.writeFileContents(repo.path.appending("test.txt"), bytes: "Hello")
XCTAssert(repo.hasUncommittedChanges())
}
}
func testBranchOperations() throws {
try testWithTemporaryDirectory { path in
// Create a repo.
let testRepoPath = path.appending("test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath)
let repo = GitRepository(path: testRepoPath)
var currentRevision = try repo.getCurrentRevision()
// This is the default branch of a new repo.
XCTAssertTrue(repo.exists(revision: Revision(identifier: "main")))
// Check a non existent revision.
XCTAssertFalse(repo.exists(revision: Revision(identifier: "nonExistent")))
// Checkout a new branch using command line.
try systemQuietly([Git.tool, "-C", testRepoPath.pathString, "checkout", "-b", "TestBranch1"])
XCTAssertTrue(repo.exists(revision: Revision(identifier: "TestBranch1")))
XCTAssertEqual(try repo.getCurrentRevision(), currentRevision)
// Make sure we're on the new branch right now.
XCTAssertEqual(try repo.currentBranch(), "TestBranch1")
// Checkout new branch using our API.
currentRevision = try repo.getCurrentRevision()
try repo.checkout(newBranch: "TestBranch2")
XCTAssert(repo.exists(revision: Revision(identifier: "TestBranch2")))
XCTAssertEqual(try repo.getCurrentRevision(), currentRevision)
XCTAssertEqual(try repo.currentBranch(), "TestBranch2")
}
}
func testRevisionOperations() throws {
try testWithTemporaryDirectory { path in
// Create a repo.
let repositoryPath = path.appending("test-repo")
try makeDirectories(repositoryPath)
initGitRepo(repositoryPath)
let repo = GitRepository(path: repositoryPath)
do {
let revision = try repo.getCurrentRevision()
XCTAssertTrue(repo.exists(revision: revision))
}
do {
XCTAssertFalse(repo.exists(revision: Revision(identifier: UUID().uuidString)))
let tag = UUID().uuidString
try repo.tag(name: tag)
let revision = try repo.resolveRevision(tag: tag)
XCTAssertTrue(repo.exists(revision: revision))
}
}
}
func testCheckoutRevision() throws {
try testWithTemporaryDirectory { path in
// Create a repo.
let testRepoPath = path.appending("test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath)
let repo = GitRepository(path: testRepoPath)
func createAndStageTestFile() throws {
try localFileSystem.writeFileContents(testRepoPath.appending("test.txt"), bytes: "Hi")
try repo.stage(file: "test.txt")
}
try repo.checkout(revision: Revision(identifier: "main"))
// Current branch must be main.
XCTAssertEqual(try repo.currentBranch(), "main")
// Create a new branch.
try repo.checkout(newBranch: "TestBranch")
XCTAssertEqual(try repo.currentBranch(), "TestBranch")
// Create some random file.
try createAndStageTestFile()
XCTAssert(repo.hasUncommittedChanges())
// Checkout current revision again, the test file should go away.
let currentRevision = try repo.getCurrentRevision()
try repo.checkout(revision: currentRevision)
XCTAssertFalse(repo.hasUncommittedChanges())
// We should be on detached head.
XCTAssertEqual(try repo.currentBranch(), "HEAD")
// Try again and checkout to a previous branch.
try createAndStageTestFile()
XCTAssert(repo.hasUncommittedChanges())
try repo.checkout(revision: Revision(identifier: "TestBranch"))
XCTAssertFalse(repo.hasUncommittedChanges())
XCTAssertEqual(try repo.currentBranch(), "TestBranch")
do {
try repo.checkout(revision: Revision(identifier: "nonExistent"))
XCTFail("Unexpected checkout success on non existent branch")
} catch {}
}
}
func testSubmodules() throws {
try testWithTemporaryDirectory { path in
let provider = GitRepositoryProvider()
// Create repos: foo and bar, foo will have bar as submodule and then later
// the submodule ref will be updated in foo.
let fooPath = path.appending("foo-original")
let fooSpecifier = RepositorySpecifier(path: fooPath)
let fooRepoPath = path.appending("foo-repo")
let fooWorkingPath = path.appending("foo-working")
let barPath = path.appending("bar-original")
let bazPath = path.appending("baz-original")
// Create the repos and add a file.
for path in [fooPath, barPath, bazPath] {
try makeDirectories(path)
initGitRepo(path)
try localFileSystem.writeFileContents(path.appending("hello.txt"), bytes: "hello")
let repo = GitRepository(path: path)
try repo.stageEverything()
try repo.commit()
}
let foo = GitRepository(path: fooPath)
let bar = GitRepository(path: barPath)
// The tag 1.0.0 does not contain the submodule.
try foo.tag(name: "1.0.0")
// Fetch and clone repo foo.
try provider.fetch(repository: fooSpecifier, to: fooRepoPath)
_ = try provider.createWorkingCopy(repository: fooSpecifier, sourcePath: fooRepoPath, at: fooWorkingPath, editable: false)
let fooRepo = GitRepository(path: fooRepoPath, isWorkingRepo: false)
let fooWorkingRepo = GitRepository(path: fooWorkingPath)
// Checkout the first tag which doesn't has submodule.
try fooWorkingRepo.checkout(tag: "1.0.0")
XCTAssertNoSuchPath(fooWorkingPath.appending("bar"))
// Add submodule to foo and tag it as 1.0.1
try foo.checkout(newBranch: "submodule")
try AsyncProcess.checkNonZeroExit(
args: Git.tool, "-C", fooPath.pathString, "submodule", "add", barPath.pathString, "bar",
environment: .init(Git.environmentBlock)
)
try foo.stageEverything()
try foo.commit()
try foo.tag(name: "1.0.1")
// Update our bare and working repos.
try fooRepo.fetch()
try fooWorkingRepo.fetch()
// Checkout the tag with submodule and expect submodules files to be present.
try fooWorkingRepo.checkout(tag: "1.0.1")
XCTAssertFileExists(fooWorkingPath.appending(components: "bar", "hello.txt"))
// Checkout the tag without submodule and ensure that the submodule files are gone.
try fooWorkingRepo.checkout(tag: "1.0.0")
XCTAssertNoSuchPath(fooWorkingPath.appending(components: "bar"))
// Add something to bar.
try localFileSystem.writeFileContents(barPath.appending("bar.txt"), bytes: "hello")
// Add a submodule too to check for recursive submodules.
try AsyncProcess.checkNonZeroExit(
args: Git.tool, "-C", barPath.pathString, "submodule", "add", bazPath.pathString, "baz",
environment: .init(Git.environmentBlock)
)
try bar.stageEverything()
try bar.commit()
// Update the ref of bar in foo and tag as 1.0.2
try systemQuietly([Git.tool, "-C", fooPath.appending("bar").pathString, "pull"])
try foo.stageEverything()
try foo.commit()
try foo.tag(name: "1.0.2")
try fooRepo.fetch()
try fooWorkingRepo.fetch()
// We should see the new file we added in the submodule.
try fooWorkingRepo.checkout(tag: "1.0.2")
XCTAssertFileExists(fooWorkingPath.appending(components: "bar", "hello.txt"))
XCTAssertFileExists(fooWorkingPath.appending(components: "bar", "bar.txt"))
XCTAssertFileExists(fooWorkingPath.appending(components: "bar", "baz", "hello.txt"))
// Double check.
try fooWorkingRepo.checkout(tag: "1.0.0")
XCTAssertNoSuchPath(fooWorkingPath.appending(components: "bar"))
}
}
func testAlternativeObjectStoreValidation() throws {
try testWithTemporaryDirectory { path in
// Create a repo.
let testRepoPath = path.appending("test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath, tag: "1.2.3")
let repo = GitRepository(path: testRepoPath)
XCTAssertEqual(try repo.getTags(), ["1.2.3"])
// Clone it somewhere.
let testClonePath = path.appending("clone")
let provider = GitRepositoryProvider()
let repoSpec = RepositorySpecifier(path: testRepoPath)
try provider.fetch(repository: repoSpec, to: testClonePath)
let clonedRepo = provider.open(repository: repoSpec, at: testClonePath)
XCTAssertEqual(try clonedRepo.getTags(), ["1.2.3"])
// Clone off a checkout.
let checkoutPath = path.appending("checkout")
let checkoutRepo = try provider.createWorkingCopy(repository: repoSpec, sourcePath: testClonePath, at: checkoutPath, editable: false)
// The object store should be valid.
XCTAssertTrue(checkoutRepo.isAlternateObjectStoreValid(expected: testClonePath))
// Wrong path
XCTAssertFalse(checkoutRepo.isAlternateObjectStoreValid(expected: testClonePath.appending(UUID().uuidString)))
// Delete the clone (alternative object store).
try localFileSystem.removeFileTree(testClonePath)
XCTAssertFalse(checkoutRepo.isAlternateObjectStoreValid(expected: testClonePath))
}
}
func testAreIgnored() throws {
try testWithTemporaryDirectory { path in
// Create a repo.
let testRepoPath = path.appending("test_repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath)
let repo = GitRepository(path: testRepoPath)
// Add a .gitignore
try localFileSystem.writeFileContents(testRepoPath.appending(".gitignore"), bytes: "ignored_file1\nignored file2")
let ignored = try repo.areIgnored([testRepoPath.appending("ignored_file1"), testRepoPath.appending("ignored file2"), testRepoPath.appending("not ignored")])
XCTAssertTrue(ignored[0])
XCTAssertTrue(ignored[1])
XCTAssertFalse(ignored[2])
let notIgnored = try repo.areIgnored([testRepoPath.appending("not_ignored")])
XCTAssertFalse(notIgnored[0])
}
}
func testAreIgnoredWithSpaceInRepoPath() throws {
try testWithTemporaryDirectory { path in
// Create a repo.
let testRepoPath = path.appending("test repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath)
let repo = GitRepository(path: testRepoPath)
// Add a .gitignore
try localFileSystem.writeFileContents(testRepoPath.appending(".gitignore"), bytes: "ignored_file1")
let ignored = try repo.areIgnored([testRepoPath.appending("ignored_file1")])
XCTAssertTrue(ignored[0])
}
}
func testMissingDefaultBranch() throws {
try testWithTemporaryDirectory { path in
// Create a repository.
let testRepoPath = path.appending("test-repo")
try makeDirectories(testRepoPath)
initGitRepo(testRepoPath)
let repo = GitRepository(path: testRepoPath)
// Create a `newMain` branch and remove `main`.
try repo.checkout(newBranch: "newMain")
try systemQuietly([Git.tool, "-C", testRepoPath.pathString, "branch", "-D", "main"])
// Change the branch name to something non-existent.
try systemQuietly([Git.tool, "-C", testRepoPath.pathString, "symbolic-ref", "HEAD", "refs/heads/_non_existent_branch_"])
// Clone it somewhere.
let testClonePath = path.appending("clone")
let provider = GitRepositoryProvider()
let repoSpec = RepositorySpecifier(path: testRepoPath)
try provider.fetch(repository: repoSpec, to: testClonePath)
let clonedRepo = provider.open(repository: repoSpec, at: testClonePath)
XCTAssertEqual(try clonedRepo.getTags(), [])
// Clone off a checkout.
let checkoutPath = path.appending("checkout")
let checkoutRepo = try provider.createWorkingCopy(repository: repoSpec, sourcePath: testClonePath, at: checkoutPath, editable: false)
XCTAssertNoSuchPath(checkoutPath.appending("file.swift"))
// Try to check out the `main` branch.
try checkoutRepo.checkout(revision: Revision(identifier: "newMain"))
XCTAssertFileExists(checkoutPath.appending("file.swift"))
// The following will throw if HEAD was set incorrectly and we didn't do a no-checkout clone.
XCTAssertNoThrow(try checkoutRepo.getCurrentRevision())
}
}
func testValidDirectoryLocalRelativeOrigin() async throws {
try testWithTemporaryDirectory { tmpDir in
// Create a repository.
let packageDir = tmpDir.appending("SomePackage")
try localFileSystem.createDirectory(packageDir)
// Create a repository manager for it.
let repoProvider = GitRepositoryProvider()
let repositoryManager = RepositoryManager(
fileSystem: localFileSystem,
path: packageDir,
provider: repoProvider,
delegate: .none
)
let customRemote = "../OriginOfSomePackage.git"
// Before initializing the directory with a git repo, it is never valid.
XCTAssertThrowsError(try repositoryManager.isValidDirectory(packageDir))
XCTAssertThrowsError(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(packageDir.pathString))))
XCTAssertThrowsError(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(customRemote))))
initGitRepo(packageDir)
// Set the remote.
try systemQuietly([Git.tool, "-C", packageDir.pathString, "remote", "add", "origin", customRemote])
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir))
let customRemoteWithoutPathExtension = (customRemote as NSString).deletingPathExtension
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(customRemote))))
// We consider the directory valid even if the remote does not have the same path extension - in this case we expected '.git'.
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(customRemoteWithoutPathExtension))))
// We consider the directory valid even if the remote does not have the same path extension - in this case we expected '.git'.
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL((customRemote as NSString).deletingPathExtension + "/"))))
// The following ensure that are actually checking the remote's origin.
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: AbsolutePath(validating: "/"))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL("/"))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: packageDir)))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(packageDir.pathString))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: packageDir.appending(extension: "git"))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(packageDir.pathString.appending(".git")))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL("https://mycustomdomain/some-package.git"))))
}
}
func testValidDirectoryLocalAbsoluteOrigin() async throws {
try testWithTemporaryDirectory { tmpDir in
// Create a repository.
let packageDir = tmpDir.appending("SomePackage")
try localFileSystem.createDirectory(packageDir)
// Create a repository manager for it.
let repoProvider = GitRepositoryProvider()
let repositoryManager = RepositoryManager(
fileSystem: localFileSystem,
path: packageDir,
provider: repoProvider,
delegate: .none
)
let customRemote = tmpDir.appending("OriginOfSomePackage.git")
// Before initializing the directory with a git repo, it is never valid.
XCTAssertThrowsError(try repositoryManager.isValidDirectory(packageDir))
XCTAssertThrowsError(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(packageDir.pathString))))
XCTAssertThrowsError(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(customRemote.pathString))))
initGitRepo(packageDir)
// Set the remote.
try systemQuietly([Git.tool, "-C", packageDir.pathString, "remote", "add", "origin", customRemote.pathString])
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir))
let customRemotePath = customRemote.pathString
let customRemotePathWithoutPathExtension = (customRemotePath as NSString).deletingPathExtension
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: customRemote)))
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(customRemotePath))))
// We consider the directory valid even if the remote does not have the same path extension - in this case we expected '.git'.
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: try AbsolutePath(validating: customRemotePathWithoutPathExtension))))
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(customRemotePathWithoutPathExtension))))
// We consider the directory valid even if the remote does not have the same path extension - in this case we expected '.git'.
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: try AbsolutePath(validating: customRemotePathWithoutPathExtension + "/"))))
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL((customRemotePath as NSString).deletingPathExtension + "/"))))
// The following ensure that are actually checking the remote's origin.
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: AbsolutePath(validating: "/"))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL("/"))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: packageDir)))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(packageDir.pathString))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: packageDir.appending(extension: "git"))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(packageDir.pathString.appending(".git")))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL("https://mycustomdomain/some-package.git"))))
}
}
func testValidDirectoryRemoteOrigin() async throws {
try testWithTemporaryDirectory { tmpDir in
// Create a repository.
let packageDir = tmpDir.appending("SomePackage")
try localFileSystem.createDirectory(packageDir)
// Create a repository manager for it.
let repoProvider = GitRepositoryProvider()
let repositoryManager = RepositoryManager(
fileSystem: localFileSystem,
path: packageDir,
provider: repoProvider,
delegate: .none
)
let customRemote = try XCTUnwrap(URL(string: "https://mycustomdomain/some-package.git"))
// Before initializing the directory with a git repo, it is never valid.
XCTAssertThrowsError(try repositoryManager.isValidDirectory(packageDir))
XCTAssertThrowsError(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(customRemote))))
initGitRepo(packageDir)
// Set the remote.
try systemQuietly([Git.tool, "-C", packageDir.pathString, "remote", "add", "origin", customRemote.absoluteString])
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir))
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(customRemote))))
// We consider the directory valid even if the remote does not have the same path extension - in this case we expected '.git'.
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL("https://mycustomdomain/some-package"))))
// We consider the directory valid even if the remote does not have the same path extension - in this case we expected '.git'.
XCTAssertTrue(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL("https://mycustomdomain/some-package/"))))
// The following ensure that are actually checking the remote's origin.
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: AbsolutePath(validating: "/"))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL("/"))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: packageDir)))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(packageDir.pathString))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(path: packageDir.appending(extension: "git"))))
XCTAssertFalse(try repositoryManager.isValidDirectory(packageDir, for: RepositorySpecifier(url: SourceControlURL(packageDir.pathString.appending(".git")))))
}
}
}
|