GitLiteTests.swift (7130B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import XCTest 3 @testable import GitLite 4 import GitTestSupport 5 6 final class GitLiteTests: XCTestCase { 7 func testObjectIDs() throws { 8 // `git hash-object` of an empty file and of "hello\n" 9 XCTAssertEqual(GitObject(type: .blob, data: []).id.hex, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391") 10 XCTAssertEqual(GitObject(type: .blob, data: Array("hello\n".utf8)).id.hex, "ce013625030ba8dba906f756967f9e9ca394464a") 11 XCTAssertEqual(ObjectID(hex: "ce013625030ba8dba906f756967f9e9ca394464a")?.hex, "ce013625030ba8dba906f756967f9e9ca394464a") 12 XCTAssertNil(ObjectID(hex: "xyz")) 13 } 14 15 func testPackRoundTrip() throws { 16 let objects = [ 17 GitObject(type: .blob, data: []), 18 GitObject(type: .blob, data: Array(repeating: 65, count: 100_000)), 19 GitObject(type: .blob, data: (0..<5000).map { UInt8(truncatingIfNeeded: $0 &* 31) }), 20 ] 21 let parsed = try Pack.parse(Pack.write(objects)) 22 XCTAssertEqual(parsed, objects) 23 } 24 25 func testCorruptPackIsRejected() throws { 26 var pack = Pack.write([GitObject(type: .blob, data: Array("abc".utf8))]) 27 pack[14] ^= 0xff 28 XCTAssertThrowsError(try Pack.parse(pack)) 29 } 30 31 func testTreeOrderMatchesGit() throws { 32 let repo = try LocalGit.create() 33 try repo.write("a.txt", "1") 34 try repo.write("a/x", "2") 35 try repo.write("a-b", "3") 36 try repo.write("a0", "4") 37 try repo.write("Z", "5") 38 try repo.commitAll("order") 39 let raw = try repo.git(["cat-file", "tree", "HEAD^{tree}"]) 40 let entries = try Tree.parse(raw) 41 XCTAssertEqual(entries.map(\.name), ["Z", "a-b", "a.txt", "a", "a0"]) 42 XCTAssertEqual(Tree.serialize(entries.shuffled()), raw) 43 } 44 45 /// Repository with history and deltas, read back in full by our client. 46 func testFetchMatchesRealGit() async throws { 47 let repo = try LocalGit.create() 48 var body = (0..<400).map { "line \($0) of the dive profile\n" }.joined() 49 for i in 0..<12 { 50 body += "addition \(i)\n" 51 try repo.write("2024/05/dive-\(i % 3)/Divecomputer", body) 52 try repo.write("00-Subsurface", "version 3\nrev \(i)\n") 53 try repo.commitAll("commit \(i)") 54 } 55 try repo.git(["gc", "-q", "--aggressive"]) 56 let head = ObjectID(hex: try repo.text(["rev-parse", "HEAD"]))! 57 58 let store = MemoryObjectStore() 59 let client = GitClient(transport: LocalProcessTransport(repository: repo.directory), store: store) 60 let remote = try await client.refs() 61 XCTAssertEqual(remote.branch("main"), head) 62 try await client.fetch(want: head, capabilities: remote.capabilities) 63 64 // Every object of the repository is there and identical. 65 let listing = try repo.text(["rev-list", "--objects", "--all"]) 66 var checked = 0 67 for line in listing.split(separator: "\n") { 68 let id = ObjectID(hex: line.prefix(40))! 69 let object = try XCTUnwrap(store.get(id), "missing object: \(line)") 70 let type = try repo.text(["cat-file", "-t", id.hex]) 71 XCTAssertEqual(object.type.name, type) 72 XCTAssertEqual(object.data, try repo.git(["cat-file", type, id.hex])) 73 checked += 1 74 } 75 XCTAssertGreaterThan(checked, 40) 76 let commit = try store.commit(head) 77 XCTAssertEqual(commit.parents.count, 1) 78 XCTAssertEqual(commit.message, "commit 11\n") 79 } 80 81 func testShallowThenIncrementalFetch() async throws { 82 let repo = try LocalGit.create() 83 try repo.write("a", "one\n") 84 try repo.commitAll("one") 85 try repo.write("b", "two\n") 86 let second = try repo.commitAll("two") 87 88 let store = MemoryObjectStore() 89 let client = GitClient(transport: LocalProcessTransport(repository: repo.directory), store: store) 90 var remote = try await client.refs() 91 let shallow = try await client.fetch(want: second, depth: 1, capabilities: remote.capabilities) 92 XCTAssertEqual(shallow, [second]) 93 XCTAssertEqual(store.count, 4) // 1 commit, 1 tree, 2 blobs: no history 94 95 try repo.write("c", "three\n") 96 let third = try repo.commitAll("three") 97 remote = try await client.refs() 98 let again = try await client.fetch(want: third, have: [second], shallow: shallow, depth: 1, capabilities: remote.capabilities) 99 XCTAssertEqual(again, [third]) 100 XCTAssertEqual(store.count, 7) // + 1 commit, 1 tree, 1 blob only 101 XCTAssertEqual(try store.tree(try store.commit(third).tree).map(\.name), ["a", "b", "c"]) 102 } 103 104 func testPushCreatesAndUpdatesBranch() async throws { 105 let repo = try LocalGit.create(bare: true) 106 let store = MemoryObjectStore() 107 let client = GitClient(transport: LocalProcessTransport(repository: repo.directory), store: store) 108 109 let blob = GitObject(type: .blob, data: Array("version 3\n".utf8)) 110 let tree = GitObject(type: .tree, data: Tree.serialize([.blob("00-Subsurface", blob.id)])) 111 let who = Commit.signature(name: "Test", email: "test@example.com", date: Date(timeIntervalSince1970: 1_700_000_000)) 112 let first = GitObject(type: .commit, data: Commit(tree: tree.id, parents: [], author: who, committer: who, message: "first\n").serialize()) 113 try await client.push(ref: "refs/heads/jean@example.com", old: nil, new: first.id, objects: [blob, tree, first]) 114 XCTAssertEqual(try repo.text(["rev-parse", "refs/heads/jean@example.com"]), first.id.hex) 115 XCTAssertEqual(try repo.text(["show", "jean@example.com:00-Subsurface"]), "version 3") 116 try repo.git(["fsck", "--strict"]) 117 118 let blob2 = GitObject(type: .blob, data: Array("notes\n".utf8)) 119 let sub = GitObject(type: .tree, data: Tree.serialize([.blob("Dive-1", blob2.id)])) 120 let tree2 = GitObject(type: .tree, data: Tree.serialize([.blob("00-Subsurface", blob.id), .tree("2024", sub.id)])) 121 let second = GitObject(type: .commit, data: Commit(tree: tree2.id, parents: [first.id], author: who, committer: who, message: "second\n").serialize()) 122 try await client.push(ref: "refs/heads/jean@example.com", old: first.id, new: second.id, objects: [blob2, sub, tree2, second]) 123 XCTAssertEqual(try repo.text(["log", "--format=%s", "jean@example.com"]), "second\nfirst") 124 try repo.git(["fsck", "--strict"]) 125 126 // An update from a stale state is rejected. 127 let third = GitObject(type: .commit, data: Commit(tree: tree.id, parents: [first.id], author: who, committer: who, message: "stale\n").serialize()) 128 do { 129 try await client.push(ref: "refs/heads/jean@example.com", old: first.id, new: third.id, objects: [third]) 130 XCTFail("the push should have been rejected") 131 } catch let error as GitError { 132 guard case .pushRejected = error else { return XCTFail("\(error)") } 133 } 134 XCTAssertEqual(try repo.text(["rev-parse", "refs/heads/jean@example.com"]), second.id.hex) 135 } 136 }