| 1 |
// Copyright 2022 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 itertools::Itertools as _; |
| 16 |
use jj_lib::backend::CommitId; |
| 17 |
use testutils::TestResult; |
| 18 |
use testutils::git; |
| 19 |
|
| 20 |
use crate::common::CommandOutput; |
| 21 |
use crate::common::TestEnvironment; |
| 22 |
use crate::common::TestWorkDir; |
| 23 |
|
| 24 |
#[test] |
| 25 |
fn test_git_export_basic() { |
| 26 |
let test_env = TestEnvironment::default(); |
| 27 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 28 |
let work_dir = test_env.work_dir("repo"); |
| 29 |
|
| 30 |
work_dir |
| 31 |
.run_jj(["bookmark", "create", "-r@", "main"]) |
| 32 |
.success(); |
| 33 |
|
| 34 |
let output = work_dir.run_jj(["git", "export", "--no-integrate-operation"]); |
| 35 |
insta::assert_snapshot!(output, @" |
| 36 |
------- stderr ------- |
| 37 |
Error: --no-integrate-operation is not respected |
| 38 |
[EOF] |
| 39 |
[exit status: 2] |
| 40 |
"); |
| 41 |
|
| 42 |
let output = work_dir.run_jj(["git", "export"]); |
| 43 |
insta::assert_snapshot!(output, @""); |
| 44 |
insta::assert_snapshot!(work_dir.run_jj(["log", "-rmain@git"]), @" |
| 45 |
@ qpvuntsm [email protected] 2001-02-03 08:05:07 main e8849ae1 |
| 46 |
│ (empty) (no description set) |
| 47 |
~ |
| 48 |
[EOF] |
| 49 |
"); |
| 50 |
} |
| 51 |
|
| 52 |
#[test] |
| 53 |
fn test_resolution_of_git_tracking_bookmarks() -> TestResult { |
| 54 |
let test_env = TestEnvironment::default(); |
| 55 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 56 |
let work_dir = test_env.work_dir("repo"); |
| 57 |
work_dir |
| 58 |
.run_jj(["bookmark", "create", "-r@", "main"]) |
| 59 |
.success(); |
| 60 |
work_dir |
| 61 |
.run_jj(["describe", "-r", "main", "-m", "old_message"]) |
| 62 |
.success(); |
| 63 |
|
| 64 |
// Create local-git tracking bookmark |
| 65 |
let output = work_dir.run_jj(["git", "export"]); |
| 66 |
insta::assert_snapshot!(output, @""); |
| 67 |
// Move the local bookmark somewhere else |
| 68 |
work_dir |
| 69 |
.run_jj(["describe", "-r", "main", "-m", "new_message"]) |
| 70 |
.success(); |
| 71 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @" |
| 72 |
main: qpvuntsm 384a1421 (empty) new_message |
| 73 |
@git (ahead by 1 commits, behind by 1 commits): qpvuntsm/1 a7f9930b (hidden) (empty) old_message |
| 74 |
[EOF] |
| 75 |
"); |
| 76 |
|
| 77 |
// Test that we can address both revisions |
| 78 |
let query = |expr| { |
| 79 |
let template = r#"commit_id ++ " " ++ description"#; |
| 80 |
work_dir.run_jj(["log", "-r", expr, "-T", template, "--no-graph"]) |
| 81 |
}; |
| 82 |
insta::assert_snapshot!(query("main"), @" |
| 83 |
384a14213707d776d0517f65cdcf954d07d88c40 new_message |
| 84 |
[EOF] |
| 85 |
"); |
| 86 |
insta::assert_snapshot!(query("main@git"), @" |
| 87 |
a7f9930bb6d54ba39e6c254135b9bfe32041fea4 old_message |
| 88 |
[EOF] |
| 89 |
"); |
| 90 |
insta::assert_snapshot!(query(r#"remote_bookmarks("main", "git")"#), @" |
| 91 |
a7f9930bb6d54ba39e6c254135b9bfe32041fea4 old_message |
| 92 |
[EOF] |
| 93 |
"); |
| 94 |
Ok(()) |
| 95 |
} |
| 96 |
|
| 97 |
#[test] |
| 98 |
fn test_git_export_conflicting_git_refs() -> TestResult { |
| 99 |
let test_env = TestEnvironment::default(); |
| 100 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 101 |
let work_dir = test_env.work_dir("repo"); |
| 102 |
|
| 103 |
work_dir |
| 104 |
.run_jj(["bookmark", "create", "-r@", "main"]) |
| 105 |
.success(); |
| 106 |
work_dir |
| 107 |
.run_jj(["bookmark", "create", "-r@", "main/sub"]) |
| 108 |
.success(); |
| 109 |
let output = work_dir.run_jj(["git", "export"]); |
| 110 |
insta::with_settings!({filters => vec![("Failed to set: .*", "Failed to set: ...")]}, { |
| 111 |
insta::assert_snapshot!(output, @r#" |
| 112 |
------- stderr ------- |
| 113 |
Warning: Failed to export some bookmarks: |
| 114 |
main/sub@git: Failed to set: ... |
| 115 |
Hint: Git doesn't allow a branch/tag name that looks like a parent directory of |
| 116 |
another (e.g. `foo` and `foo/bar`). Try to rename the bookmarks/tags that failed |
| 117 |
to export or their "parent" bookmarks/tags. |
| 118 |
[EOF] |
| 119 |
"#); |
| 120 |
}); |
| 121 |
Ok(()) |
| 122 |
} |
| 123 |
|
| 124 |
#[test] |
| 125 |
fn test_git_export_undo() -> TestResult { |
| 126 |
let test_env = TestEnvironment::default(); |
| 127 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 128 |
let work_dir = test_env.work_dir("repo"); |
| 129 |
let git_repo = git::open(work_dir.root().join(".jj/repo/store/git")); |
| 130 |
|
| 131 |
work_dir |
| 132 |
.run_jj(["bookmark", "create", "-r@", "a"]) |
| 133 |
.success(); |
| 134 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @" |
| 135 |
a: qpvuntsm e8849ae1 (empty) (no description set) |
| 136 |
[EOF] |
| 137 |
"); |
| 138 |
let output = work_dir.run_jj(["git", "export"]); |
| 139 |
insta::assert_snapshot!(output, @""); |
| 140 |
insta::assert_snapshot!(work_dir.run_jj(["log", "-ra@git"]), @" |
| 141 |
@ qpvuntsm [email protected] 2001-02-03 08:05:07 a e8849ae1 |
| 142 |
│ (empty) (no description set) |
| 143 |
~ |
| 144 |
[EOF] |
| 145 |
"); |
| 146 |
|
| 147 |
// Exported refs won't be removed by undoing the export, but the git-tracking |
| 148 |
// bookmark is. This is the same as remote-tracking bookmarks. |
| 149 |
let output = work_dir.run_jj(["undo"]); |
| 150 |
insta::assert_snapshot!(output, @" |
| 151 |
------- stderr ------- |
| 152 |
Undid operation: 29447db431a3 (2001-02-03 08:05:10) export git refs |
| 153 |
Restored to operation: 5142cb3d7c38 (2001-02-03 08:05:08) create bookmark a pointing to commit e8849ae12c709f2321908879bc724fdb2ab8a781 |
| 154 |
[EOF] |
| 155 |
"); |
| 156 |
insta::assert_debug_snapshot!(get_git_repo_refs(&git_repo), @r#" |
| 157 |
[ |
| 158 |
( |
| 159 |
"refs/heads/a", |
| 160 |
CommitId( |
| 161 |
"e8849ae12c709f2321908879bc724fdb2ab8a781", |
| 162 |
), |
| 163 |
), |
| 164 |
] |
| 165 |
"#); |
| 166 |
insta::assert_snapshot!(work_dir.run_jj(["log", "-ra@git"]), @" |
| 167 |
------- stderr ------- |
| 168 |
Error: Revision `a@git` doesn't exist |
| 169 |
Hint: Did you mean `a`? |
| 170 |
[EOF] |
| 171 |
[exit status: 1] |
| 172 |
"); |
| 173 |
|
| 174 |
// This would re-export bookmark "a" and create git-tracking bookmark. |
| 175 |
let output = work_dir.run_jj(["git", "export"]); |
| 176 |
insta::assert_snapshot!(output, @""); |
| 177 |
insta::assert_snapshot!(work_dir.run_jj(["log", "-ra@git"]), @" |
| 178 |
@ qpvuntsm [email protected] 2001-02-03 08:05:07 a e8849ae1 |
| 179 |
│ (empty) (no description set) |
| 180 |
~ |
| 181 |
[EOF] |
| 182 |
"); |
| 183 |
Ok(()) |
| 184 |
} |
| 185 |
|
| 186 |
#[test] |
| 187 |
fn test_git_import_undo() -> TestResult { |
| 188 |
let test_env = TestEnvironment::default(); |
| 189 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 190 |
let work_dir = test_env.work_dir("repo"); |
| 191 |
let git_repo = git::open(work_dir.root().join(".jj/repo/store/git")); |
| 192 |
|
| 193 |
// Create bookmark "a" in git repo |
| 194 |
let commit_id = work_dir |
| 195 |
.run_jj(&["log", "-Tcommit_id", "--no-graph", "-r@"]) |
| 196 |
.success() |
| 197 |
.stdout |
| 198 |
.into_raw(); |
| 199 |
let commit_id = gix::ObjectId::from_hex(commit_id.as_bytes())?; |
| 200 |
git_repo.reference( |
| 201 |
"refs/heads/a", |
| 202 |
commit_id, |
| 203 |
gix::refs::transaction::PreviousValue::Any, |
| 204 |
"", |
| 205 |
)?; |
| 206 |
|
| 207 |
// Initial state we will return to after `undo`. There are no bookmarks. |
| 208 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @""); |
| 209 |
let base_operation_id = work_dir.current_operation_id(); |
| 210 |
|
| 211 |
let output = work_dir.run_jj(["git", "import"]); |
| 212 |
insta::assert_snapshot!(output, @" |
| 213 |
------- stderr ------- |
| 214 |
bookmark: a@git [new] tracked |
| 215 |
[EOF] |
| 216 |
"); |
| 217 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @" |
| 218 |
a: qpvuntsm e8849ae1 (empty) (no description set) |
| 219 |
@git: qpvuntsm e8849ae1 (empty) (no description set) |
| 220 |
[EOF] |
| 221 |
"); |
| 222 |
|
| 223 |
// "git import" can be undone by default. |
| 224 |
let output = work_dir.run_jj(["op", "restore", &base_operation_id]); |
| 225 |
insta::assert_snapshot!(output, @" |
| 226 |
------- stderr ------- |
| 227 |
Restored to operation: f63ee16f9553 (2001-02-03 08:05:07) add workspace 'default' |
| 228 |
[EOF] |
| 229 |
"); |
| 230 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @""); |
| 231 |
// Try "git import" again, which should re-import the bookmark "a". |
| 232 |
let output = work_dir.run_jj(["git", "import"]); |
| 233 |
insta::assert_snapshot!(output, @" |
| 234 |
------- stderr ------- |
| 235 |
bookmark: a@git [new] tracked |
| 236 |
[EOF] |
| 237 |
"); |
| 238 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @" |
| 239 |
a: qpvuntsm e8849ae1 (empty) (no description set) |
| 240 |
@git: qpvuntsm e8849ae1 (empty) (no description set) |
| 241 |
[EOF] |
| 242 |
"); |
| 243 |
Ok(()) |
| 244 |
} |
| 245 |
|
| 246 |
#[test] |
| 247 |
fn test_git_import_move_export_with_default_undo() -> TestResult { |
| 248 |
let test_env = TestEnvironment::default(); |
| 249 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 250 |
let work_dir = test_env.work_dir("repo"); |
| 251 |
let git_repo = git::open(work_dir.root().join(".jj/repo/store/git")); |
| 252 |
|
| 253 |
// Create bookmark "a" in git repo |
| 254 |
let commit_id = work_dir |
| 255 |
.run_jj(&["log", "-Tcommit_id", "--no-graph", "-r@"]) |
| 256 |
.success() |
| 257 |
.stdout |
| 258 |
.into_raw(); |
| 259 |
let commit_id = gix::ObjectId::from_hex(commit_id.as_bytes())?; |
| 260 |
git_repo.reference( |
| 261 |
"refs/heads/a", |
| 262 |
commit_id, |
| 263 |
gix::refs::transaction::PreviousValue::Any, |
| 264 |
"", |
| 265 |
)?; |
| 266 |
|
| 267 |
// Initial state we will try to return to after `op restore`. There are no |
| 268 |
// bookmarks. |
| 269 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @""); |
| 270 |
let base_operation_id = work_dir.current_operation_id(); |
| 271 |
|
| 272 |
let output = work_dir.run_jj(["git", "import"]); |
| 273 |
insta::assert_snapshot!(output, @" |
| 274 |
------- stderr ------- |
| 275 |
bookmark: a@git [new] tracked |
| 276 |
[EOF] |
| 277 |
"); |
| 278 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @" |
| 279 |
a: qpvuntsm e8849ae1 (empty) (no description set) |
| 280 |
@git: qpvuntsm e8849ae1 (empty) (no description set) |
| 281 |
[EOF] |
| 282 |
"); |
| 283 |
|
| 284 |
// Move bookmark "a" and export to git repo |
| 285 |
work_dir.run_jj(["new"]).success(); |
| 286 |
work_dir |
| 287 |
.run_jj(["bookmark", "set", "a", "--to=@"]) |
| 288 |
.success(); |
| 289 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @" |
| 290 |
a: royxmykx e7d0d5fd (empty) (no description set) |
| 291 |
@git (behind by 1 commits): qpvuntsm e8849ae1 (empty) (no description set) |
| 292 |
[EOF] |
| 293 |
"); |
| 294 |
let output = work_dir.run_jj(["git", "export"]); |
| 295 |
insta::assert_snapshot!(output, @""); |
| 296 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @" |
| 297 |
a: royxmykx e7d0d5fd (empty) (no description set) |
| 298 |
@git: royxmykx e7d0d5fd (empty) (no description set) |
| 299 |
[EOF] |
| 300 |
"); |
| 301 |
|
| 302 |
// "git import" can be undone with the default `restore` behavior, as shown in |
| 303 |
// the previous test. However, "git export" can't: the bookmarks in the git |
| 304 |
// repo stay where they were. |
| 305 |
let output = work_dir.run_jj(["op", "restore", &base_operation_id]); |
| 306 |
insta::assert_snapshot!(output, @" |
| 307 |
------- stderr ------- |
| 308 |
Restored to operation: f63ee16f9553 (2001-02-03 08:05:07) add workspace 'default' |
| 309 |
Working copy (@) now at: qpvuntsm e8849ae1 (empty) (no description set) |
| 310 |
Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set) |
| 311 |
[EOF] |
| 312 |
"); |
| 313 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @""); |
| 314 |
insta::assert_debug_snapshot!(get_git_repo_refs(&git_repo), @r#" |
| 315 |
[ |
| 316 |
( |
| 317 |
"refs/heads/a", |
| 318 |
CommitId( |
| 319 |
"e7d0d5fdaf96051d0dacec1e74d9413d64a15822", |
| 320 |
), |
| 321 |
), |
| 322 |
] |
| 323 |
"#); |
| 324 |
|
| 325 |
// The last bookmark "a" state is imported from git. No idea what's the most |
| 326 |
// intuitive result here. |
| 327 |
let output = work_dir.run_jj(["git", "import"]); |
| 328 |
insta::assert_snapshot!(output, @" |
| 329 |
------- stderr ------- |
| 330 |
bookmark: a@git [new] tracked |
| 331 |
[EOF] |
| 332 |
"); |
| 333 |
insta::assert_snapshot!(get_bookmark_output(&work_dir), @" |
| 334 |
a: royxmykx e7d0d5fd (empty) (no description set) |
| 335 |
@git: royxmykx e7d0d5fd (empty) (no description set) |
| 336 |
[EOF] |
| 337 |
"); |
| 338 |
Ok(()) |
| 339 |
} |
| 340 |
|
| 341 |
#[test] |
| 342 |
fn test_git_import_export_stats_color() -> TestResult { |
| 343 |
let test_env = TestEnvironment::default(); |
| 344 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 345 |
let work_dir = test_env.work_dir("repo"); |
| 346 |
let git_repo = git::open(work_dir.root().join(".jj/repo/store/git")); |
| 347 |
|
| 348 |
work_dir.run_jj(["bookmark", "set", "-r@", "foo"]).success(); |
| 349 |
work_dir |
| 350 |
.run_jj(["bookmark", "set", "-r@", "'un:exportable'"]) |
| 351 |
.success(); |
| 352 |
work_dir.run_jj(["new", "--no-edit", "root()"]).success(); |
| 353 |
let other_commit_id = work_dir |
| 354 |
.run_jj(&["log", "-Tcommit_id", "--no-graph", "-rvisible_heads() ~ @"]) |
| 355 |
.success() |
| 356 |
.stdout |
| 357 |
.into_raw(); |
| 358 |
|
| 359 |
let output = work_dir |
| 360 |
.run_jj(["git", "export", "--color=always"]) |
| 361 |
.success(); |
| 362 |
insta::assert_snapshot!(output, @r#" |
| 363 |
------- stderr ------- |
| 364 |
[1m[38;5;3mWarning: [39mFailed to export some bookmarks:[0m |
| 365 |
[38;5;5m"un:exportable"@git[39m: Failed to set: The ref name or path is not a valid ref name: Reference name contains invalid byte: ":" |
| 366 |
[1m[38;5;6mHint: [0m[39mGit doesn't allow a branch/tag name that looks like a parent directory of[39m |
| 367 |
[39manother (e.g. `foo` and `foo/bar`). Try to rename the bookmarks/tags that failed[39m |
| 368 |
[39mto export or their "parent" bookmarks/tags.[39m |
| 369 |
[EOF] |
| 370 |
"#); |
| 371 |
|
| 372 |
let other_commit_id = gix::ObjectId::from_hex(other_commit_id.as_bytes())?; |
| 373 |
for name in ["refs/heads/foo", "refs/heads/bar", "refs/tags/baz"] { |
| 374 |
git_repo.reference( |
| 375 |
name, |
| 376 |
other_commit_id, |
| 377 |
gix::refs::transaction::PreviousValue::Any, |
| 378 |
"", |
| 379 |
)?; |
| 380 |
} |
| 381 |
|
| 382 |
let output = work_dir |
| 383 |
.run_jj(["git", "import", "--color=always"]) |
| 384 |
.success(); |
| 385 |
insta::assert_snapshot!(output, @" |
| 386 |
------- stderr ------- |
| 387 |
bookmark: [38;5;5mbar@git[39m [new] tracked |
| 388 |
bookmark: [38;5;5mfoo@git[39m [updated] tracked |
| 389 |
tag: [38;5;5mbaz@git[39m [new] tracked |
| 390 |
[EOF] |
| 391 |
"); |
| 392 |
Ok(()) |
| 393 |
} |
| 394 |
|
| 395 |
#[must_use] |
| 396 |
fn get_bookmark_output(work_dir: &TestWorkDir) -> CommandOutput { |
| 397 |
work_dir.run_jj(["bookmark", "list", "--all-remotes"]) |
| 398 |
} |
| 399 |
|
| 400 |
fn get_git_repo_refs(git_repo: &gix::Repository) -> Vec<(bstr::BString, CommitId)> { |
| 401 |
let mut refs: Vec<_> = git_repo |
| 402 |
.references() |
| 403 |
.unwrap() |
| 404 |
.all() |
| 405 |
.unwrap() |
| 406 |
.filter_ok(|git_ref| { |
| 407 |
matches!( |
| 408 |
git_ref.name().category(), |
| 409 |
Some( |
| 410 |
gix::reference::Category::Tag |
| 411 |
| gix::reference::Category::LocalBranch |
| 412 |
| gix::reference::Category::RemoteBranch |
| 413 |
), |
| 414 |
) |
| 415 |
}) |
| 416 |
.filter_map_ok(|mut git_ref| { |
| 417 |
let full_name = git_ref.name().as_bstr().to_owned(); |
| 418 |
let git_commit = git_ref.peel_to_commit().ok()?; |
| 419 |
let commit_id = CommitId::from_bytes(git_commit.id().as_bytes()); |
| 420 |
Some((full_name, commit_id)) |
| 421 |
}) |
| 422 |
.try_collect() |
| 423 |
.unwrap(); |
| 424 |
refs.sort(); |
| 425 |
refs |
| 426 |
} |