| 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 crate::common::TestEnvironment; |
| 16 |
use crate::common::TestWorkDir; |
| 17 |
|
| 18 |
fn set_up(test_env: &TestEnvironment) { |
| 19 |
test_env.run_jj_in(".", ["git", "init", "origin"]).success(); |
| 20 |
let origin_dir = test_env.work_dir("origin"); |
| 21 |
let origin_git_repo_path = origin_dir |
| 22 |
.root() |
| 23 |
.join(".jj") |
| 24 |
.join("repo") |
| 25 |
.join("store") |
| 26 |
.join("git"); |
| 27 |
|
| 28 |
origin_dir.run_jj(["describe", "-m=public 1"]).success(); |
| 29 |
origin_dir.run_jj(["new", "-m=public 2"]).success(); |
| 30 |
origin_dir |
| 31 |
.run_jj(["bookmark", "create", "-r@", "main"]) |
| 32 |
.success(); |
| 33 |
origin_dir.run_jj(["git", "export"]).success(); |
| 34 |
|
| 35 |
test_env |
| 36 |
.run_jj_in( |
| 37 |
".", |
| 38 |
[ |
| 39 |
"git", |
| 40 |
"clone", |
| 41 |
"--config=remotes.origin.auto-track-bookmarks='*'", |
| 42 |
origin_git_repo_path.to_str().unwrap(), |
| 43 |
"local", |
| 44 |
], |
| 45 |
) |
| 46 |
.success(); |
| 47 |
} |
| 48 |
|
| 49 |
fn set_up_remote_at_main(test_env: &TestEnvironment, work_dir: &TestWorkDir, remote_name: &str) { |
| 50 |
test_env |
| 51 |
.run_jj_in(".", ["git", "init", remote_name]) |
| 52 |
.success(); |
| 53 |
let other_path = test_env.env_root().join(remote_name); |
| 54 |
let other_git_repo_path = other_path |
| 55 |
.join(".jj") |
| 56 |
.join("repo") |
| 57 |
.join("store") |
| 58 |
.join("git"); |
| 59 |
work_dir |
| 60 |
.run_jj([ |
| 61 |
"git", |
| 62 |
"remote", |
| 63 |
"add", |
| 64 |
remote_name, |
| 65 |
other_git_repo_path.to_str().unwrap(), |
| 66 |
]) |
| 67 |
.success(); |
| 68 |
work_dir |
| 69 |
.run_jj(["bookmark", "track", "--remote", remote_name, "main"]) |
| 70 |
.success(); |
| 71 |
work_dir |
| 72 |
.run_jj(["git", "push", "--remote", remote_name, "-b=main"]) |
| 73 |
.success(); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_git_private_commits_block_pushing() { |
| 78 |
let test_env = TestEnvironment::default(); |
| 79 |
set_up(&test_env); |
| 80 |
test_env.add_config("revset-aliases.'immutable_heads()' = 'none()'"); |
| 81 |
let origin_dir = test_env.work_dir("origin"); |
| 82 |
let work_dir = test_env.work_dir("local"); |
| 83 |
|
| 84 |
// Add tracked tag |
| 85 |
origin_dir.run_jj(["tag", "set", "alpha", "-r@"]).success(); |
| 86 |
origin_dir.run_jj(["git", "export"]).success(); |
| 87 |
work_dir.run_jj(["git", "fetch", "--tag=*"]).success(); |
| 88 |
|
| 89 |
// Update bookmarks and tags |
| 90 |
work_dir.run_jj(["new", "main", "-m=private 1"]).success(); |
| 91 |
work_dir |
| 92 |
.run_jj(["bookmark", "set", "main", "-r@"]) |
| 93 |
.success(); |
| 94 |
work_dir.run_jj(["bookmark", "track", "main"]).success(); |
| 95 |
work_dir.run_jj(["new", "root()", "-m=private 2"]).success(); |
| 96 |
work_dir |
| 97 |
.run_jj(["tag", "set", "--allow-move", "alpha", "-r@"]) |
| 98 |
.success(); |
| 99 |
|
| 100 |
// Will not push when a pushed commit is contained in git.private-commits |
| 101 |
test_env.add_config(r#"git.private-commits = "description('private*')""#); |
| 102 |
let output = work_dir.run_jj(["git", "push", "--all"]); |
| 103 |
insta::assert_snapshot!(output, @" |
| 104 |
------- stderr ------- |
| 105 |
Warning: Won't push bookmark main: commit 469f044473ed is private |
| 106 |
znkkpsqq 469f0444 main* | (empty) private 1 |
| 107 |
Hint: Configured git.private-commits: 'description('private*')' |
| 108 |
Warning: Won't push tag alpha: commit 69b30fdbc569 is private |
| 109 |
wqnwkozp 69b30fdb (empty) private 2 |
| 110 |
Hint: Configured git.private-commits: 'description('private*')' |
| 111 |
Nothing changed. |
| 112 |
[EOF] |
| 113 |
"); |
| 114 |
let output = work_dir.run_jj(["git", "push", "--tracked"]); |
| 115 |
insta::assert_snapshot!(output, @" |
| 116 |
------- stderr ------- |
| 117 |
Warning: Won't push bookmark main: commit 469f044473ed is private |
| 118 |
znkkpsqq 469f0444 main* | (empty) private 1 |
| 119 |
Hint: Configured git.private-commits: 'description('private*')' |
| 120 |
Warning: Won't push tag alpha: commit 69b30fdbc569 is private |
| 121 |
wqnwkozp 69b30fdb (empty) private 2 |
| 122 |
Hint: Configured git.private-commits: 'description('private*')' |
| 123 |
Nothing changed. |
| 124 |
[EOF] |
| 125 |
"); |
| 126 |
let output = work_dir.run_jj(["git", "push", "-rall()"]); |
| 127 |
insta::assert_snapshot!(output, @" |
| 128 |
------- stderr ------- |
| 129 |
Warning: Won't push bookmark main: commit 469f044473ed is private |
| 130 |
znkkpsqq 469f0444 main* | (empty) private 1 |
| 131 |
Hint: Configured git.private-commits: 'description('private*')' |
| 132 |
Warning: Won't push tag alpha: commit 69b30fdbc569 is private |
| 133 |
wqnwkozp 69b30fdb (empty) private 2 |
| 134 |
Hint: Configured git.private-commits: 'description('private*')' |
| 135 |
Nothing changed. |
| 136 |
[EOF] |
| 137 |
"); |
| 138 |
let output = work_dir.run_jj(["git", "push", "--change=@"]); |
| 139 |
insta::assert_snapshot!(output, @" |
| 140 |
------- stderr ------- |
| 141 |
Creating bookmark push-wqnwkozpkust for revision wqnwkozpkust |
| 142 |
Error: Won't push commit 69b30fdbc569 since it is private |
| 143 |
Hint: Rejected commit: wqnwkozp 69b30fdb (empty) private 2 |
| 144 |
Hint: Configured git.private-commits: 'description('private*')' |
| 145 |
[EOF] |
| 146 |
[exit status: 1] |
| 147 |
"); |
| 148 |
let output = work_dir.run_jj(["git", "push", "--bookmark=*"]); |
| 149 |
insta::assert_snapshot!(output, @" |
| 150 |
------- stderr ------- |
| 151 |
Error: Won't push commit 469f044473ed since it is private |
| 152 |
Hint: Rejected commit: znkkpsqq 469f0444 main* | (empty) private 1 |
| 153 |
Hint: Configured git.private-commits: 'description('private*')' |
| 154 |
[EOF] |
| 155 |
[exit status: 1] |
| 156 |
"); |
| 157 |
let output = work_dir.run_jj(["git", "push", "--tag=*"]); |
| 158 |
insta::assert_snapshot!(output, @" |
| 159 |
------- stderr ------- |
| 160 |
Error: Won't push commit 69b30fdbc569 since it is private |
| 161 |
Hint: Rejected commit: wqnwkozp 69b30fdb (empty) private 2 |
| 162 |
Hint: Configured git.private-commits: 'description('private*')' |
| 163 |
[EOF] |
| 164 |
[exit status: 1] |
| 165 |
"); |
| 166 |
|
| 167 |
// May push when the commit is removed from git.private-commits |
| 168 |
test_env.add_config(r#"git.private-commits = "none()""#); |
| 169 |
let output = work_dir.run_jj(["git", "push", "--all"]); |
| 170 |
insta::assert_snapshot!(output, @" |
| 171 |
------- stderr ------- |
| 172 |
Changes to push to origin: |
| 173 |
bookmark: main [move forward from 95cc152cd086 to 469f044473ed] |
| 174 |
tag: alpha [move sideways from 95cc152cd086 to 69b30fdbc569] |
| 175 |
[EOF] |
| 176 |
"); |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn test_git_private_commits_can_be_overridden() { |
| 181 |
let test_env = TestEnvironment::default(); |
| 182 |
set_up(&test_env); |
| 183 |
let work_dir = test_env.work_dir("local"); |
| 184 |
|
| 185 |
work_dir.run_jj(["new", "main", "-m=private 1"]).success(); |
| 186 |
work_dir |
| 187 |
.run_jj(["bookmark", "set", "main", "-r@"]) |
| 188 |
.success(); |
| 189 |
|
| 190 |
// Will not push when a pushed commit is contained in git.private-commits |
| 191 |
test_env.add_config(r#"git.private-commits = "description('private*')""#); |
| 192 |
let output = work_dir.run_jj(["git", "push", "--all"]); |
| 193 |
insta::assert_snapshot!(output, @" |
| 194 |
------- stderr ------- |
| 195 |
Warning: Won't push bookmark main: commit 7f665ca27d4e is private |
| 196 |
yqosqzyt 7f665ca2 main* | (empty) private 1 |
| 197 |
Hint: Configured git.private-commits: 'description('private*')' |
| 198 |
Nothing changed. |
| 199 |
[EOF] |
| 200 |
"); |
| 201 |
|
| 202 |
// May push when the commit is removed from git.private-commits |
| 203 |
let output = work_dir.run_jj(["git", "push", "--all", "--allow-private"]); |
| 204 |
insta::assert_snapshot!(output, @" |
| 205 |
------- stderr ------- |
| 206 |
Changes to push to origin: |
| 207 |
bookmark: main [move forward from 95cc152cd086 to 7f665ca27d4e] |
| 208 |
Warning: The working-copy commit became immutable; a new commit has been created on top of it. |
| 209 |
Working copy (@) now at: znkkpsqq 8227d51b (empty) (no description set) |
| 210 |
Parent commit (@-) : yqosqzyt 7f665ca2 main | (empty) private 1 |
| 211 |
[EOF] |
| 212 |
"); |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn test_git_private_commits_are_not_checked_if_immutable() { |
| 217 |
let test_env = TestEnvironment::default(); |
| 218 |
set_up(&test_env); |
| 219 |
let work_dir = test_env.work_dir("local"); |
| 220 |
|
| 221 |
work_dir.run_jj(["new", "main", "-m=private 1"]).success(); |
| 222 |
work_dir |
| 223 |
.run_jj(["bookmark", "set", "main", "-r@"]) |
| 224 |
.success(); |
| 225 |
|
| 226 |
test_env.add_config(r#"git.private-commits = "description('private*')""#); |
| 227 |
test_env.add_config(r#"revset-aliases."immutable_heads()" = "all()""#); |
| 228 |
let output = work_dir.run_jj(["git", "push", "--all"]); |
| 229 |
insta::assert_snapshot!(output, @" |
| 230 |
------- stderr ------- |
| 231 |
Changes to push to origin: |
| 232 |
bookmark: main [move forward from 95cc152cd086 to 7f665ca27d4e] |
| 233 |
Warning: The working-copy commit became immutable; a new commit has been created on top of it. |
| 234 |
Working copy (@) now at: yostqsxw 17947f20 (empty) (no description set) |
| 235 |
Parent commit (@-) : yqosqzyt 7f665ca2 main | (empty) private 1 |
| 236 |
[EOF] |
| 237 |
"); |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn test_git_private_commits_not_directly_in_line_block_pushing() { |
| 242 |
let test_env = TestEnvironment::default(); |
| 243 |
set_up(&test_env); |
| 244 |
test_env.add_config("remotes.origin.auto-track-bookmarks = '*'"); |
| 245 |
let work_dir = test_env.work_dir("local"); |
| 246 |
|
| 247 |
// New private commit descended from root() |
| 248 |
work_dir.run_jj(["new", "root()", "-m=private 1"]).success(); |
| 249 |
|
| 250 |
work_dir |
| 251 |
.run_jj(["new", "main", "@", "-m=public 3"]) |
| 252 |
.success(); |
| 253 |
work_dir |
| 254 |
.run_jj(["bookmark", "create", "-r@", "bookmark1"]) |
| 255 |
.success(); |
| 256 |
|
| 257 |
test_env.add_config(r#"git.private-commits = "description('private*')""#); |
| 258 |
let output = work_dir.run_jj(["git", "push", "-b=bookmark1"]); |
| 259 |
insta::assert_snapshot!(output, @" |
| 260 |
------- stderr ------- |
| 261 |
Error: Won't push commit 613114f44bdd since it is private |
| 262 |
Hint: Rejected commit: yqosqzyt 613114f4 (empty) private 1 |
| 263 |
Hint: Configured git.private-commits: 'description('private*')' |
| 264 |
[EOF] |
| 265 |
[exit status: 1] |
| 266 |
"); |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn test_git_private_commits_descending_from_commits_pushed_do_not_block_pushing() { |
| 271 |
let test_env = TestEnvironment::default(); |
| 272 |
set_up(&test_env); |
| 273 |
let work_dir = test_env.work_dir("local"); |
| 274 |
|
| 275 |
work_dir.run_jj(["new", "main", "-m=public 3"]).success(); |
| 276 |
work_dir |
| 277 |
.run_jj(["bookmark", "move", "main", "--to=@"]) |
| 278 |
.success(); |
| 279 |
work_dir.run_jj(["new", "-m=private 1"]).success(); |
| 280 |
work_dir |
| 281 |
.run_jj(["bookmark", "set", "wip1", "wip2", "--to=@"]) |
| 282 |
.success(); |
| 283 |
|
| 284 |
test_env.add_config(r#"git.private-commits = "description('private*')""#); |
| 285 |
let output = work_dir.run_jj(["git", "push", "--all"]); |
| 286 |
insta::assert_snapshot!(output, @" |
| 287 |
------- stderr ------- |
| 288 |
Warning: Won't push bookmark wip1: commit c3ad06b3e0ea is private |
| 289 |
yostqsxw c3ad06b3 wip1 wip2 | (empty) private 1 |
| 290 |
Hint: Configured git.private-commits: 'description('private*')' |
| 291 |
Warning: Won't push bookmark wip2: commit c3ad06b3e0ea is private |
| 292 |
yostqsxw c3ad06b3 wip1 wip2 | (empty) private 1 |
| 293 |
Hint: Configured git.private-commits: 'description('private*')' |
| 294 |
Changes to push to origin: |
| 295 |
bookmark: main [move forward from 95cc152cd086 to f0291dea729d] |
| 296 |
[EOF] |
| 297 |
"); |
| 298 |
} |
| 299 |
|
| 300 |
#[test] |
| 301 |
fn test_git_private_commits_already_on_the_remote_do_not_block_push() { |
| 302 |
let test_env = TestEnvironment::default(); |
| 303 |
set_up(&test_env); |
| 304 |
test_env.add_config("remotes.origin.auto-track-bookmarks = '*'"); |
| 305 |
let work_dir = test_env.work_dir("local"); |
| 306 |
|
| 307 |
// Start a bookmark before a "private" commit lands in main |
| 308 |
work_dir |
| 309 |
.run_jj(["bookmark", "create", "bookmark1", "-r=main"]) |
| 310 |
.success(); |
| 311 |
|
| 312 |
// Push a commit that would become a private_root if it weren't already on |
| 313 |
// the remote |
| 314 |
work_dir.run_jj(["new", "main", "-m=private 1"]).success(); |
| 315 |
work_dir.run_jj(["new", "-m=public 3"]).success(); |
| 316 |
work_dir |
| 317 |
.run_jj(["bookmark", "set", "main", "-r@"]) |
| 318 |
.success(); |
| 319 |
let output = work_dir.run_jj(["git", "push", "-b=main", "-b=bookmark1"]); |
| 320 |
insta::assert_snapshot!(output, @" |
| 321 |
------- stderr ------- |
| 322 |
Changes to push to origin: |
| 323 |
bookmark: bookmark1 [add to 95cc152cd086] |
| 324 |
bookmark: main [move forward from 95cc152cd086 to 03bc2bf271e0] |
| 325 |
Warning: The working-copy commit became immutable; a new commit has been created on top of it. |
| 326 |
Working copy (@) now at: kpqxywon 5308110d (empty) (no description set) |
| 327 |
Parent commit (@-) : yostqsxw 03bc2bf2 main | (empty) public 3 |
| 328 |
[EOF] |
| 329 |
"); |
| 330 |
|
| 331 |
test_env.add_config(r#"git.private-commits = "subject('private*')""#); |
| 332 |
|
| 333 |
// Since "private 1" is already on the remote, pushing it should be allowed |
| 334 |
work_dir |
| 335 |
.run_jj(["bookmark", "set", "bookmark1", "-r=main"]) |
| 336 |
.success(); |
| 337 |
let output = work_dir.run_jj(["git", "push", "--all"]); |
| 338 |
insta::assert_snapshot!(output, @" |
| 339 |
------- stderr ------- |
| 340 |
Changes to push to origin: |
| 341 |
bookmark: bookmark1 [move forward from 95cc152cd086 to 03bc2bf271e0] |
| 342 |
[EOF] |
| 343 |
"); |
| 344 |
|
| 345 |
// Ensure that the already-pushed commit doesn't block a new bookmark from |
| 346 |
// being pushed |
| 347 |
work_dir |
| 348 |
.run_jj(["new", "subject('private 1')", "-m=public 4"]) |
| 349 |
.success(); |
| 350 |
work_dir |
| 351 |
.run_jj(["bookmark", "create", "-r@", "bookmark2"]) |
| 352 |
.success(); |
| 353 |
let output = work_dir.run_jj(["git", "push", "-b=bookmark2"]); |
| 354 |
insta::assert_snapshot!(output, @" |
| 355 |
------- stderr ------- |
| 356 |
Changes to push to origin: |
| 357 |
bookmark: bookmark2 [add to 987ee765174d] |
| 358 |
[EOF] |
| 359 |
"); |
| 360 |
} |
| 361 |
|
| 362 |
#[test] |
| 363 |
fn test_git_private_commits_are_evaluated_separately_for_each_remote() { |
| 364 |
let test_env = TestEnvironment::default(); |
| 365 |
set_up(&test_env); |
| 366 |
let work_dir = test_env.work_dir("local"); |
| 367 |
set_up_remote_at_main(&test_env, &work_dir, "other"); |
| 368 |
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#); |
| 369 |
|
| 370 |
// Push a commit that would become a private_root if it weren't already on |
| 371 |
// the remote |
| 372 |
work_dir.run_jj(["new", "main", "-m=private 1"]).success(); |
| 373 |
work_dir.run_jj(["new", "-m=public 3"]).success(); |
| 374 |
work_dir |
| 375 |
.run_jj(["bookmark", "set", "main", "-r@"]) |
| 376 |
.success(); |
| 377 |
let output = work_dir.run_jj(["git", "push", "-b=main"]); |
| 378 |
insta::assert_snapshot!(output, @" |
| 379 |
------- stderr ------- |
| 380 |
Changes to push to origin: |
| 381 |
bookmark: main [move forward from 95cc152cd086 to efa3666d00e4] |
| 382 |
[EOF] |
| 383 |
"); |
| 384 |
|
| 385 |
test_env.add_config(r#"git.private-commits = "description('private*')""#); |
| 386 |
|
| 387 |
// But pushing to a repo that doesn't have the private commit yet is still |
| 388 |
// blocked |
| 389 |
let output = work_dir.run_jj(["git", "push", "--remote=other", "-b=main"]); |
| 390 |
insta::assert_snapshot!(output, @" |
| 391 |
------- stderr ------- |
| 392 |
Error: Won't push commit a714fa972186 since it is private |
| 393 |
Hint: Rejected commit: kpqxywon a714fa97 (empty) private 1 |
| 394 |
Hint: Configured git.private-commits: 'description('private*')' |
| 395 |
[EOF] |
| 396 |
[exit status: 1] |
| 397 |
"); |
| 398 |
} |