| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { cpSync, mkdtempSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { afterEach, beforeEach |
| 6 | |
| 7 | import { commitTree, mergeTree |
| 8 | |
| 9 | const identity = { |
| 10 | GIT_AUTHOR_NAME: 'Test', |
| 11 | GIT_AUTHOR_EMAIL: 'test@example.com', |
| 12 | GIT_COMMITTER_NAME: 'Test', |
| 13 | GIT_COMMITTER_EMAIL: 'test@example.com', |
| 14 | }; |
| 15 | |
| 16 | /** A fork's log is copied at fork time and never shares a write again, so the two caches hold disjoint objects. These cover the merge running against that. */ |
| 17 | describe('merging across two repositories', () |
| 18 | let root: |
| 19 | let baseDir: |
| 20 | let headDir: |
| 21 | let baseSha: |
| 22 | let headSha: |
| 23 | |
| 24 | const git = |
| 25 | const env: |
| 26 | delete env.GIT_DIR; |
| 27 | return execFileSync('git |
| 28 | }; |
| 29 | |
| 30 | beforeEach(() => |
| 31 | root = mkdtempSync( |
| 32 | const work = |
| 33 | baseDir = path. |
| 34 | headDir = path. |
| 35 | |
| 36 | execFileSync('git', |
| 37 | git(work, ' |
| 38 | git(work, ' |
| 39 | writeFileSync(path.join |
| 40 | git(work, ' |
| 41 | git(work, ' |
| 42 | execFileSync('git', |
| 43 | |
| 44 | // the fork: a byte copy of the log, exactly what copyLog produces |
| 45 | cpSync(baseDir, headDir |
| 46 | |
| 47 | const fork = |
| 48 | execFileSync('git', |
| 49 | git(fork, ' |
| 50 | git(fork, ' |
| 51 | git(fork, ' |
| 52 | writeFileSync(path.join |
| 53 | git(fork, ' |
| 54 | git(fork, ' |
| 55 | git(fork, ' |
| 56 | headSha = git( |
| 57 | |
| 58 | // the base moves on independently after the fork |
| 59 | writeFileSync(path.join |
| 60 | git(work, ' |
| 61 | git(work, ' |
| 62 | git(work, ' |
| 63 | baseSha = git( |
| 64 | }); |
| 65 | |
| 66 | afterEach(() => |
| 67 | |
| 68 | it('cannot see the other side without the alternate', |
| 69 | await expect( |
| 70 | mergeTree({ gitDir: |
| 71 | ).rejects.toThrow() |
| 72 | }); |
| 73 | |
| 74 | it('builds a pack a node that never saw the fork can replay', |
| 75 | const alternates = |
| 76 | const tree = |
| 77 | gitDir: baseDir, |
| 78 | alternates, |
| 79 | base: baseSha, |
| 80 | head: headSha, |
| 81 | }); |
| 82 | expect(tree).not |
| 83 | |
| 84 | const mergeCommitSha = |
| 85 | gitDir: baseDir, |
| 86 | alternates, |
| 87 | tree: tree!, |
| 88 | parents: [baseSha, |
| 89 | message: 'Merge pull request #1\n', |
| 90 | author: { name: ' |
| 91 | }); |
| 92 | |
| 93 | const pack = |
| 94 | gitDir: baseDir, |
| 95 | alternates, |
| 96 | include: mergeCommitSha, |
| 97 | exclude: [baseSha], |
| 98 | prefix: path.join |
| 99 | }); |
| 100 | expect(pack.size |
| 101 | |
| 102 | // a cold node: the base repository's objects, then this entry, nothing else |
| 103 | const cold = |
| 104 | execFileSync('git', |
| 105 | cpSync(path.join |
| 106 | recursive: true, |
| 107 | }); |
| 108 | execFileSync('git', |
| 109 | cwd: cold, |
| 110 | env: { ...process |
| 111 | input: execFileSync('cat |
| 112 | }); |
| 113 | |
| 114 | expect(git(cold |
| 115 | expect(git(cold |
| 116 | expect( |
| 117 | git(cold, ' |
| 118 | ).not.toMatch( |
| 119 | }); |
| 120 | |
| 121 | it('returns null instead of throwing when the merge conflicts', |
| 122 | const fork = |
| 123 | const work = |
| 124 | |
| 125 | writeFileSync(path.join |
| 126 | git(fork, ' |
| 127 | git(fork, ' |
| 128 | git(fork, ' |
| 129 | |
| 130 | writeFileSync(path.join |
| 131 | git(work, ' |
| 132 | git(work, ' |
| 133 | git(work, ' |
| 134 | |
| 135 | expect( |
| 136 | await mergeTree({ |
| 137 | gitDir: baseDir, |
| 138 | alternates: [headDir], |
| 139 | base: git(work |
| 140 | head: git(fork |
| 141 | }), |
| 142 | ).toBeNull(); |
| 143 | }); |
| 144 | }); |