Debian Codegraph @

cli/tests/test_gerrit_upload.rs

raw
1 // Copyright 2025 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use testutils::TestResult;
16
17 use crate::common::TestEnvironment;
18 use crate::common::create_commit;
19 use crate::common::create_commit_with_files;
20
21 #[test]
22 fn test_gerrit_upload_dryrun() {
23 let test_env = TestEnvironment::default();
24 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
25 let work_dir = test_env.work_dir("repo");
26
27 create_commit(&work_dir, "a", &[]);
28 create_commit(&work_dir, "b", &["a"]);
29 create_commit(&work_dir, "c", &["a"]);
30 let output = work_dir.run_jj(["gerrit", "upload", "-r", "b"]);
31 insta::assert_snapshot!(output, @"
32 ------- stderr -------
33 Error: No remote specified, and no 'gerrit' remote was found
34 [EOF]
35 [exit status: 1]
36 ");
37
38 // With remote specified but.
39 test_env.add_config(r#"gerrit.default-remote="origin""#);
40 let output = work_dir.run_jj(["gerrit", "upload", "-r", "b"]);
41 insta::assert_snapshot!(output, @"
42 ------- stderr -------
43 Error: The remote 'origin' (configured via `gerrit.default-remote`) does not exist
44 [EOF]
45 [exit status: 1]
46 ");
47
48 let output = work_dir.run_jj(["gerrit", "upload", "-r", "b", "--remote=origin"]);
49 insta::assert_snapshot!(output, @"
50 ------- stderr -------
51 Error: The remote 'origin' (specified via `--remote`) does not exist
52 [EOF]
53 [exit status: 1]
54 ");
55
56 let output = work_dir.run_jj([
57 "git",
58 "remote",
59 "add",
60 "origin",
61 "http://example.com/repo/foo",
62 ]);
63 insta::assert_snapshot!(output, @"");
64 let output = work_dir.run_jj(["gerrit", "upload", "-r", "b", "--remote=origin"]);
65 insta::assert_snapshot!(output, @"
66 ------- stderr -------
67 Error: No target branch specified via --remote-branch, and no 'gerrit.default-remote-branch' was found
68 [EOF]
69 [exit status: 1]
70 ");
71
72 test_env.add_config(r#"gerrit.default-remote-branch="main""#);
73 let output = work_dir.run_jj(["gerrit", "upload", "-r", "b", "--dry-run"]);
74 insta::assert_snapshot!(output, @r"
75 ------- stderr -------
76 Found 1 heads to push to Gerrit (remote 'origin'), target branch 'main'.
77 Dry-run: Would push zsuskuln 123b4d91 b | b
78 [EOF]
79 ");
80
81 let output = work_dir.run_jj(["gerrit", "upload", "-r", "b", "--dry-run", "-b", "other"]);
82 insta::assert_snapshot!(output, @r"
83 ------- stderr -------
84 Found 1 heads to push to Gerrit (remote 'origin'), target branch 'other'.
85 Dry-run: Would push zsuskuln 123b4d91 b | b
86 [EOF]
87 ");
88 }
89
90 #[test]
91 fn test_gerrit_upload_default_revision() {
92 let test_env = TestEnvironment::default();
93 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
94 let work_dir = test_env.work_dir("repo");
95
96 work_dir
97 .run_jj([
98 "git",
99 "remote",
100 "add",
101 "origin",
102 "http://example.com/repo/foo",
103 ])
104 .success();
105 test_env.add_config(r#"gerrit.default-remote="origin""#);
106 test_env.add_config(r#"gerrit.default-remote-branch="main""#);
107
108 work_dir
109 .run_jj(["new", "--message", "parent", "root()"])
110 .success();
111 work_dir.write_file("parent", "parent");
112 let output = work_dir.run_jj(["gerrit", "upload", "--dry-run"]);
113 insta::assert_snapshot!(output, @r"
114 ------- stderr -------
115 No revision provided. Defaulting to @
116 Found 1 heads to push to Gerrit (remote 'origin'), target branch 'main'.
117 Dry-run: Would push kkmpptxz a41ea4e9 parent
118 [EOF]
119 ");
120
121 work_dir.run_jj(["new"]).success();
122 let output = work_dir.run_jj(["gerrit", "upload", "--dry-run"]);
123 insta::assert_snapshot!(output, @r"
124 ------- stderr -------
125 No revision provided and @ has no description. Defaulting to @-
126 Found 1 heads to push to Gerrit (remote 'origin'), target branch 'main'.
127 Dry-run: Would push kkmpptxz a41ea4e9 parent
128 [EOF]
129 ");
130
131 work_dir.run_jj(["new", "@", "@-"]).success();
132 let output = work_dir.run_jj(["gerrit", "upload", "--dry-run"]);
133 insta::assert_snapshot!(output, @r"
134 ------- stderr -------
135 Error: No revision provided, and @ is a merge commit with no description. Unable to determine a suitable default commit to upload.
136 Hint: Explicitly specify a revision to upload with `-r`
137 [EOF]
138 [exit status: 1]
139 ");
140
141 work_dir.run_jj(["workspace", "forget"]).success();
142 let output = work_dir.run_jj(["gerrit", "upload", "--dry-run"]);
143 insta::assert_snapshot!(output, @"
144 ------- stderr -------
145 Error: No revision provided
146 Hint: Explicitly specify a revision to upload with `-r`
147 [EOF]
148 [exit status: 1]
149 ");
150 }
151
152 #[test]
153 fn test_gerrit_upload_default_revision_already_in_trunk() {
154 let test_env = TestEnvironment::default();
155 test_env
156 .run_jj_in(".", ["git", "init", "--colocate", "remote"])
157 .success();
158 let remote_dir = test_env.work_dir("remote");
159 create_commit(&remote_dir, "main", &[]);
160
161 test_env
162 .run_jj_in(".", ["git", "clone", "remote", "local"])
163 .success();
164 let local_dir = test_env.work_dir("local");
165 test_env.add_config(r#"gerrit.default-remote="origin""#);
166 test_env.add_config(r#"gerrit.default-remote-branch="main""#);
167 test_env.add_config(r#"revset-aliases."trunk()" = "main@origin""#);
168
169 // Make @ an empty working-copy commit over immutable main@origin.
170 local_dir.run_jj(["new", "main@origin"]).success();
171 local_dir.run_jj(["describe", "-m="]).success();
172
173 let output = local_dir.run_jj(["gerrit", "upload", "--dry-run"]);
174 insta::assert_snapshot!(output, @r#"
175 ------- stderr -------
176 No revision provided and @ has no description. Defaulting to @-
177 Error: Commit 57df4838fd85 is immutable
178 Hint: Could not modify commit: rlvkpnrz 57df4838 main@origin | main
179 Hint: Immutable commits are used to protect shared history.
180 Hint: For more information, see:
181 - https://docs.jj-vcs.dev/latest/config/#set-of-immutable-commits
182 - `jj help -k config`, "Set of immutable commits"
183 Hint: This operation would rewrite 1 immutable commits.
184 [EOF]
185 [exit status: 1]
186 "#);
187 }
188
189 #[test]
190 fn test_gerrit_upload_option_failure() {
191 let test_env = TestEnvironment::default();
192 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
193 let work_dir = test_env.work_dir("repo");
194
195 // upload options are validated before anything else
196 // malformed custom option
197 let output = work_dir.run_jj(["gerrit", "upload", "--custom", "foo"]);
198 insta::assert_snapshot!(output, @"
199 ------- stderr -------
200 Error: Custom values must be of the form 'key:value'. Got foo
201 [EOF]
202 [exit status: 1]
203 ");
204
205 // mutually exclusive flags
206 let output = work_dir.run_jj(["gerrit", "upload", "--wip", "--ready"]);
207 insta::assert_snapshot!(output, @"
208 ------- stderr -------
209 Error: --wip and --ready are mutually exclusive
210 [EOF]
211 [exit status: 1]
212 ");
213 let output = work_dir.run_jj(["gerrit", "upload", "--private", "--remove-private"]);
214 insta::assert_snapshot!(output, @"
215 ------- stderr -------
216 Error: --private and --remove-private are mutually exclusive
217 [EOF]
218 [exit status: 1]
219 ");
220 let output = work_dir.run_jj([
221 "gerrit",
222 "upload",
223 "--publish-comments",
224 "--no-publish-comments",
225 ]);
226 insta::assert_snapshot!(output, @"
227 ------- stderr -------
228 Error: --publish-comments and --no-publish-comments are mutually exclusive
229 [EOF]
230 [exit status: 1]
231 ");
232
233 // cannot skip validation without submitting
234 let output = work_dir.run_jj(["gerrit", "upload", "--skip-validation"]);
235 insta::assert_snapshot!(output, @"
236 ------- stderr -------
237 Error: --skip-validation is only supported for --submit
238 [EOF]
239 [exit status: 1]
240 ");
241 }
242
243 #[test]
244 fn test_gerrit_upload_failure() {
245 let test_env = TestEnvironment::default();
246 test_env
247 .run_jj_in(".", ["git", "init", "--colocate", "remote"])
248 .success();
249 let remote_dir = test_env.work_dir("remote");
250 create_commit(&remote_dir, "a", &[]);
251
252 test_env
253 .run_jj_in(".", ["git", "clone", "remote", "local"])
254 .success();
255 let local_dir = test_env.work_dir("local");
256
257 // construct test revisions
258 create_commit_with_files(&local_dir, "b", &["a@origin"], &[]);
259 create_commit(&local_dir, "c", &["a@origin"]);
260 local_dir.run_jj(["describe", "-m="]).success();
261 create_commit(&local_dir, "d", &["a@origin"]);
262
263 let output = local_dir.run_jj(["gerrit", "upload", "-r", "none()", "--remote-branch=main"]);
264 insta::assert_snapshot!(output, @"
265 ------- stderr -------
266 No revisions to upload.
267 [EOF]
268 ");
269
270 // empty revisions are not allowed
271 let output = local_dir.run_jj(["gerrit", "upload", "-r", "b", "--remote-branch=main"]);
272 insta::assert_snapshot!(output, @"
273 ------- stderr -------
274 Error: Refusing to upload revision mzvwutvlkqwt because it is empty
275 Hint: Perhaps you squashed then ran upload? Maybe you meant to upload the parent commit instead (eg. @-)
276 [EOF]
277 [exit status: 1]
278 ");
279
280 // empty descriptions are not allowed
281 let output = local_dir.run_jj(["gerrit", "upload", "-r", "c", "--remote-branch=main"]);
282 insta::assert_snapshot!(output, @"
283 ------- stderr -------
284 Error: Refusing to upload revision yqosqzytrlsw because it is has no description
285 Hint: Maybe you meant to upload the parent commit instead (eg. @-)
286 [EOF]
287 [exit status: 1]
288 ");
289
290 // upload failure
291 local_dir
292 .run_jj(["git", "remote", "set-url", "origin", "nonexistent"])
293 .success();
294 let output = local_dir.run_jj(["gerrit", "upload", "-r", "d", "--remote-branch=main"]);
295 insta::assert_snapshot!(output, @r"
296 ------- stderr -------
297 Found 1 heads to push to Gerrit (remote 'origin'), target branch 'main'.
298 Pushing znkkpsqq 47f1f88c d | d
299 Error: Internal git error while pushing to gerrit
300 Caused by: Could not find repository at '$TEST_ENV/local/nonexistent'
301 [EOF]
302 [exit status: 1]
303 ");
304 }
305
306 #[test]
307 fn test_gerrit_upload_local_implicit_change_ids() {
308 let test_env = TestEnvironment::default();
309 test_env
310 .run_jj_in(".", ["git", "init", "--colocate", "remote"])
311 .success();
312 let remote_dir = test_env.work_dir("remote");
313 create_commit(&remote_dir, "a", &[]);
314
315 test_env
316 .run_jj_in(".", ["git", "clone", "remote", "local"])
317 .success();
318 let local_dir = test_env.work_dir("local");
319 create_commit(&local_dir, "b", &["a@origin"]);
320 create_commit(&local_dir, "c", &["b"]);
321
322 // Ensure other trailers are preserved (no extra newlines)
323 local_dir
324 .run_jj([
325 "describe",
326 "c",
327 "-m",
328 "c\n\nSigned-off-by: Lucky K Maintainer <[email protected]>\n",
329 ])
330 .success();
331
332 // The output should only mention commit IDs from the log output above (no
333 // temporary commits)
334 let output = local_dir.run_jj(["log", "-r", "all()"]);
335 insta::assert_snapshot!(output, @"
336 @ yqosqzyt [email protected] 2001-02-03 08:05:15 c f6e97ced
337 │ c
338 ○ mzvwutvl [email protected] 2001-02-03 08:05:12 b 3bcb28c4
339 │ b
340 ◆ rlvkpnrz [email protected] 2001-02-03 08:05:09 a@origin 7d980be7
341 │ a
342 ◆ zzzzzzzz root() 00000000
343 [EOF]
344 ");
345
346 let output = local_dir.run_jj(["gerrit", "upload", "-r", "c", "--remote-branch=main"]);
347 insta::assert_snapshot!(output, @r"
348 ------- stderr -------
349 Found 1 heads to push to Gerrit (remote 'origin'), target branch 'main'.
350 Pushing yqosqzyt f6e97ced c | c
351 [EOF]
352 ");
353
354 // The output should be unchanged because we only add Change-Id trailers
355 // transiently
356 let output = local_dir.run_jj(["log", "-r", "all()"]);
357 insta::assert_snapshot!(output, @"
358 @ yqosqzyt [email protected] 2001-02-03 08:05:15 c f6e97ced
359 │ c
360 ○ mzvwutvl [email protected] 2001-02-03 08:05:12 b 3bcb28c4
361 │ b
362 ◆ rlvkpnrz [email protected] 2001-02-03 08:05:09 a@origin 7d980be7
363 │ a
364 ◆ zzzzzzzz root() 00000000
365 [EOF]
366 ");
367
368 // There's no particular reason to run this with jj util exec, it's just that
369 // the infra makes it easier to run this way.
370 let output = remote_dir.run_jj(["util", "exec", "--", "git", "log", "refs/for/main"]);
371 insta::assert_snapshot!(output, @"
372 commit 68b986d2eb820643b767ae219fb48128dcc2fc03
373 Author: Test User <[email protected]>
374 Date: Sat Feb 3 04:05:13 2001 +0700
375
376 c
377
378 Signed-off-by: Lucky K Maintainer <[email protected]>
379 Change-Id: I19b790168e73f7a73a98deae21e807c06a6a6964
380
381 commit 81b723522d1c1a583a045eab5bfb323e45e6198d
382 Author: Test User <[email protected]>
383 Date: Sat Feb 3 04:05:11 2001 +0700
384
385 b
386
387 Change-Id: Id043564ef93650b06a70f92f9d91912b6a6a6964
388
389 commit 7d980be7a1d499e4d316ab4c01242885032f7eaf
390 Author: Test User <[email protected]>
391 Date: Sat Feb 3 04:05:08 2001 +0700
392
393 a
394 [EOF]
395 ");
396 }
397
398 #[test]
399 fn test_gerrit_upload_local_implicit_change_id_link() {
400 let test_env = TestEnvironment::default();
401 test_env.add_config(
402 r#"
403 [gerrit]
404 review-url = "https://gerrit.example.com/"
405 "#,
406 );
407 test_env
408 .run_jj_in(".", ["git", "init", "--colocate", "remote"])
409 .success();
410 let remote_dir = test_env.work_dir("remote");
411 create_commit(&remote_dir, "a", &[]);
412
413 test_env
414 .run_jj_in(".", ["git", "clone", "remote", "local"])
415 .success();
416 let local_dir = test_env.work_dir("local");
417 create_commit(&local_dir, "b", &["a@origin"]);
418 create_commit(&local_dir, "c", &["b"]);
419
420 // Ensure other trailers are preserved (no extra newlines)
421 local_dir
422 .run_jj([
423 "describe",
424 "c",
425 "-m",
426 "c\n\nSigned-off-by: Lucky K Maintainer <[email protected]>\n",
427 ])
428 .success();
429
430 // The output should only mention commit IDs from the log output above (no
431 // temporary commits)
432 let output = local_dir.run_jj(["log", "-r", "all()"]);
433 insta::assert_snapshot!(output, @"
434 @ yqosqzyt [email protected] 2001-02-03 08:05:15 c f6e97ced
435 │ c
436 ○ mzvwutvl [email protected] 2001-02-03 08:05:12 b 3bcb28c4
437 │ b
438 ◆ rlvkpnrz [email protected] 2001-02-03 08:05:09 a@origin 7d980be7
439 │ a
440 ◆ zzzzzzzz root() 00000000
441 [EOF]
442 ");
443
444 let output = local_dir.run_jj(["gerrit", "upload", "-r", "c", "--remote-branch=main"]);
445 insta::assert_snapshot!(output, @r"
446 ------- stderr -------
447 Found 1 heads to push to Gerrit (remote 'origin'), target branch 'main'.
448 Pushing yqosqzyt f6e97ced c | c
449 [EOF]
450 ");
451
452 // The output should be unchanged because we only add Link trailers
453 // transiently
454 let output = local_dir.run_jj(["log", "-r", "all()"]);
455 insta::assert_snapshot!(output, @"
456 @ yqosqzyt [email protected] 2001-02-03 08:05:15 c f6e97ced
457 │ c
458 ○ mzvwutvl [email protected] 2001-02-03 08:05:12 b 3bcb28c4
459 │ b
460 ◆ rlvkpnrz [email protected] 2001-02-03 08:05:09 a@origin 7d980be7
461 │ a
462 ◆ zzzzzzzz root() 00000000
463 [EOF]
464 ");
465
466 // There's no particular reason to run this with jj util exec, it's just that
467 // the infra makes it easier to run this way.
468 let output = remote_dir.run_jj(["util", "exec", "--", "git", "log", "refs/for/main"]);
469 insta::assert_snapshot!(output, @r"
470 commit b2731737e530be944c12679a86dacca2a3d3c6ad
471 Author: Test User <[email protected]>
472 Date: Sat Feb 3 04:05:13 2001 +0700
473
474 c
475
476 Signed-off-by: Lucky K Maintainer <[email protected]>
477 Link: https://gerrit.example.com/id/I19b790168e73f7a73a98deae21e807c06a6a6964
478
479 commit 9bc0339b54de4f3bcf241f8d68daf75bd6501cff
480 Author: Test User <[email protected]>
481 Date: Sat Feb 3 04:05:11 2001 +0700
482
483 b
484
485 Link: https://gerrit.example.com/id/Id043564ef93650b06a70f92f9d91912b6a6a6964
486
487 commit 7d980be7a1d499e4d316ab4c01242885032f7eaf
488 Author: Test User <[email protected]>
489 Date: Sat Feb 3 04:05:08 2001 +0700
490
491 a
492 [EOF]
493 ");
494 }
495
496 #[test]
497 fn test_gerrit_upload_local_explicit_change_ids() {
498 let test_env = TestEnvironment::default();
499 test_env
500 .run_jj_in(".", ["git", "init", "--colocate", "remote"])
501 .success();
502 let remote_dir = test_env.work_dir("remote");
503 create_commit(&remote_dir, "a", &[]);
504
505 test_env
506 .run_jj_in(".", ["git", "clone", "remote", "local"])
507 .success();
508 let local_dir = test_env.work_dir("local");
509 create_commit(&local_dir, "b", &["a@origin"]);
510
511 // Add an explicit Change-Id footer to b
512 let output = local_dir.run_jj([
513 "describe",
514 "b",
515 "-m",
516 "b\n\nChange-Id: Id39b308212fe7e0b746d16c13355f3a90712d7f9\n",
517 ]);
518 insta::assert_snapshot!(output, @"
519 ------- stderr -------
520 Working copy (@) now at: mzvwutvl 887a7016 b | b
521 Parent commit (@-) : rlvkpnrz 7d980be7 a@origin | a
522 [EOF]
523 ");
524
525 create_commit(&local_dir, "c", &["b"]);
526
527 // Add an explicit Link footer to c
528 let output = local_dir.run_jj([
529 "describe",
530 "c",
531 "-m",
532 "c\n\nLink: https://gerrit.example.com/id/Idfac1e8c149efddf5c7a286f787b43886a6a6964\n",
533 ]);
534 insta::assert_snapshot!(output, @"
535 ------- stderr -------
536 Working copy (@) now at: vruxwmqv b4124fc9 c | c
537 Parent commit (@-) : mzvwutvl 887a7016 b | b
538 [EOF]
539 ");
540
541 // The output should only mention commit IDs from the log output above (no
542 // temporary commits)
543 let output = local_dir.run_jj(["log", "-r", "all()"]);
544 insta::assert_snapshot!(output, @"
545 @ vruxwmqv [email protected] 2001-02-03 08:05:16 c b4124fc9
546 │ c
547 ○ mzvwutvl [email protected] 2001-02-03 08:05:13 b 887a7016
548 │ b
549 ◆ rlvkpnrz [email protected] 2001-02-03 08:05:09 a@origin 7d980be7
550 │ a
551 ◆ zzzzzzzz root() 00000000
552 [EOF]
553 ");
554
555 let output = local_dir.run_jj(["gerrit", "upload", "-r", "c", "--remote-branch=main"]);
556 insta::assert_snapshot!(output, @r"
557 ------- stderr -------
558 Found 1 heads to push to Gerrit (remote 'origin'), target branch 'main'.
559 Pushing vruxwmqv b4124fc9 c | c
560 [EOF]
561 ");
562
563 // The output should be unchanged because no temporary commits should have
564 // been created
565 let output = local_dir.run_jj(["log", "-r", "all()"]);
566 insta::assert_snapshot!(output, @"
567 @ vruxwmqv [email protected] 2001-02-03 08:05:16 c b4124fc9
568 │ c
569 ○ mzvwutvl [email protected] 2001-02-03 08:05:13 b 887a7016
570 │ b
571 ◆ rlvkpnrz [email protected] 2001-02-03 08:05:09 a@origin 7d980be7
572 │ a
573 ◆ zzzzzzzz root() 00000000
574 [EOF]
575 ");
576
577 // There's no particular reason to run this with jj util exec, it's just that
578 // the infra makes it easier to run this way.
579 let output = remote_dir.run_jj(["util", "exec", "--", "git", "log", "refs/for/main"]);
580 insta::assert_snapshot!(output, @"
581 commit b4124fc9d4694eecb4d9938cf4874cd13f1252b6
582 Author: Test User <[email protected]>
583 Date: Sat Feb 3 04:05:14 2001 +0700
584
585 c
586
587 Link: https://gerrit.example.com/id/Idfac1e8c149efddf5c7a286f787b43886a6a6964
588
589 commit 887a7016ec03a904835da1059543d8cc34b6ba76
590 Author: Test User <[email protected]>
591 Date: Sat Feb 3 04:05:11 2001 +0700
592
593 b
594
595 Change-Id: Id39b308212fe7e0b746d16c13355f3a90712d7f9
596
597 commit 7d980be7a1d499e4d316ab4c01242885032f7eaf
598 Author: Test User <[email protected]>
599 Date: Sat Feb 3 04:05:08 2001 +0700
600
601 a
602 [EOF]
603 ");
604 }
605
606 #[test]
607 fn test_gerrit_upload_local_mixed_change_ids() {
608 let test_env = TestEnvironment::default();
609 test_env
610 .run_jj_in(".", ["git", "init", "--colocate", "remote"])
611 .success();
612 let remote_dir = test_env.work_dir("remote");
613 create_commit(&remote_dir, "a", &[]);
614
615 test_env
616 .run_jj_in(".", ["git", "clone", "remote", "local"])
617 .success();
618 let local_dir = test_env.work_dir("local");
619 create_commit(&local_dir, "b", &["a@origin"]);
620 create_commit(&local_dir, "c", &["b"]);
621
622 // Add an explicit Change-Id footer to c but not b
623 let output = local_dir.run_jj([
624 "describe",
625 "c",
626 "-m",
627 "c\n\nChange-Id: Id39b308212fe7e0b746d16c13355f3a90712d7f9\n",
628 ]);
629 insta::assert_snapshot!(output, @"
630 ------- stderr -------
631 Working copy (@) now at: yqosqzyt 8d46d915 c | c
632 Parent commit (@-) : mzvwutvl 3bcb28c4 b | b
633 [EOF]
634 ");
635
636 // The output should only mention commit IDs from the log output above (no
637 // temporary commits)
638 let output = local_dir.run_jj(["log", "-r", "all()"]);
639 insta::assert_snapshot!(output, @"
640 @ yqosqzyt [email protected] 2001-02-03 08:05:15 c 8d46d915
641 │ c
642 ○ mzvwutvl [email protected] 2001-02-03 08:05:12 b 3bcb28c4
643 │ b
644 ◆ rlvkpnrz [email protected] 2001-02-03 08:05:09 a@origin 7d980be7
645 │ a
646 ◆ zzzzzzzz root() 00000000
647 [EOF]
648 ");
649
650 let output = local_dir.run_jj(["gerrit", "upload", "-r", "c", "--remote-branch=main"]);
651 insta::assert_snapshot!(output, @r"
652 ------- stderr -------
653 Found 1 heads to push to Gerrit (remote 'origin'), target branch 'main'.
654 Pushing yqosqzyt 8d46d915 c | c
655 [EOF]
656 ");
657
658 // The output should be unchanged because commits created within 'upload'
659 // should all be temporary
660 let output = local_dir.run_jj(["log", "-r", "all()"]);
661 insta::assert_snapshot!(output, @"
662 @ yqosqzyt [email protected] 2001-02-03 08:05:15 c 8d46d915
663 │ c
664 ○ mzvwutvl [email protected] 2001-02-03 08:05:12 b 3bcb28c4
665 │ b
666 ◆ rlvkpnrz [email protected] 2001-02-03 08:05:09 a@origin 7d980be7
667 │ a
668 ◆ zzzzzzzz root() 00000000
669 [EOF]
670 ");
671
672 // There's no particular reason to run this with jj util exec, it's just that
673 // the infra makes it easier to run this way.
674 let output = remote_dir.run_jj(["util", "exec", "--", "git", "log", "refs/for/main"]);
675 insta::assert_snapshot!(output, @"
676 commit 015df2b1d38bdc71ae7ef24c2889100e39d34ef8
677 Author: Test User <[email protected]>
678 Date: Sat Feb 3 04:05:13 2001 +0700
679
680 c
681
682 Change-Id: Id39b308212fe7e0b746d16c13355f3a90712d7f9
683
684 commit 81b723522d1c1a583a045eab5bfb323e45e6198d
685 Author: Test User <[email protected]>
686 Date: Sat Feb 3 04:05:11 2001 +0700
687
688 b
689
690 Change-Id: Id043564ef93650b06a70f92f9d91912b6a6a6964
691
692 commit 7d980be7a1d499e4d316ab4c01242885032f7eaf
693 Author: Test User <[email protected]>
694 Date: Sat Feb 3 04:05:08 2001 +0700
695
696 a
697 [EOF]
698 ");
699 }
700
701 #[test]
702 fn test_gerrit_upload_bad_change_ids() {
703 let test_env = TestEnvironment::default();
704 test_env
705 .run_jj_in(".", ["git", "init", "--colocate", "remote"])
706 .success();
707 let remote_dir = test_env.work_dir("remote");
708 create_commit(&remote_dir, "a", &[]);
709
710 test_env
711 .run_jj_in(".", ["git", "clone", "remote", "local"])
712 .success();
713 let local_dir = test_env.work_dir("local");
714 create_commit(&local_dir, "b", &["a@origin"]);
715 create_commit(&local_dir, "b2", &["b"]);
716 create_commit(&local_dir, "b3", &["b2"]);
717 create_commit(&local_dir, "b4", &["b3"]);
718 create_commit(&local_dir, "c", &["a@origin"]);
719 create_commit(&local_dir, "d", &["a@origin"]);
720 create_commit(&local_dir, "e", &["a@origin"]);
721
722 local_dir
723 .run_jj(["describe", "-rb", "-m\n\nChange-Id: malformed\n"])
724 .success();
725 local_dir
726 .run_jj([
727 "describe",
728 "-rb2",
729 "-m\n\nChange-Id: i0000000000000000000000000000000000000000\n",
730 ])
731 .success();
732 local_dir
733 .run_jj(["describe", "-rb3", "-m\n\nLink: malformed\n"])
734 .success();
735 local_dir
736 .run_jj([
737 "describe",
738 "-rb4",
739 "-m\n\nLink: https://gerrit.example.com/id/Imalformed\n",
740 ])
741 .success();
742 local_dir
743 .run_jj([
744 "describe",
745 "-rc",
746 "-m",
747 concat!(
748 "\n\n",
749 "Change-Id: I1111111111111111111111111111111111111111\n",
750 "Change-Id: I2222222222222222222222222222222222222222\n",
751 ),
752 ])
753 .success();
754 local_dir
755 .run_jj([
756 "describe",
757 "-rd",
758 "-m",
759 concat!(
760 "\n\n",
761 "Link: https://gerrit.example.com/id/I1111111111111111111111111111111111111111\n",
762 "Change-Id: I2222222222222222222222222222222222222222\n",
763 ),
764 ])
765 .success();
766 local_dir
767 .run_jj([
768 "describe",
769 "-re",
770 "-m",
771 concat!(
772 "\n\n",
773 "Link: https://gerrit.example.com/id/I1111111111111111111111111111111111111111\n",
774 "Link: https://gerrit.example.com/id/I2222222222222222222222222222222222222222\n",
775 ),
776 ])
777 .success();
778
779 let output = local_dir.run_jj(["gerrit", "upload", "-rc", "--remote-branch=main"]);
780 insta::assert_snapshot!(output, @"
781 ------- stderr -------
782 Error: Multiple Change-Id trailers in revision wqnwkozpkust
783 [EOF]
784 [exit status: 1]
785 ");
786 let output = local_dir.run_jj(["gerrit", "upload", "-rd", "--remote-branch=main"]);
787 insta::assert_snapshot!(output, @"
788 ------- stderr -------
789 Error: Multiple Change-Id trailers in revision kxryzmorwvtz
790 [EOF]
791 [exit status: 1]
792 ");
793 let output = local_dir.run_jj(["gerrit", "upload", "-re", "--remote-branch=main"]);
794 insta::assert_snapshot!(output, @"
795 ------- stderr -------
796 Error: Multiple Change-Id trailers in revision uyznsvlquzzm
797 [EOF]
798 [exit status: 1]
799 ");
800
801 // check both badly and slightly malformed Change-Id / Link trailers
802 let output = local_dir.run_jj(["gerrit", "upload", "-rb4", "--remote-branch=main"]);
803 insta::assert_snapshot!(output, @"
804 ------- stderr -------
805 Warning: Invalid Change-Id trailer in revision mzvwutvlkqwt
806 Warning: Invalid Change-Id trailer in revision yqosqzytrlsw
807 Warning: Invalid Link trailer in revision yostqsxwqrlt
808 Warning: Invalid Link trailer in revision kpqxywonksrl
809 Found 1 heads to push to Gerrit (remote 'origin'), target branch 'main'.
810 Pushing kpqxywon 69536ef3 b4
811 [EOF]
812 ");
813 }
814
815 #[test]
816 fn test_gerrit_upload_rejected_by_remote() -> TestResult {
817 let test_env = TestEnvironment::default();
818 test_env
819 .run_jj_in(".", ["git", "init", "--colocate", "remote"])
820 .success();
821 let remote_dir = test_env.work_dir("remote");
822 create_commit(&remote_dir, "a", &[]);
823
824 // create a hook on the remote that prevents pushing
825 let hook_path = test_env
826 .env_root()
827 .join("remote")
828 .join(".git")
829 .join("hooks")
830 .join("update");
831
832 std::fs::write(&hook_path, "#!/bin/sh\nexit 1")?;
833 #[cfg(unix)]
834 {
835 use std::os::unix::fs::PermissionsExt as _;
836
837 std::fs::set_permissions(&hook_path, std::fs::Permissions::from_mode(0o700))?;
838 }
839
840 test_env
841 .run_jj_in(".", ["git", "clone", "remote", "local"])
842 .success();
843 let local_dir = test_env.work_dir("local");
844 create_commit(&local_dir, "b", &["a@origin"]);
845
846 // Add an explicit Change-Id footer to b
847 let output = local_dir.run_jj([
848 "describe",
849 "b",
850 "-m",
851 "b\n\nChange-Id: Id39b308212fe7e0b746d16c13355f3a90712d7f9\n",
852 ]);
853 insta::assert_snapshot!(output, @"
854 ------- stderr -------
855 Working copy (@) now at: mzvwutvl 887a7016 b | b
856 Parent commit (@-) : rlvkpnrz 7d980be7 a@origin | a
857 [EOF]
858 ");
859
860 let output = local_dir.run_jj(["gerrit", "upload", "-r", "b", "--remote-branch=main"]);
861 insta::assert_snapshot!(output, @r"
862 ------- stderr -------
863 Found 1 heads to push to Gerrit (remote 'origin'), target branch 'main'.
864 Pushing mzvwutvl 887a7016 b | b
865 remote: error: hook declined to update refs/for/main
866 Warning: The remote rejected the following updates:
867 refs/for/main (reason: hook declined)
868 Hint: Try checking if you have permission to push to all the bookmarks.
869 Error: Failed to push all changes to gerrit
870 [EOF]
871 [exit status: 1]
872 ");
873 Ok(())
874 }