| 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 |
use testutils::git; |
| 17 |
|
| 18 |
use crate::common::CommandOutput; |
| 19 |
use crate::common::TestEnvironment; |
| 20 |
use crate::common::TestWorkDir; |
| 21 |
|
| 22 |
#[must_use] |
| 23 |
fn get_colocation_status(work_dir: &TestWorkDir) -> CommandOutput { |
| 24 |
work_dir.run_jj([ |
| 25 |
"git", |
| 26 |
"colocation", |
| 27 |
"status", |
| 28 |
"--ignore-working-copy", |
| 29 |
"--quiet", // suppress hint |
| 30 |
]) |
| 31 |
} |
| 32 |
|
| 33 |
fn read_git_target(workspace_root: &std::path::Path) -> String { |
| 34 |
let mut path = workspace_root.to_path_buf(); |
| 35 |
path.extend([".jj", "repo", "store", "git_target"]); |
| 36 |
std::fs::read_to_string(path).unwrap() |
| 37 |
} |
| 38 |
|
| 39 |
#[test] |
| 40 |
fn test_git_colocation_enable_success() -> TestResult { |
| 41 |
let test_env = TestEnvironment::default(); |
| 42 |
|
| 43 |
// Initialize a non-colocated Jujutsu/Git workspace |
| 44 |
test_env |
| 45 |
.run_jj_in( |
| 46 |
test_env.env_root(), |
| 47 |
["git", "init", "--no-colocate", "repo"], |
| 48 |
) |
| 49 |
.success(); |
| 50 |
let work_dir = test_env.work_dir("repo"); |
| 51 |
let workspace_root = work_dir.root(); |
| 52 |
|
| 53 |
// Need at least one commit to be able to set git HEAD later |
| 54 |
work_dir.run_jj(["new"]).success(); |
| 55 |
|
| 56 |
// Verify it's not colocated initially |
| 57 |
assert!(!workspace_root.join(".git").exists()); |
| 58 |
assert_eq!(read_git_target(workspace_root), "git"); |
| 59 |
|
| 60 |
// And that there is no Git HEAD yet |
| 61 |
insta::assert_snapshot!(get_colocation_status(&work_dir), @" |
| 62 |
Workspace is currently not colocated with Git. |
| 63 |
Last imported/exported Git HEAD: (none) |
| 64 |
[EOF] |
| 65 |
"); |
| 66 |
|
| 67 |
// Run colocate command |
| 68 |
let output = work_dir.run_jj(["git", "colocation", "enable"]); |
| 69 |
insta::assert_snapshot!(output, @" |
| 70 |
------- stderr ------- |
| 71 |
Workspace successfully converted into a colocated Jujutsu/Git workspace. |
| 72 |
[EOF] |
| 73 |
"); |
| 74 |
|
| 75 |
// Verify colocate succeeded |
| 76 |
assert!(workspace_root.join(".git").exists()); |
| 77 |
assert!( |
| 78 |
!workspace_root |
| 79 |
.join(".jj") |
| 80 |
.join("repo") |
| 81 |
.join("store") |
| 82 |
.join("git") |
| 83 |
.exists() |
| 84 |
); |
| 85 |
assert_eq!(read_git_target(workspace_root), "../../../.git"); |
| 86 |
|
| 87 |
// Verify .jj/.gitignore was created |
| 88 |
let gitignore_content = std::fs::read_to_string(workspace_root.join(".jj").join(".gitignore"))?; |
| 89 |
assert_eq!(gitignore_content, "/*\n"); |
| 90 |
|
| 91 |
// Verify that Git HEAD was set correctly |
| 92 |
insta::assert_snapshot!(get_colocation_status(&work_dir), @" |
| 93 |
Workspace is currently colocated with Git. |
| 94 |
Last imported/exported Git HEAD: e8849ae12c709f2321908879bc724fdb2ab8a781 |
| 95 |
[EOF] |
| 96 |
"); |
| 97 |
|
| 98 |
// Verify that the repo changed |
| 99 |
let output = work_dir.run_jj(["op", "show", "-T", "description ++ '\n'"]); |
| 100 |
insta::assert_snapshot!(output, @" |
| 101 |
set git head to working copy parent |
| 102 |
[EOF] |
| 103 |
"); |
| 104 |
Ok(()) |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn test_git_colocation_enable_empty() { |
| 109 |
let test_env = TestEnvironment::default(); |
| 110 |
|
| 111 |
// Initialize a non-colocated Jujutsu/Git workspace |
| 112 |
test_env |
| 113 |
.run_jj_in( |
| 114 |
test_env.env_root(), |
| 115 |
["git", "init", "--no-colocate", "repo"], |
| 116 |
) |
| 117 |
.success(); |
| 118 |
let work_dir = test_env.work_dir("repo"); |
| 119 |
let setup_op_id = work_dir.current_operation_id(); |
| 120 |
|
| 121 |
// Run colocate command |
| 122 |
let output = work_dir.run_jj(["git", "colocation", "enable"]); |
| 123 |
insta::assert_snapshot!(output, @" |
| 124 |
------- stderr ------- |
| 125 |
Workspace successfully converted into a colocated Jujutsu/Git workspace. |
| 126 |
[EOF] |
| 127 |
"); |
| 128 |
|
| 129 |
// Verify that Git HEAD was set correctly |
| 130 |
insta::assert_snapshot!(get_colocation_status(&work_dir), @" |
| 131 |
Workspace is currently colocated with Git. |
| 132 |
Last imported/exported Git HEAD: (none) |
| 133 |
[EOF] |
| 134 |
"); |
| 135 |
|
| 136 |
// No repo change required |
| 137 |
assert_eq!(setup_op_id, work_dir.current_operation_id()); |
| 138 |
} |
| 139 |
|
| 140 |
#[test] |
| 141 |
fn test_git_colocation_enable_already_colocated() { |
| 142 |
let test_env = TestEnvironment::default(); |
| 143 |
|
| 144 |
// Initialize a colocated Jujutsu/Git repo |
| 145 |
test_env |
| 146 |
.run_jj_in(test_env.env_root(), ["git", "init", "--colocate", "repo"]) |
| 147 |
.success(); |
| 148 |
let work_dir = test_env.work_dir("repo"); |
| 149 |
|
| 150 |
// Try to colocate it again - should fail |
| 151 |
let output = work_dir.run_jj(["git", "colocation", "enable"]); |
| 152 |
insta::assert_snapshot!(output, @" |
| 153 |
------- stderr ------- |
| 154 |
Workspace is already colocated with Git. |
| 155 |
[EOF] |
| 156 |
"); |
| 157 |
} |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn test_git_colocation_enable_with_existing_git_dir() -> TestResult { |
| 161 |
let test_env = TestEnvironment::default(); |
| 162 |
|
| 163 |
// Initialize a non-colocated Jujutsu/Git repo |
| 164 |
test_env |
| 165 |
.run_jj_in( |
| 166 |
test_env.env_root(), |
| 167 |
["git", "init", "--no-colocate", "repo"], |
| 168 |
) |
| 169 |
.success(); |
| 170 |
let work_dir = test_env.work_dir("repo"); |
| 171 |
let workspace_root = work_dir.root(); |
| 172 |
|
| 173 |
// Create a .git directory manually |
| 174 |
std::fs::create_dir(workspace_root.join(".git"))?; |
| 175 |
std::fs::write(workspace_root.join(".git").join("dummy"), "dummy")?; |
| 176 |
|
| 177 |
// Try to colocate - should fail |
| 178 |
let output = work_dir.run_jj(["git", "colocation", "enable"]); |
| 179 |
insta::assert_snapshot!(output.strip_stderr_last_line(), @" |
| 180 |
------- stderr ------- |
| 181 |
Error: A .git directory already exists in the workspace root. Cannot colocate. |
| 182 |
[EOF] |
| 183 |
[exit status: 1] |
| 184 |
"); |
| 185 |
Ok(()) |
| 186 |
} |
| 187 |
|
| 188 |
#[test] |
| 189 |
fn test_git_colocation_enable_external_git_repo() { |
| 190 |
let test_env = TestEnvironment::default(); |
| 191 |
|
| 192 |
// Initialize a Jujutsu workspace backed by an external Git repository |
| 193 |
let git_repo_path = test_env.env_root().join("git-repo"); |
| 194 |
git::init(&git_repo_path); |
| 195 |
test_env |
| 196 |
.run_jj_in( |
| 197 |
test_env.env_root(), |
| 198 |
[ |
| 199 |
"git", |
| 200 |
"init", |
| 201 |
"repo", |
| 202 |
"--git-repo", |
| 203 |
git_repo_path.to_str().unwrap(), |
| 204 |
], |
| 205 |
) |
| 206 |
.success(); |
| 207 |
let work_dir = test_env.work_dir("repo"); |
| 208 |
|
| 209 |
// The status hint shouldn't suggest enabling colocation |
| 210 |
let output = work_dir.run_jj(["git", "colocation", "status"]); |
| 211 |
insta::assert_snapshot!(output, @" |
| 212 |
Workspace is currently not colocated with Git. |
| 213 |
Last imported/exported Git HEAD: (none) |
| 214 |
[EOF] |
| 215 |
------- stderr ------- |
| 216 |
Hint: Colocation cannot be enabled because the workspace is backed by an external Git repository. |
| 217 |
[EOF] |
| 218 |
"); |
| 219 |
|
| 220 |
// Trying to colocate should fail with a clean error |
| 221 |
let output = work_dir.run_jj(["git", "colocation", "enable"]); |
| 222 |
insta::assert_snapshot!(output, @" |
| 223 |
------- stderr ------- |
| 224 |
Error: Cannot colocate a workspace backed by an external Git repository at $TEST_ENV/git-repo/.git |
| 225 |
[EOF] |
| 226 |
[exit status: 1] |
| 227 |
"); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn test_git_colocation_disable_success() { |
| 232 |
let test_env = TestEnvironment::default(); |
| 233 |
|
| 234 |
// Create a colocated Jujutsu/Git repo |
| 235 |
test_env |
| 236 |
.run_jj_in(test_env.env_root(), ["git", "init", "--colocate", "repo"]) |
| 237 |
.success(); |
| 238 |
let work_dir = test_env.work_dir("repo"); |
| 239 |
let workspace_root = work_dir.root(); |
| 240 |
|
| 241 |
// Need at least one commit to be able to set git HEAD later |
| 242 |
work_dir.run_jj(["new"]).success(); |
| 243 |
|
| 244 |
// Verify that Git HEAD is set |
| 245 |
insta::assert_snapshot!(get_colocation_status(&work_dir), @" |
| 246 |
Workspace is currently colocated with Git. |
| 247 |
Last imported/exported Git HEAD: e8849ae12c709f2321908879bc724fdb2ab8a781 |
| 248 |
[EOF] |
| 249 |
"); |
| 250 |
|
| 251 |
// Verify it's colocated |
| 252 |
assert!(workspace_root.join(".git").exists()); |
| 253 |
assert_eq!(read_git_target(workspace_root), "../../../.git"); |
| 254 |
|
| 255 |
// Disable colocation |
| 256 |
let output = work_dir.run_jj(["git", "colocation", "disable"]); |
| 257 |
insta::assert_snapshot!(output, @" |
| 258 |
------- stderr ------- |
| 259 |
Workspace successfully converted into a non-colocated Jujutsu/Git workspace. |
| 260 |
[EOF] |
| 261 |
"); |
| 262 |
|
| 263 |
// Verify that disable colocation succeeded |
| 264 |
assert!(!workspace_root.join(".git").exists()); |
| 265 |
assert!( |
| 266 |
workspace_root |
| 267 |
.join(".jj") |
| 268 |
.join("repo") |
| 269 |
.join("store") |
| 270 |
.join("git") |
| 271 |
.exists() |
| 272 |
); |
| 273 |
assert_eq!(read_git_target(workspace_root), "git"); |
| 274 |
assert!(!workspace_root.join(".jj").join(".gitignore").exists()); |
| 275 |
|
| 276 |
// Verify that Git HEAD was removed correctly |
| 277 |
insta::assert_snapshot!(get_colocation_status(&work_dir), @" |
| 278 |
Workspace is currently not colocated with Git. |
| 279 |
Last imported/exported Git HEAD: (none) |
| 280 |
[EOF] |
| 281 |
"); |
| 282 |
|
| 283 |
// Verify that the repo changed |
| 284 |
let output = work_dir.run_jj(["op", "show", "-T", "description ++ '\n'"]); |
| 285 |
insta::assert_snapshot!(output, @" |
| 286 |
remove git head reference |
| 287 |
[EOF] |
| 288 |
"); |
| 289 |
} |
| 290 |
|
| 291 |
#[test] |
| 292 |
fn test_git_colocation_disable_empty() { |
| 293 |
let test_env = TestEnvironment::default(); |
| 294 |
|
| 295 |
// Create a colocated Jujutsu/Git repo |
| 296 |
test_env |
| 297 |
.run_jj_in(test_env.env_root(), ["git", "init", "--colocate", "repo"]) |
| 298 |
.success(); |
| 299 |
let work_dir = test_env.work_dir("repo"); |
| 300 |
let setup_op_id = work_dir.current_operation_id(); |
| 301 |
|
| 302 |
// Verify that Git HEAD is unset |
| 303 |
insta::assert_snapshot!(get_colocation_status(&work_dir), @" |
| 304 |
Workspace is currently colocated with Git. |
| 305 |
Last imported/exported Git HEAD: (none) |
| 306 |
[EOF] |
| 307 |
"); |
| 308 |
|
| 309 |
// Disable colocation |
| 310 |
let output = work_dir.run_jj(["git", "colocation", "disable"]); |
| 311 |
insta::assert_snapshot!(output, @" |
| 312 |
------- stderr ------- |
| 313 |
Workspace successfully converted into a non-colocated Jujutsu/Git workspace. |
| 314 |
[EOF] |
| 315 |
"); |
| 316 |
|
| 317 |
// No repo change required |
| 318 |
assert_eq!(setup_op_id, work_dir.current_operation_id()); |
| 319 |
} |
| 320 |
|
| 321 |
#[test] |
| 322 |
fn test_git_colocation_disable_not_colocated() { |
| 323 |
let test_env = TestEnvironment::default(); |
| 324 |
|
| 325 |
// Initialize a non-colocated Jujutsu/Git repo |
| 326 |
test_env |
| 327 |
.run_jj_in( |
| 328 |
test_env.env_root(), |
| 329 |
["git", "init", "--no-colocate", "repo"], |
| 330 |
) |
| 331 |
.success(); |
| 332 |
let work_dir = test_env.work_dir("repo"); |
| 333 |
|
| 334 |
// Try to disable colocation when not colocated - should fail |
| 335 |
let output = work_dir.run_jj(["git", "colocation", "disable"]); |
| 336 |
insta::assert_snapshot!(output, @" |
| 337 |
------- stderr ------- |
| 338 |
Workspace is already not colocated with Git. |
| 339 |
[EOF] |
| 340 |
"); |
| 341 |
} |
| 342 |
|
| 343 |
#[test] |
| 344 |
fn test_git_colocation_status_non_colocated() { |
| 345 |
let test_env = TestEnvironment::default(); |
| 346 |
|
| 347 |
// Initialize a non-colocated Jujutsu/Git repo |
| 348 |
test_env |
| 349 |
.run_jj_in( |
| 350 |
test_env.env_root(), |
| 351 |
["git", "init", "--no-colocate", "repo"], |
| 352 |
) |
| 353 |
.success(); |
| 354 |
let work_dir = test_env.work_dir("repo"); |
| 355 |
|
| 356 |
// Check status - should show non-colocated |
| 357 |
let output = work_dir.run_jj(["git", "colocation", "status"]); |
| 358 |
insta::assert_snapshot!(output, @" |
| 359 |
Workspace is currently not colocated with Git. |
| 360 |
Last imported/exported Git HEAD: (none) |
| 361 |
[EOF] |
| 362 |
------- stderr ------- |
| 363 |
Hint: To enable colocation, run: `jj git colocation enable` |
| 364 |
[EOF] |
| 365 |
"); |
| 366 |
} |
| 367 |
|
| 368 |
#[test] |
| 369 |
fn test_git_colocation_status_colocated() { |
| 370 |
let test_env = TestEnvironment::default(); |
| 371 |
|
| 372 |
// Initialize a colocated jj repo |
| 373 |
test_env |
| 374 |
.run_jj_in(test_env.env_root(), ["git", "init", "--colocate", "repo"]) |
| 375 |
.success(); |
| 376 |
let work_dir = test_env.work_dir("repo"); |
| 377 |
|
| 378 |
// Check status - should show colocated |
| 379 |
let output = work_dir.run_jj(["git", "colocation", "status"]); |
| 380 |
insta::assert_snapshot!(output, @" |
| 381 |
Workspace is currently colocated with Git. |
| 382 |
Last imported/exported Git HEAD: (none) |
| 383 |
[EOF] |
| 384 |
------- stderr ------- |
| 385 |
Hint: To disable colocation, run: `jj git colocation disable` |
| 386 |
[EOF] |
| 387 |
"); |
| 388 |
} |
| 389 |
|
| 390 |
#[test] |
| 391 |
fn test_git_colocation_in_secondary_workspace() { |
| 392 |
let test_env = TestEnvironment::default(); |
| 393 |
test_env |
| 394 |
.run_jj_in(".", ["git", "init", "--no-colocate", "main"]) |
| 395 |
.success(); |
| 396 |
let main_dir = test_env.work_dir("main"); |
| 397 |
main_dir |
| 398 |
.run_jj(["workspace", "add", "../secondary"]) |
| 399 |
.success(); |
| 400 |
let secondary_dir = test_env.work_dir("secondary"); |
| 401 |
|
| 402 |
let output = secondary_dir.run_jj(["git", "colocation", "status"]); |
| 403 |
insta::assert_snapshot!(output, @" |
| 404 |
------- stderr ------- |
| 405 |
Error: This command cannot be used in a non-main Jujutsu workspace |
| 406 |
[EOF] |
| 407 |
[exit status: 1] |
| 408 |
"); |
| 409 |
|
| 410 |
let output = secondary_dir.run_jj(["git", "colocation", "enable"]); |
| 411 |
insta::assert_snapshot!(output, @" |
| 412 |
------- stderr ------- |
| 413 |
Error: This command cannot be used in a non-main Jujutsu workspace |
| 414 |
[EOF] |
| 415 |
[exit status: 1] |
| 416 |
"); |
| 417 |
|
| 418 |
let output = secondary_dir.run_jj(["git", "colocation", "disable"]); |
| 419 |
insta::assert_snapshot!(output, @" |
| 420 |
------- stderr ------- |
| 421 |
Error: This command cannot be used in a non-main Jujutsu workspace |
| 422 |
[EOF] |
| 423 |
[exit status: 1] |
| 424 |
"); |
| 425 |
} |