Debian Codegraph @

cli/tests/test_git_init.rs

raw
1 // Copyright 2024 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 std::path::Path;
16 use std::path::PathBuf;
17
18 use indoc::formatdoc;
19 use test_case::test_case;
20 use testutils::TestResult;
21 use testutils::git;
22
23 use crate::common::CommandOutput;
24 use crate::common::TestEnvironment;
25 use crate::common::TestWorkDir;
26 use crate::common::to_toml_value;
27
28 fn init_git_repo(git_repo_path: &Path, bare: bool) -> gix::Repository {
29 let git_repo = if bare {
30 git::init_bare(git_repo_path)
31 } else {
32 git::init(git_repo_path)
33 };
34
35 let git::CommitResult { commit_id, .. } = git::add_commit(
36 &git_repo,
37 "refs/heads/my-bookmark",
38 "some-file",
39 b"some content",
40 "My commit message",
41 &[],
42 );
43 git::set_head_to_id(&git_repo, commit_id);
44 git_repo
45 }
46
47 #[must_use]
48 fn get_bookmark_output(work_dir: &TestWorkDir) -> CommandOutput {
49 work_dir.run_jj(["bookmark", "list", "--all-remotes"])
50 }
51
52 #[must_use]
53 fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput {
54 let template = r#"
55 separate(" ",
56 commit_id.short(),
57 bookmarks,
58 description,
59 )"#;
60 work_dir.run_jj(["log", "-T", template, "-r=all()"])
61 }
62
63 #[must_use]
64 fn get_colocation_status(work_dir: &TestWorkDir) -> CommandOutput {
65 work_dir.run_jj([
66 "git",
67 "colocation",
68 "status",
69 "--ignore-working-copy",
70 "--quiet", // suppress hint
71 ])
72 }
73
74 fn read_git_target(work_dir: &TestWorkDir) -> String {
75 String::from_utf8(work_dir.read_file(".jj/repo/store/git_target").into()).unwrap()
76 }
77
78 #[test]
79 fn test_git_init_internal() {
80 let test_env = TestEnvironment::default();
81 test_env.add_config("git.colocate = false");
82 let output = test_env.run_jj_in(".", ["git", "init", "repo"]);
83 insta::assert_snapshot!(output, @r#"
84 ------- stderr -------
85 Initialized repo in "repo"
86 [EOF]
87 "#);
88
89 let work_dir = test_env.work_dir("repo");
90 let jj_path = work_dir.root().join(".jj");
91 let repo_path = jj_path.join("repo");
92 let store_path = repo_path.join("store");
93 assert!(work_dir.root().is_dir());
94 assert!(jj_path.is_dir());
95 assert!(jj_path.join("working_copy").is_dir());
96 assert!(repo_path.is_dir());
97 assert!(store_path.is_dir());
98 assert!(store_path.join("git").is_dir());
99 assert_eq!(read_git_target(&work_dir), "git");
100 }
101
102 #[test]
103 fn test_git_init_internal_preexisting_git_repo() {
104 let test_env = TestEnvironment::default();
105 test_env.add_config("git.colocate = false");
106 test_env.work_dir("").create_dir_all("repo/.git");
107 let output = test_env.run_jj_in(".", ["git", "init", "repo"]);
108 insta::assert_snapshot!(output, @"
109 ------- stderr -------
110 Error: Did not create a jj repo because there is an existing Git repo in this directory.
111 Hint: To create a repo backed by the existing Git repo, run `jj git init --colocate` instead.
112 [EOF]
113 [exit status: 1]
114 ");
115 }
116
117 #[test]
118 fn test_git_init_internal_no_integrate_operation() {
119 let test_env = TestEnvironment::default();
120 let workspace_root = test_env.env_root().join("repo");
121 std::fs::create_dir(&workspace_root).unwrap();
122
123 let output = test_env.run_jj_in(
124 &workspace_root,
125 &["git", "init", "--no-integrate-operation"],
126 );
127 insta::assert_snapshot!(output, @r"
128 ------- stderr -------
129 Error: --no-integrate-operation is not respected
130 [EOF]
131 [exit status: 2]
132 ");
133 }
134
135 #[test]
136 fn test_git_init_ignore_working_copy() {
137 let test_env = TestEnvironment::default();
138 test_env.add_config("git.colocate = true");
139 let work_dir = test_env.work_dir("").create_dir("repo");
140 work_dir.write_file("file1", "");
141
142 let output = work_dir.run_jj(["git", "init", "--ignore-working-copy"]);
143 insta::assert_snapshot!(output, @"
144 ------- stderr -------
145 Error: --ignore-working-copy is not respected
146 [EOF]
147 [exit status: 2]
148 ");
149 }
150
151 #[test]
152 fn test_git_init_at_operation() {
153 let test_env = TestEnvironment::default();
154 test_env.add_config("git.colocate = true");
155 let work_dir = test_env.work_dir("").create_dir("repo");
156
157 let output = work_dir.run_jj(["git", "init", "--at-op=@-"]);
158 insta::assert_snapshot!(output, @"
159 ------- stderr -------
160 Error: --at-op is not respected
161 [EOF]
162 [exit status: 2]
163 ");
164 }
165
166 #[test_case(false; "full")]
167 #[test_case(true; "bare")]
168 fn test_git_init_external(bare: bool) {
169 let test_env = TestEnvironment::default();
170 let git_repo_path = test_env.env_root().join("git-repo");
171 init_git_repo(&git_repo_path, bare);
172
173 // Explicitly enable git.colocate (which is also the default)
174 test_env.add_config("git.colocate = true");
175
176 // --git-repo takes precedence over git.colocate=true
177 let output = test_env.run_jj_in(
178 ".",
179 [
180 "git",
181 "init",
182 "repo",
183 "--git-repo",
184 git_repo_path.to_str().unwrap(),
185 ],
186 );
187 insta::allow_duplicates! {
188 insta::assert_snapshot!(output, @r#"
189 ------- stderr -------
190 Done importing changes from the underlying Git repo.
191 Working copy (@) now at: sqpuoqvx ed6b5138 (empty) (no description set)
192 Parent commit (@-) : nntyzxmz e80a42cc my-bookmark | My commit message
193 Added 1 files, modified 0 files, removed 0 files
194 Initialized repo in "repo"
195 [EOF]
196 "#);
197 }
198
199 let work_dir = test_env.work_dir("repo");
200 let jj_path = work_dir.root().join(".jj");
201 let repo_path = jj_path.join("repo");
202 let store_path = repo_path.join("store");
203 assert!(work_dir.root().is_dir());
204 assert!(jj_path.is_dir());
205 assert!(jj_path.join("working_copy").is_dir());
206 assert!(repo_path.is_dir());
207 assert!(store_path.is_dir());
208 let unix_git_target_file_contents = read_git_target(&work_dir).replace('\\', "/");
209 if bare {
210 assert!(unix_git_target_file_contents.ends_with("/git-repo"));
211 } else {
212 assert!(unix_git_target_file_contents.ends_with("/git-repo/.git"));
213 }
214
215 // Check that the Git repo's HEAD got checked out
216 insta::allow_duplicates! {
217 insta::assert_snapshot!(get_log_output(&work_dir), @"
218 @ ed6b513890ae
219 ○ e80a42cccd06 my-bookmark My commit message
220 ◆ 000000000000
221 [EOF]
222 ");
223 // Non-colocated repo shouldn't record the Git HEAD
224 insta::assert_snapshot!(get_colocation_status(&work_dir), @"
225 Workspace is currently not colocated with Git.
226 Last imported/exported Git HEAD: (none)
227 [EOF]
228 ");
229 }
230 }
231
232 #[test]
233 fn test_git_init_external_with_colocate_config() {
234 let test_env = TestEnvironment::default();
235 let git_repo_path = test_env.env_root().join("git-repo");
236 init_git_repo(&git_repo_path, true);
237
238 // Explicitly enable git.colocate (which is also the default)
239 test_env.add_config("git.colocate = true");
240
241 // --git-repo takes precedence over git.colocate.true
242 let output = test_env.run_jj_in(
243 ".",
244 [
245 "git",
246 "init",
247 "repo",
248 "--git-repo",
249 git_repo_path.to_str().unwrap(),
250 ],
251 );
252 insta::allow_duplicates! {
253 insta::assert_snapshot!(output, @r#"
254 ------- stderr -------
255 Done importing changes from the underlying Git repo.
256 Working copy (@) now at: sqpuoqvx ed6b5138 (empty) (no description set)
257 Parent commit (@-) : nntyzxmz e80a42cc my-bookmark | My commit message
258 Added 1 files, modified 0 files, removed 0 files
259 Initialized repo in "repo"
260 [EOF]
261 "#);
262 }
263
264 // Evolution history should be omitted for the "init" operation
265 let work_dir = test_env.work_dir("repo");
266 let output = work_dir.run_jj(["evolog", "-r..remote_bookmarks(remote=git)"]);
267 insta::assert_snapshot!(output, @"
268 ○ nntyzxmz [email protected] 1970-01-01 11:00:00 my-bookmark e80a42cc
269 My commit message
270 [EOF]
271 ");
272 }
273
274 #[test_case(false; "full")]
275 #[test_case(true; "bare")]
276 fn test_git_init_external_import_trunk(bare: bool) -> TestResult {
277 let test_env = TestEnvironment::default();
278 let git_repo_path = test_env.env_root().join("git-repo");
279 let git_repo = init_git_repo(&git_repo_path, bare);
280
281 // Explicitly enable git.colocate (which is also the default)
282 test_env.add_config("git.colocate = true");
283
284 // Add remote bookmark "trunk" for remote "origin", and set it as "origin/HEAD"
285 let oid = git_repo.find_reference("refs/heads/my-bookmark")?.id();
286
287 git_repo.reference(
288 "refs/remotes/origin/trunk",
289 oid.detach(),
290 gix::refs::transaction::PreviousValue::MustNotExist,
291 "create remote ref",
292 )?;
293
294 git::set_symbolic_reference(
295 &git_repo,
296 "refs/remotes/origin/HEAD",
297 "refs/remotes/origin/trunk",
298 );
299
300 let output = test_env.run_jj_in(
301 ".",
302 [
303 "git",
304 "init",
305 "repo",
306 "--git-repo",
307 git_repo_path.to_str().unwrap(),
308 ],
309 );
310 insta::allow_duplicates! {
311 insta::assert_snapshot!(output, @r#"
312 ------- stderr -------
313 Done importing changes from the underlying Git repo.
314 Setting the revset alias `trunk()` to `trunk@origin`.
315 Working copy (@) now at: sqpuoqvx ed6b5138 (empty) (no description set)
316 Parent commit (@-) : nntyzxmz e80a42cc my-bookmark trunk@origin | My commit message
317 Added 1 files, modified 0 files, removed 0 files
318 Initialized repo in "repo"
319 [EOF]
320 "#);
321 }
322
323 // "trunk()" alias should be set to remote "origin"'s default bookmark "trunk"
324 let work_dir = test_env.work_dir("repo");
325 let output = work_dir.run_jj(["config", "list", "--repo", "revset-aliases.\"trunk()\""]);
326 insta::allow_duplicates! {
327 insta::assert_snapshot!(output, @r#"
328 revset-aliases."trunk()" = "trunk@origin"
329 [EOF]
330 "#);
331 }
332 Ok(())
333 }
334
335 #[test]
336 fn test_git_init_external_import_trunk_upstream_takes_precedence() -> TestResult {
337 let test_env = TestEnvironment::default();
338 let git_repo_path = test_env.env_root().join("git-repo");
339 let git_repo = init_git_repo(&git_repo_path, false);
340
341 // Explicitly enable git.colocate (which is also the default)
342 test_env.add_config("git.colocate = true");
343
344 let oid = git_repo.find_reference("refs/heads/my-bookmark")?.id();
345
346 // Add both upstream and origin remotes with different default branches
347 // upstream has "develop" as default
348 git_repo.reference(
349 "refs/remotes/upstream/develop",
350 oid.detach(),
351 gix::refs::transaction::PreviousValue::MustNotExist,
352 "create upstream remote ref",
353 )?;
354
355 git::set_symbolic_reference(
356 &git_repo,
357 "refs/remotes/upstream/HEAD",
358 "refs/remotes/upstream/develop",
359 );
360
361 // origin has "trunk" as default
362 git_repo.reference(
363 "refs/remotes/origin/trunk",
364 oid.detach(),
365 gix::refs::transaction::PreviousValue::MustNotExist,
366 "create origin remote ref",
367 )?;
368
369 git::set_symbolic_reference(
370 &git_repo,
371 "refs/remotes/origin/HEAD",
372 "refs/remotes/origin/trunk",
373 );
374
375 // also accepts full .git path
376 let output = test_env.run_jj_in(
377 ".",
378 [
379 "git",
380 "init",
381 "repo",
382 "--git-repo",
383 git_repo_path.join(".git").to_str().unwrap(),
384 ],
385 );
386 insta::allow_duplicates! {
387 insta::assert_snapshot!(output, @r#"
388 ------- stderr -------
389 Done importing changes from the underlying Git repo.
390 Setting the revset alias `trunk()` to `develop@upstream`.
391 Working copy (@) now at: sqpuoqvx ed6b5138 (empty) (no description set)
392 Parent commit (@-) : nntyzxmz e80a42cc develop@upstream my-bookmark trunk@origin | My commit message
393 Added 1 files, modified 0 files, removed 0 files
394 Initialized repo in "repo"
395 [EOF]
396 "#);
397 }
398
399 // "trunk()" alias should be set to "upstream"'s default, not "origin"'s
400 let work_dir = test_env.work_dir("repo");
401 let output = work_dir.run_jj(["config", "list", "--repo", "revset-aliases.\"trunk()\""]);
402 insta::allow_duplicates! {
403 insta::assert_snapshot!(output, @r#"
404 revset-aliases."trunk()" = "develop@upstream"
405 [EOF]
406 "#);
407 }
408 Ok(())
409 }
410
411 #[test]
412 fn test_git_init_external_non_existent_directory() {
413 let test_env = TestEnvironment::default();
414 test_env.add_config("git.colocate = true");
415 let output = test_env.run_jj_in(".", ["git", "init", "repo", "--git-repo", "non-existent"]);
416 insta::assert_snapshot!(output.strip_stderr_last_line(), @"
417 ------- stderr -------
418 Error: Failed to access the repository
419 Caused by:
420 1: Cannot access $TEST_ENV/non-existent
421 [EOF]
422 [exit status: 1]
423 ");
424 }
425
426 #[test]
427 fn test_git_init_external_non_existent_git_directory() {
428 let test_env = TestEnvironment::default();
429 test_env.add_config("git.colocate = true");
430 let work_dir = test_env.work_dir("repo");
431 let output = test_env.run_jj_in(".", ["git", "init", "repo", "--git-repo", "repo"]);
432 insta::assert_snapshot!(output, @r#"
433 ------- stderr -------
434 Error: Failed to access the repository
435 Caused by:
436 1: Failed to open git repository
437 2: "$TEST_ENV/repo" does not appear to be a git repository
438 3: Missing HEAD at '.git/HEAD'
439 [EOF]
440 [exit status: 1]
441 "#);
442 let jj_path = work_dir.root().join(".jj");
443 assert!(!jj_path.exists());
444 }
445
446 #[test]
447 fn test_git_init_colocated_via_git_repo_path() {
448 let test_env = TestEnvironment::default();
449 test_env.add_config("git.colocate = true");
450 let work_dir = test_env.work_dir("repo");
451 init_git_repo(work_dir.root(), false);
452 let output = work_dir.run_jj(["git", "init", "--git-repo", "."]);
453 insta::assert_snapshot!(output, @r#"
454 ------- stderr -------
455 Done importing changes from the underlying Git repo.
456 Initialized repo in "."
457 [EOF]
458 "#);
459
460 let jj_path = work_dir.root().join(".jj");
461 let repo_path = jj_path.join("repo");
462 let store_path = repo_path.join("store");
463 assert!(work_dir.root().is_dir());
464 assert!(jj_path.is_dir());
465 assert!(jj_path.join("working_copy").is_dir());
466 assert!(repo_path.is_dir());
467 assert!(store_path.is_dir());
468 assert!(
469 read_git_target(&work_dir)
470 .replace('\\', "/")
471 .ends_with("../../../.git")
472 );
473
474 // Check that the Git repo's HEAD got checked out
475 insta::assert_snapshot!(get_log_output(&work_dir), @"
476 @ f3fe58bc88cc
477 ○ e80a42cccd06 my-bookmark My commit message
478 ◆ 000000000000
479 [EOF]
480 ");
481 insta::assert_snapshot!(get_colocation_status(&work_dir), @"
482 Workspace is currently colocated with Git.
483 Last imported/exported Git HEAD: e80a42cccd069007c7a2bb427ac7f1d10b408633
484 [EOF]
485 ");
486
487 // Check that the Git repo's HEAD moves
488 work_dir.run_jj(["new"]).success();
489 insta::assert_snapshot!(get_log_output(&work_dir), @"
490 @ bacc067e7740
491 ○ f3fe58bc88cc
492 ○ e80a42cccd06 my-bookmark My commit message
493 ◆ 000000000000
494 [EOF]
495 ");
496 insta::assert_snapshot!(get_colocation_status(&work_dir), @"
497 Workspace is currently colocated with Git.
498 Last imported/exported Git HEAD: f3fe58bc88ccfb820b930a21297d8e48bf76ac2a
499 [EOF]
500 ");
501 }
502
503 #[test]
504 fn test_git_init_colocated_via_git_repo_path_gitlink() {
505 let test_env = TestEnvironment::default();
506 test_env.add_config("git.colocate = true");
507 // <jj_work_dir>/.git -> <git_repo_path>
508 let git_repo_path = test_env.env_root().join("git-repo");
509 let git_repo = init_git_repo(&git_repo_path, false);
510 let jj_work_dir = test_env.work_dir("").create_dir("repo");
511 git::create_gitlink(jj_work_dir.root(), git_repo.path());
512
513 assert!(jj_work_dir.root().join(".git").is_file());
514 let output = jj_work_dir.run_jj(["git", "init", "--git-repo", "."]);
515 insta::assert_snapshot!(output, @r#"
516 ------- stderr -------
517 Done importing changes from the underlying Git repo.
518 Initialized repo in "."
519 [EOF]
520 "#);
521 insta::assert_snapshot!(read_git_target(&jj_work_dir), @"../../../.git");
522
523 // Check that the Git repo's HEAD got checked out
524 insta::assert_snapshot!(get_log_output(&jj_work_dir), @"
525 @ f3fe58bc88cc
526 ○ e80a42cccd06 my-bookmark My commit message
527 ◆ 000000000000
528 [EOF]
529 ");
530 insta::assert_snapshot!(get_colocation_status(&jj_work_dir), @"
531 Workspace is currently colocated with Git.
532 Last imported/exported Git HEAD: e80a42cccd069007c7a2bb427ac7f1d10b408633
533 [EOF]
534 ");
535
536 // Check that the Git repo's HEAD moves
537 jj_work_dir.run_jj(["new"]).success();
538 insta::assert_snapshot!(get_log_output(&jj_work_dir), @"
539 @ bacc067e7740
540 ○ f3fe58bc88cc
541 ○ e80a42cccd06 my-bookmark My commit message
542 ◆ 000000000000
543 [EOF]
544 ");
545 insta::assert_snapshot!(get_colocation_status(&jj_work_dir), @"
546 Workspace is currently colocated with Git.
547 Last imported/exported Git HEAD: f3fe58bc88ccfb820b930a21297d8e48bf76ac2a
548 [EOF]
549 ");
550 }
551
552 #[cfg(unix)]
553 #[test]
554 fn test_git_init_colocated_via_git_repo_path_symlink_directory() -> TestResult {
555 let test_env = TestEnvironment::default();
556 test_env.add_config("git.colocate = true");
557 // <jj_work_dir>/.git -> <git_repo_path>
558 let git_repo_path = test_env.env_root().join("git-repo");
559 init_git_repo(&git_repo_path, false);
560 let jj_work_dir = test_env.work_dir("").create_dir("repo");
561 std::os::unix::fs::symlink(git_repo_path.join(".git"), jj_work_dir.root().join(".git"))?;
562 let output = jj_work_dir.run_jj(["git", "init", "--git-repo", "."]);
563 insta::assert_snapshot!(output, @r#"
564 ------- stderr -------
565 Done importing changes from the underlying Git repo.
566 Initialized repo in "."
567 [EOF]
568 "#);
569 insta::assert_snapshot!(read_git_target(&jj_work_dir), @"../../../.git");
570
571 // Check that the Git repo's HEAD got checked out
572 insta::assert_snapshot!(get_log_output(&jj_work_dir), @"
573 @ f3fe58bc88cc
574 ○ e80a42cccd06 my-bookmark My commit message
575 ◆ 000000000000
576 [EOF]
577 ");
578 insta::assert_snapshot!(get_colocation_status(&jj_work_dir), @"
579 Workspace is currently colocated with Git.
580 Last imported/exported Git HEAD: e80a42cccd069007c7a2bb427ac7f1d10b408633
581 [EOF]
582 ");
583
584 // Check that the Git repo's HEAD moves
585 jj_work_dir.run_jj(["new"]).success();
586 insta::assert_snapshot!(get_log_output(&jj_work_dir), @"
587 @ bacc067e7740
588 ○ f3fe58bc88cc
589 ○ e80a42cccd06 my-bookmark My commit message
590 ◆ 000000000000
591 [EOF]
592 ");
593 insta::assert_snapshot!(get_colocation_status(&jj_work_dir), @"
594 Workspace is currently colocated with Git.
595 Last imported/exported Git HEAD: f3fe58bc88ccfb820b930a21297d8e48bf76ac2a
596 [EOF]
597 ");
598 Ok(())
599 }
600
601 #[cfg(unix)]
602 #[test]
603 fn test_git_init_colocated_via_git_repo_path_symlink_directory_without_bare_config() -> TestResult {
604 let test_env = TestEnvironment::default();
605 test_env.add_config("git.colocate = true");
606 // <jj_work_dir>/.git -> <git_repo_path>
607 let git_repo_path = test_env.env_root().join("git-repo.git");
608 let jj_work_dir = test_env.work_dir("repo");
609 // Set up git repo without core.bare set (as the "repo" tool would do.)
610 // The core.bare config is deduced from the directory name.
611 let git_repo = init_git_repo(jj_work_dir.root(), false);
612 git::remove_config_value(git_repo, "config", "bare");
613
614 std::fs::rename(jj_work_dir.root().join(".git"), &git_repo_path)?;
615 std::os::unix::fs::symlink(&git_repo_path, jj_work_dir.root().join(".git"))?;
616 let output = jj_work_dir.run_jj(["git", "init", "--git-repo", "."]);
617 insta::assert_snapshot!(output, @r#"
618 ------- stderr -------
619 Done importing changes from the underlying Git repo.
620 Initialized repo in "."
621 [EOF]
622 "#);
623 insta::assert_snapshot!(read_git_target(&jj_work_dir), @"../../../.git");
624
625 // Check that the Git repo's HEAD got checked out
626 insta::assert_snapshot!(get_log_output(&jj_work_dir), @"
627 @ f3fe58bc88cc
628 ○ e80a42cccd06 my-bookmark My commit message
629 ◆ 000000000000
630 [EOF]
631 ");
632 insta::assert_snapshot!(get_colocation_status(&jj_work_dir), @"
633 Workspace is currently colocated with Git.
634 Last imported/exported Git HEAD: e80a42cccd069007c7a2bb427ac7f1d10b408633
635 [EOF]
636 ");
637
638 // Check that the Git repo's HEAD moves
639 jj_work_dir.run_jj(["new"]).success();
640 insta::assert_snapshot!(get_log_output(&jj_work_dir), @"
641 @ bacc067e7740
642 ○ f3fe58bc88cc
643 ○ e80a42cccd06 my-bookmark My commit message
644 ◆ 000000000000
645 [EOF]
646 ");
647 insta::assert_snapshot!(get_colocation_status(&jj_work_dir), @"
648 Workspace is currently colocated with Git.
649 Last imported/exported Git HEAD: f3fe58bc88ccfb820b930a21297d8e48bf76ac2a
650 [EOF]
651 ");
652 Ok(())
653 }
654
655 #[cfg(unix)]
656 #[test]
657 fn test_git_init_colocated_via_git_repo_path_symlink_gitlink() -> TestResult {
658 let test_env = TestEnvironment::default();
659 test_env.add_config("git.colocate = true");
660 // <jj_work_dir>/.git -> <git_workdir_path>/.git -> <git_repo_path>
661 let git_repo_path = test_env.env_root().join("git-repo");
662 let git_workdir_path = test_env.env_root().join("git-workdir");
663 let git_repo = init_git_repo(&git_repo_path, false);
664 std::fs::create_dir(&git_workdir_path)?;
665 git::create_gitlink(&git_workdir_path, git_repo.path());
666 assert!(git_workdir_path.join(".git").is_file());
667 let jj_work_dir = test_env.work_dir("").create_dir("repo");
668 std::os::unix::fs::symlink(
669 git_workdir_path.join(".git"),
670 jj_work_dir.root().join(".git"),
671 )?;
672 let output = jj_work_dir.run_jj(["git", "init", "--git-repo", "."]);
673 insta::assert_snapshot!(output, @r#"
674 ------- stderr -------
675 Done importing changes from the underlying Git repo.
676 Initialized repo in "."
677 [EOF]
678 "#);
679 insta::assert_snapshot!(read_git_target(&jj_work_dir), @"../../../.git");
680
681 // Check that the Git repo's HEAD got checked out
682 insta::assert_snapshot!(get_log_output(&jj_work_dir), @"
683 @ f3fe58bc88cc
684 ○ e80a42cccd06 my-bookmark My commit message
685 ◆ 000000000000
686 [EOF]
687 ");
688 insta::assert_snapshot!(get_colocation_status(&jj_work_dir), @"
689 Workspace is currently colocated with Git.
690 Last imported/exported Git HEAD: e80a42cccd069007c7a2bb427ac7f1d10b408633
691 [EOF]
692 ");
693
694 // Check that the Git repo's HEAD moves
695 jj_work_dir.run_jj(["new"]).success();
696 insta::assert_snapshot!(get_log_output(&jj_work_dir), @"
697 @ bacc067e7740
698 ○ f3fe58bc88cc
699 ○ e80a42cccd06 my-bookmark My commit message
700 ◆ 000000000000
701 [EOF]
702 ");
703 insta::assert_snapshot!(get_colocation_status(&jj_work_dir), @"
704 Workspace is currently colocated with Git.
705 Last imported/exported Git HEAD: f3fe58bc88ccfb820b930a21297d8e48bf76ac2a
706 [EOF]
707 ");
708 Ok(())
709 }
710
711 #[test]
712 fn test_git_init_colocated_via_git_repo_path_imported_refs() {
713 let test_env = TestEnvironment::default();
714 test_env.add_config("git.colocate = true");
715 test_env.add_config("remotes.origin.auto-track-bookmarks = '*'");
716
717 // Set up remote refs
718 test_env
719 .run_jj_in(".", ["git", "init", "--no-colocate", "remote"])
720 .success();
721 let remote_dir = test_env.work_dir("remote");
722 remote_dir
723 .run_jj(["bookmark", "create", "-r@", "local-remote", "remote-only"])
724 .success();
725 remote_dir.run_jj(["new"]).success();
726 remote_dir.run_jj(["git", "export"]).success();
727
728 let remote_git_path = remote_dir
729 .root()
730 .join(PathBuf::from_iter([".jj", "repo", "store", "git"]));
731 let set_up_local_repo = |local_path: &Path| {
732 let git_repo = git::clone(local_path, remote_git_path.to_str().unwrap(), None);
733 let git_ref = git_repo
734 .find_reference("refs/remotes/origin/local-remote")
735 .unwrap();
736 git_repo
737 .reference(
738 "refs/heads/local-remote",
739 git_ref.target().id().to_owned(),
740 gix::refs::transaction::PreviousValue::MustNotExist,
741 "move local-remote bookmark",
742 )
743 .unwrap();
744 };
745
746 // With remotes.origin.auto-track-bookmarks = '*'
747 let local_dir = test_env.work_dir("local1");
748 set_up_local_repo(local_dir.root());
749 let output = local_dir.run_jj(["git", "init", "--git-repo=."]);
750 insta::assert_snapshot!(output, @r#"
751 ------- stderr -------
752 Done importing changes from the underlying Git repo.
753 Initialized repo in "."
754 [EOF]
755 "#);
756 insta::assert_snapshot!(get_bookmark_output(&local_dir), @"
757 local-remote: qpvuntsm e8849ae1 (empty) (no description set)
758 @git: qpvuntsm e8849ae1 (empty) (no description set)
759 @origin: qpvuntsm e8849ae1 (empty) (no description set)
760 remote-only: qpvuntsm e8849ae1 (empty) (no description set)
761 @git: qpvuntsm e8849ae1 (empty) (no description set)
762 @origin: qpvuntsm e8849ae1 (empty) (no description set)
763 [EOF]
764 ");
765
766 // With remotes.origin.auto-track-bookmarks = '~*'
767 test_env.add_config("remotes.origin.auto-track-bookmarks = '~*'");
768 let local_dir = test_env.work_dir("local2");
769 set_up_local_repo(local_dir.root());
770 let output = local_dir.run_jj(["git", "init", "--git-repo=."]);
771 insta::assert_snapshot!(output, @r#"
772 ------- stderr -------
773 Done importing changes from the underlying Git repo.
774 Hint: The following remote bookmarks aren't associated with the existing local bookmarks:
775 local-remote@origin
776 Hint: Run the following command to keep local bookmarks updated on future pulls:
777 jj bookmark track local-remote@origin
778 Initialized repo in "."
779 [EOF]
780 "#);
781 insta::assert_snapshot!(get_bookmark_output(&local_dir), @"
782 local-remote: qpvuntsm e8849ae1 (empty) (no description set)
783 @git: qpvuntsm e8849ae1 (empty) (no description set)
784 local-remote@origin: qpvuntsm e8849ae1 (empty) (no description set)
785 remote-only@origin: qpvuntsm e8849ae1 (empty) (no description set)
786 [EOF]
787 ");
788 }
789
790 #[test]
791 fn test_git_init_colocated_dirty_working_copy() {
792 let test_env = TestEnvironment::default();
793 test_env.add_config("git.colocate = true");
794 let work_dir = test_env.work_dir("repo");
795 let git_repo = init_git_repo(work_dir.root(), false);
796
797 let mut index_manager = git::IndexManager::new(&git_repo);
798
799 index_manager.add_file("new-staged-file", b"new content");
800 index_manager.add_file("some-file", b"new content");
801 index_manager.sync_index();
802
803 work_dir.write_file("unstaged-file", "new content");
804 insta::assert_debug_snapshot!(git::status(&git_repo), @r#"
805 [
806 GitStatus {
807 path: "new-staged-file",
808 status: Index(
809 Addition,
810 ),
811 },
812 GitStatus {
813 path: "some-file",
814 status: Index(
815 Modification,
816 ),
817 },
818 GitStatus {
819 path: "unstaged-file",
820 status: Worktree(
821 Added,
822 ),
823 },
824 ]
825 "#);
826
827 let output = work_dir.run_jj(["git", "init", "--git-repo", "."]);
828 insta::assert_snapshot!(output, @r#"
829 ------- stderr -------
830 Done importing changes from the underlying Git repo.
831 Initialized repo in "."
832 [EOF]
833 "#);
834
835 // Working-copy changes should have been snapshotted.
836 let output = work_dir.run_jj(["log", "-s", "--ignore-working-copy"]);
837 insta::assert_snapshot!(output, @"
838 @ sqpuoqvx [email protected] 2001-02-03 08:05:07 6efc2a53
839 │ (no description set)
840 │ C {some-file => new-staged-file}
841 │ M some-file
842 │ C {some-file => unstaged-file}
843 ○ nntyzxmz [email protected] 1970-01-01 11:00:00 my-bookmark e80a42cc
844 │ My commit message
845 │ A some-file
846 ◆ zzzzzzzz root() 00000000
847 [EOF]
848 ");
849
850 // Git index should be consistent with the working copy parent. With the
851 // current implementation, the index is unchanged. Since jj created new
852 // working copy commit, it's also okay to update the index reflecting the
853 // working copy commit or the working copy parent.
854 insta::assert_debug_snapshot!(git::status(&git_repo), @r#"
855 [
856 GitStatus {
857 path: ".jj/.gitignore",
858 status: Worktree(
859 Ignored,
860 ),
861 },
862 GitStatus {
863 path: ".jj/repo",
864 status: Worktree(
865 Ignored,
866 ),
867 },
868 GitStatus {
869 path: ".jj/working_copy",
870 status: Worktree(
871 Ignored,
872 ),
873 },
874 GitStatus {
875 path: "new-staged-file",
876 status: Index(
877 Addition,
878 ),
879 },
880 GitStatus {
881 path: "some-file",
882 status: Index(
883 Modification,
884 ),
885 },
886 GitStatus {
887 path: "unstaged-file",
888 status: Worktree(
889 IntentToAdd,
890 ),
891 },
892 ]
893 "#);
894 }
895
896 #[test]
897 fn test_git_init_external_but_git_dir_exists() {
898 let test_env = TestEnvironment::default();
899 let git_repo_path = test_env.env_root().join("git-repo");
900 let work_dir = test_env.work_dir("repo");
901 git::init(&git_repo_path);
902 init_git_repo(work_dir.root(), false);
903
904 // Explicitly enable git.colocate (which is also the default)
905 test_env.add_config("git.colocate = true");
906
907 // --git-repo takes precedence over git.colocate=true
908 let output = work_dir.run_jj(["git", "init", "--git-repo", git_repo_path.to_str().unwrap()]);
909 insta::assert_snapshot!(output, @r#"
910 ------- stderr -------
911 Initialized repo in "."
912 [EOF]
913 "#);
914
915 // The local ".git" repository is unrelated, so no commits should be imported
916 insta::assert_snapshot!(get_log_output(&work_dir), @"
917 @ e8849ae12c70
918 ◆ 000000000000
919 [EOF]
920 ");
921
922 // Check that Git HEAD is not set because this isn't a colocated workspace
923 work_dir.run_jj(["new"]).success();
924 insta::assert_snapshot!(get_log_output(&work_dir), @"
925 @ 1c1c95df80e5
926 ○ e8849ae12c70
927 ◆ 000000000000
928 [EOF]
929 ");
930 insta::assert_snapshot!(get_colocation_status(&work_dir), @"
931 Workspace is currently not colocated with Git.
932 Last imported/exported Git HEAD: (none)
933 [EOF]
934 ");
935 }
936
937 #[test]
938 fn test_git_init_colocated_via_flag_git_dir_exists() {
939 let test_env = TestEnvironment::default();
940 test_env.add_config("git.colocate = true");
941 let work_dir = test_env.work_dir("repo");
942 init_git_repo(work_dir.root(), false);
943
944 let output = test_env.run_jj_in(".", ["git", "init", "--colocate", "repo"]);
945 insta::assert_snapshot!(output, @r#"
946 ------- stderr -------
947 Done importing changes from the underlying Git repo.
948 Initialized repo in "repo"
949 [EOF]
950 "#);
951
952 // Check that the Git repo's HEAD got checked out
953 insta::assert_snapshot!(get_log_output(&work_dir), @"
954 @ f3fe58bc88cc
955 ○ e80a42cccd06 my-bookmark My commit message
956 ◆ 000000000000
957 [EOF]
958 ");
959 insta::assert_snapshot!(get_colocation_status(&work_dir), @"
960 Workspace is currently colocated with Git.
961 Last imported/exported Git HEAD: e80a42cccd069007c7a2bb427ac7f1d10b408633
962 [EOF]
963 ");
964
965 // Check that the Git repo's HEAD moves
966 work_dir.run_jj(["new"]).success();
967 insta::assert_snapshot!(get_log_output(&work_dir), @"
968 @ bacc067e7740
969 ○ f3fe58bc88cc
970 ○ e80a42cccd06 my-bookmark My commit message
971 ◆ 000000000000
972 [EOF]
973 ");
974 insta::assert_snapshot!(get_colocation_status(&work_dir), @"
975 Workspace is currently colocated with Git.
976 Last imported/exported Git HEAD: f3fe58bc88ccfb820b930a21297d8e48bf76ac2a
977 [EOF]
978 ");
979 }
980
981 #[test]
982 fn test_git_init_colocated_via_config_git_dir_exists() {
983 let test_env = TestEnvironment::default();
984 let work_dir = test_env.work_dir("repo");
985 init_git_repo(work_dir.root(), false);
986
987 test_env.add_config("git.colocate = true");
988
989 let output = test_env.run_jj_in(".", ["git", "init", "repo"]);
990 insta::assert_snapshot!(output, @r#"
991 ------- stderr -------
992 Done importing changes from the underlying Git repo.
993 Initialized repo in "repo"
994 [EOF]
995 "#);
996
997 // Check that the Git repo's HEAD got checked out
998 insta::assert_snapshot!(get_log_output(&work_dir), @"
999 @ f3fe58bc88cc
1000 ○ e80a42cccd06 my-bookmark My commit message
1001 ◆ 000000000000
1002 [EOF]
1003 ");
1004 insta::assert_snapshot!(get_colocation_status(&work_dir), @"
1005 Workspace is currently colocated with Git.
1006 Last imported/exported Git HEAD: e80a42cccd069007c7a2bb427ac7f1d10b408633
1007 [EOF]
1008 ");
1009
1010 // Check that the Git repo's HEAD moves
1011 work_dir.run_jj(["new"]).success();
1012 insta::assert_snapshot!(get_log_output(&work_dir), @"
1013 @ bacc067e7740
1014 ○ f3fe58bc88cc
1015 ○ e80a42cccd06 my-bookmark My commit message
1016 ◆ 000000000000
1017 [EOF]
1018 ");
1019 insta::assert_snapshot!(get_colocation_status(&work_dir), @"
1020 Workspace is currently colocated with Git.
1021 Last imported/exported Git HEAD: f3fe58bc88ccfb820b930a21297d8e48bf76ac2a
1022 [EOF]
1023 ");
1024 }
1025
1026 #[test]
1027 fn test_git_init_no_colocate() {
1028 let test_env = TestEnvironment::default();
1029 let work_dir = test_env.work_dir("repo");
1030
1031 test_env.add_config("git.colocate = true");
1032
1033 let output = test_env.run_jj_in(".", ["git", "init", "--no-colocate", "repo"]);
1034 insta::assert_snapshot!(output, @r#"
1035 ------- stderr -------
1036 Initialized repo in "repo"
1037 [EOF]
1038 "#);
1039
1040 assert!(!work_dir.root().join(".git").exists());
1041 }
1042
1043 #[test]
1044 fn test_git_init_not_colocated_via_false_config() {
1045 let test_env = TestEnvironment::default();
1046 let work_dir = test_env.work_dir("repo");
1047 test_env.add_config("git.colocate = false");
1048
1049 let output = test_env.run_jj_in(".", ["git", "init", "repo"]);
1050 insta::assert_snapshot!(output, @r#"
1051 ------- stderr -------
1052 Initialized repo in "repo"
1053 [EOF]
1054 "#);
1055
1056 assert!(!work_dir.root().join(".git").exists());
1057 assert!(work_dir.root().join(".jj/repo/store/git").is_dir());
1058 }
1059
1060 #[test]
1061 fn test_git_init_colocated_via_flag_overrides_false_config() {
1062 let test_env = TestEnvironment::default();
1063 let work_dir = test_env.work_dir("repo");
1064 init_git_repo(work_dir.root(), false);
1065 test_env.add_config("git.colocate = false");
1066
1067 // --colocate flag takes precedence over git.colocate=false
1068 let output = test_env.run_jj_in(".", ["git", "init", "--colocate", "repo"]);
1069 insta::assert_snapshot!(output, @r#"
1070 ------- stderr -------
1071 Done importing changes from the underlying Git repo.
1072 Initialized repo in "repo"
1073 [EOF]
1074 "#);
1075
1076 insta::assert_snapshot!(get_log_output(&work_dir), @"
1077 @ f3fe58bc88cc
1078 ○ e80a42cccd06 my-bookmark My commit message
1079 ◆ 000000000000
1080 [EOF]
1081 ");
1082 insta::assert_snapshot!(get_colocation_status(&work_dir), @"
1083 Workspace is currently colocated with Git.
1084 Last imported/exported Git HEAD: e80a42cccd069007c7a2bb427ac7f1d10b408633
1085 [EOF]
1086 ");
1087 }
1088
1089 #[test]
1090 fn test_git_init_colocated_via_flag_git_dir_not_exists() {
1091 let test_env = TestEnvironment::default();
1092 test_env.add_config("git.colocate = true");
1093 let work_dir = test_env.work_dir("repo");
1094 let output = test_env.run_jj_in(".", ["git", "init", "--colocate", "repo"]);
1095 insta::assert_snapshot!(output, @r#"
1096 ------- stderr -------
1097 Initialized repo in "repo"
1098 [EOF]
1099 "#);
1100 // No HEAD ref is available yet
1101 insta::assert_snapshot!(get_log_output(&work_dir), @"
1102 @ e8849ae12c70
1103 ◆ 000000000000
1104 [EOF]
1105 ");
1106 insta::assert_snapshot!(get_colocation_status(&work_dir), @"
1107 Workspace is currently colocated with Git.
1108 Last imported/exported Git HEAD: (none)
1109 [EOF]
1110 ");
1111
1112 // Create the default bookmark (create both in case we change the default)
1113 work_dir
1114 .run_jj(["bookmark", "create", "-r@", "main", "master"])
1115 .success();
1116
1117 // If .git/HEAD pointed to the default bookmark, new working-copy commit would
1118 // be created on top.
1119 insta::assert_snapshot!(get_log_output(&work_dir), @"
1120 @ e8849ae12c70 main master
1121 ◆ 000000000000
1122 [EOF]
1123 ");
1124 insta::assert_snapshot!(get_colocation_status(&work_dir), @"
1125 Workspace is currently colocated with Git.
1126 Last imported/exported Git HEAD: (none)
1127 [EOF]
1128 ");
1129 }
1130
1131 #[test]
1132 fn test_git_init_conditional_config() {
1133 let test_env = TestEnvironment::default();
1134 test_env.add_config("git.colocate = true");
1135 let old_workspace_dir = test_env.work_dir("old");
1136 let new_workspace_dir = test_env.work_dir("new");
1137
1138 let run_jj = |work_dir: &TestWorkDir, args: &[&str]| {
1139 work_dir.run_jj_with(|cmd| {
1140 cmd.args(args)
1141 .env_remove("JJ_EMAIL")
1142 .env_remove("JJ_OP_HOSTNAME")
1143 .env_remove("JJ_OP_USERNAME")
1144 })
1145 };
1146 let log_template = r#"separate(' ', author.email(), description.first_line()) ++ "\n""#;
1147 let op_log_template = r#"separate(' ', user, description.first_line()) ++ "\n""#;
1148
1149 // Override user.email and operation.username conditionally
1150 test_env.add_config(formatdoc! {"
1151 user.email = '[email protected]'
1152 operation.hostname = 'base'
1153 operation.username = 'base'
1154 [[--scope]]
1155 --when.repositories = [{new_workspace_root}]
1156 user.email = '[email protected]'
1157 operation.username = 'new-repo'
1158 ",
1159 new_workspace_root = to_toml_value(new_workspace_dir.root().to_str().unwrap()),
1160 });
1161
1162 // Override operation.hostname by repo config, which should be loaded into
1163 // the command settings, but shouldn't be copied to the new repo.
1164 run_jj(&test_env.work_dir(""), &["git", "init", "old"]).success();
1165 run_jj(
1166 &old_workspace_dir,
1167 &["config", "set", "--repo", "operation.hostname", "old-repo"],
1168 )
1169 .success();
1170 run_jj(&old_workspace_dir, &["new"]).success();
1171 let output = run_jj(&old_workspace_dir, &["op", "log", "-T", op_log_template]);
1172 insta::assert_snapshot!(output, @"
1173 @ base@old-repo new empty commit
1174 ○ base@base add workspace 'default'
1175 ○ @
1176 [EOF]
1177 ");
1178
1179 // Create new repo at the old workspace directory.
1180 let output = run_jj(&old_workspace_dir, &["git", "init", "../new"]);
1181 insta::assert_snapshot!(output.normalize_backslash(), @r#"
1182 ------- stderr -------
1183 Initialized repo in "../new"
1184 [EOF]
1185 "#);
1186 run_jj(&new_workspace_dir, &["new"]).success();
1187 let output = run_jj(&new_workspace_dir, &["log", "-T", log_template]);
1188 insta::assert_snapshot!(output, @"
1189 @ [email protected]
1190 ○ [email protected]
1191 ◆
1192 [EOF]
1193 ");
1194 let output = run_jj(&new_workspace_dir, &["op", "log", "-T", op_log_template]);
1195 insta::assert_snapshot!(output, @"
1196 @ new-repo@base new empty commit
1197 ○ new-repo@base add workspace 'default'
1198 ○ @
1199 [EOF]
1200 ");
1201 }
1202
1203 #[test]
1204 fn test_git_init_bad_wc_path() -> TestResult {
1205 let test_env = TestEnvironment::default();
1206 test_env.add_config("git.colocate = true");
1207 std::fs::write(test_env.env_root().join("existing-file"), b"")?;
1208 let output = test_env.run_jj_in(".", ["git", "init", "existing-file"]);
1209 insta::assert_snapshot!(output.strip_stderr_last_line(), @"
1210 ------- stderr -------
1211 Error: Failed to create workspace
1212 [EOF]
1213 [exit status: 1]
1214 ");
1215 Ok(())
1216 }
1217
1218 #[test]
1219 fn test_git_init_with_invalid_gitlink() {
1220 let test_env = TestEnvironment::default();
1221 test_env.add_config("git.colocate = true");
1222 let work_dir = test_env.work_dir("repo");
1223 work_dir.write_file(".git", "invalid");
1224
1225 // `jj git init --colocate` first checks for a worktree, make it fail
1226 // subsequent errors are not specific to colocation
1227 let output = work_dir.run_jj(["git", "init", "--colocate"]);
1228 insta::assert_snapshot!(output, @r#"
1229 ------- stderr -------
1230 Error: Failed to access the repository
1231 Caused by:
1232 1: Failed to open git repository
1233 2: "$TEST_ENV/repo/.git" does not appear to be a git repository
1234 3: Format should be 'gitdir: <path>', but got: "invalid"
1235 [EOF]
1236 [exit status: 1]
1237 "#);
1238
1239 let output = work_dir.run_jj(["git", "init", "--git-repo", "."]);
1240 insta::assert_snapshot!(output, @r#"
1241 ------- stderr -------
1242 Error: Failed to access the repository
1243 Caused by:
1244 1: Failed to open git repository
1245 2: "$TEST_ENV/repo/.git" does not appear to be a git repository
1246 3: Format should be 'gitdir: <path>', but got: "invalid"
1247 [EOF]
1248 [exit status: 1]
1249 "#);
1250 }
1251
1252 #[test]
1253 fn test_git_init_colocate_in_git_worktree() {
1254 let test_env = TestEnvironment::default();
1255 test_env.add_config("git.colocate = true");
1256 let main_repo_path = test_env.env_root().join("main-repo");
1257 init_git_repo(&main_repo_path, false);
1258
1259 // Create a Git worktree
1260 let worktree_path = test_env.env_root().join("worktree");
1261 let status = std::process::Command::new("git")
1262 .args([
1263 "worktree",
1264 "add",
1265 worktree_path.to_str().unwrap(),
1266 "-b",
1267 "worktree-branch",
1268 ])
1269 .current_dir(&main_repo_path)
1270 .status()
1271 .expect("git worktree add failed to spawn");
1272 assert!(status.success(), "git worktree add failed: {status}");
1273
1274 // Verify .git is a file (gitlink)
1275 assert!(worktree_path.join(".git").is_file());
1276
1277 // Try to init colocated jj repo - should fail
1278 let output = test_env.run_jj_in(
1279 worktree_path.to_str().unwrap(),
1280 ["git", "init", "--colocate"],
1281 );
1282 insta::assert_snapshot!(output, @"
1283 ------- stderr -------
1284 Error: Cannot create a colocated jj repo inside a Git worktree.
1285 Hint: Run `jj git init` in the main Git repository instead, or use `jj workspace add` to create additional jj workspaces.
1286 [EOF]
1287 [exit status: 1]
1288 ");
1289
1290 // Verify no .jj directory was created
1291 assert!(!worktree_path.join(".jj").exists());
1292 }
1293
1294 #[test]
1295 fn test_git_init_colocate_gitlink_not_worktree() -> TestResult {
1296 // Test that a gitlink pointing to a path that contains "worktrees" in a
1297 // user directory (NOT in the .git/worktrees/<name> pattern) is NOT
1298 // incorrectly detected as a Git worktree
1299 let test_env = TestEnvironment::default();
1300 test_env.add_config("git.colocate = true");
1301
1302 // Create a bare git repo at a path containing "worktrees" as a directory name
1303 let git_repo_path = test_env.env_root().join("worktrees").join("my-repo.git");
1304 std::fs::create_dir_all(&git_repo_path)?;
1305 init_git_repo(&git_repo_path, true);
1306
1307 // Create a working directory with a gitlink pointing to that bare repo
1308 let work_dir = test_env.env_root().join("work");
1309 std::fs::create_dir_all(&work_dir)?;
1310 let gitlink_content = format!("gitdir: {}", git_repo_path.to_str().unwrap());
1311 std::fs::write(work_dir.join(".git"), gitlink_content)?;
1312
1313 // Verify .git is a file (gitlink)
1314 assert!(work_dir.join(".git").is_file());
1315
1316 // jj git init --colocate should succeed (not be blocked as a worktree)
1317 let output = test_env.run_jj_in(work_dir.to_str().unwrap(), ["git", "init", "--colocate"]);
1318 insta::assert_snapshot!(output, @r#"
1319 ------- stderr -------
1320 Done importing changes from the underlying Git repo.
1321 Initialized repo in "."
1322 [EOF]
1323 "#);
1324
1325 // Verify .jj directory was created
1326 assert!(work_dir.join(".jj").exists());
1327 Ok(())
1328 }