| 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).toMatchObject |
| 83 | |
| 84 | const mergeCommitSha = |
| 85 | gitDir: baseDir, |
| 86 | alternates, |
| 87 | tree: 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('names the conflicted paths 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 | ).toMatchObject({ clean |
| 143 | }); |
| 144 | |
| 145 | it('replays the head onto the base, keeping authors and dropping merge commits', |
| 146 | const fork = |
| 147 | const work = |
| 148 | writeFileSync(path.join |
| 149 | git(fork, ' |
| 150 | git( |
| 151 | fork, |
| 152 | 'commit', |
| 153 | '-m', |
| 154 | 'second commit', |
| 155 | '-m', |
| 156 | 'with a body', |
| 157 | '--author', |
| 158 | 'Alice <alice@example.com>', |
| 159 | ); |
| 160 | git(fork, ' |
| 161 | git(fork, ' |
| 162 | git(fork, ' |
| 163 | const head = |
| 164 | |
| 165 | const rebased = |
| 166 | gitDir: baseDir, |
| 167 | alternates: [headDir], |
| 168 | onto: baseSha, |
| 169 | from: git(fork |
| 170 | to: head, |
| 171 | committer: { name: ' |
| 172 | }); |
| 173 | if (!rebased.clean |
| 174 | |
| 175 | const log = |
| 176 | git(baseDir, ' |
| 177 | expect( |
| 178 | log('%P') |
| 179 | .split('\n') |
| 180 | .map((parents) |
| 181 | ).toEqual([1, |
| 182 | expect(log('%an <%ae>|%cn |
| 183 | 'Alice <alice@example.com>|Merger\nTest <test@example.com>|Merger', |
| 184 | ); |
| 185 | expect(git(baseDir |
| 186 | 'second commit\n\nwith a body', |
| 187 | ); |
| 188 | expect(log('%ad |
| 189 | git(fork, ' |
| 190 | ); |
| 191 | // merge-tree skips writing a tree the lent store already holds, so reading it back needs the same loan |
| 192 | const tree = |
| 193 | encoding: 'utf8', |
| 194 | env: { |
| 195 | ...process.env, |
| 196 | GIT_DIR: baseDir, |
| 197 | GIT_ALTERNATE_OBJECT_DIRECTORIES: path.join |
| 198 | }, |
| 199 | }); |
| 200 | expect(tree.trim |
| 201 | 'feature.txt', |
| 202 | 'second.txt', |
| 203 | 'shared.txt', |
| 204 | 'upstream.txt', |
| 205 | ]); |
| 206 | }); |
| 207 | |
| 208 | it('names the conflicted paths of the first commit that cannot be replayed', |
| 209 | const fork = |
| 210 | const work = |
| 211 | writeFileSync(path.join |
| 212 | git(fork, ' |
| 213 | git(fork, ' |
| 214 | git(fork, ' |
| 215 | |
| 216 | writeFileSync(path.join |
| 217 | git(work, ' |
| 218 | git(work, ' |
| 219 | git(work, ' |
| 220 | |
| 221 | expect( |
| 222 | await rebaseCommits({ |
| 223 | gitDir: baseDir, |
| 224 | alternates: [headDir], |
| 225 | onto: git(work |
| 226 | from: git(work |
| 227 | to: git(fork |
| 228 | committer: { name: ' |
| 229 | }), |
| 230 | ).toEqual({ clean |
| 231 | }); |
| 232 | }); |