Skip to content

Commit 2f9020a

Browse files
committed
cli: git: print current Git HEAD by "colocation status"
As I'm going to deprecate git_head()/git_refs() functions, we'll need an alternative command to debug view.git_head() movement. I think "git colocation status" is a good place to print this kind of information. We don't use "debug object view" because I expect git_head and git_refs will be removed from the view object.
1 parent cfb6509 commit 2f9020a

File tree

4 files changed

+282
-80
lines changed

4 files changed

+282
-80
lines changed

cli/src/commands/git/colocation.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use std::io::ErrorKind;
1616
use std::io::Write as _;
1717

18+
use itertools::Itertools as _;
1819
use jj_lib::commit::Commit;
1920
use jj_lib::file_util::IoResultExt as _;
2021
use jj_lib::git;
@@ -98,20 +99,40 @@ fn cmd_git_colocation_status(
9899
// Make sure that the workspace supports git colocation commands
99100
workspace_supports_git_colocation_commands(&workspace_command)?;
100101

101-
let is_colocated =
102-
is_colocated_git_workspace(workspace_command.workspace(), workspace_command.repo());
102+
let repo = workspace_command.repo();
103+
let is_colocated = is_colocated_git_workspace(workspace_command.workspace(), repo);
104+
let git_head = repo.view().git_head();
103105

104106
if is_colocated {
105107
writeln!(ui.stdout(), "Workspace is currently colocated with Git.")?;
106-
writeln!(
107-
ui.hint_default(),
108-
"To disable colocation, run: `jj git colocation disable`"
109-
)?;
110108
} else {
111109
writeln!(
112110
ui.stdout(),
113111
"Workspace is currently not colocated with Git."
114112
)?;
113+
}
114+
115+
// git_head should be absent in non-colocated workspace, but print the
116+
// actual status so we can debug problems.
117+
writeln!(
118+
ui.stdout(),
119+
"Last imported/exported Git HEAD: {}",
120+
git_head
121+
.as_merge()
122+
.iter()
123+
.map(|maybe_id| match maybe_id {
124+
Some(id) => id.to_string(),
125+
None => "(none)".to_owned(),
126+
})
127+
.join(", ")
128+
)?;
129+
130+
if is_colocated {
131+
writeln!(
132+
ui.hint_default(),
133+
"To disable colocation, run: `jj git colocation disable`"
134+
)?;
135+
} else {
115136
writeln!(
116137
ui.hint_default(),
117138
"To enable colocation, run: `jj git colocation enable`"

cli/tests/test_git_colocated.rs

Lines changed: 116 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,17 @@ fn test_git_colocated() {
5858
git_repo.head_id().unwrap().to_string(),
5959
@"97358f54806c7cd005ed5ade68a779595efbae7e"
6060
);
61+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
62+
Workspace is currently colocated with Git.
63+
Last imported/exported Git HEAD: 97358f54806c7cd005ed5ade68a779595efbae7e
64+
[EOF]
65+
");
6166

6267
// Modify the working copy. The working-copy commit should changed, but the Git
6368
// HEAD commit should not
6469
work_dir.write_file("file", "modified");
6570
insta::assert_snapshot!(get_log_output(&work_dir), @r"
66-
@ f40534d1cfee0e0916dcfbc65c31970b3c705269
71+
@ 9dfe8c7005c8dff6078ecdfd953c6bfddc633c90
6772
○ 97358f54806c7cd005ed5ade68a779595efbae7e master git_head() initial
6873
◆ 0000000000000000000000000000000000000000
6974
[EOF]
@@ -72,21 +77,31 @@ fn test_git_colocated() {
7277
git_repo.head_id().unwrap().to_string(),
7378
@"97358f54806c7cd005ed5ade68a779595efbae7e"
7479
);
80+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
81+
Workspace is currently colocated with Git.
82+
Last imported/exported Git HEAD: 97358f54806c7cd005ed5ade68a779595efbae7e
83+
[EOF]
84+
");
7585

7686
// Create a new change from jj and check that it's reflected in Git
7787
work_dir.run_jj(["new"]).success();
7888
insta::assert_snapshot!(get_log_output(&work_dir), @r"
79-
@ b369903b66e2dba03f3f6b24433670784f6180d7
80-
f40534d1cfee0e0916dcfbc65c31970b3c705269 git_head()
89+
@ 4ddddef596e9d68f729f1be9e1b2cdaaf45bef08
90+
9dfe8c7005c8dff6078ecdfd953c6bfddc633c90 git_head()
8191
○ 97358f54806c7cd005ed5ade68a779595efbae7e master initial
8292
◆ 0000000000000000000000000000000000000000
8393
[EOF]
8494
");
8595
assert!(git_repo.head().unwrap().is_detached());
8696
insta::assert_snapshot!(
8797
git_repo.head_id().unwrap().to_string(),
88-
@"f40534d1cfee0e0916dcfbc65c31970b3c705269"
98+
@"9dfe8c7005c8dff6078ecdfd953c6bfddc633c90"
8999
);
100+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
101+
Workspace is currently colocated with Git.
102+
Last imported/exported Git HEAD: 9dfe8c7005c8dff6078ecdfd953c6bfddc633c90
103+
[EOF]
104+
");
90105
}
91106

92107
#[test]
@@ -195,13 +210,18 @@ fn test_git_colocated_unborn_bookmark() {
195210
◆ 0000000000000000000000000000000000000000
196211
[EOF]
197212
");
213+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
214+
Workspace is currently colocated with Git.
215+
Last imported/exported Git HEAD: (none)
216+
[EOF]
217+
");
198218

199219
// Stage some change, and check out root. This shouldn't clobber the HEAD.
200220
add_file_to_index("file0", "");
201221
let output = work_dir.run_jj(["new", "root()"]);
202222
insta::assert_snapshot!(output, @r"
203223
------- stderr -------
204-
Working copy (@) now at: kkmpptxz 2b17ac71 (empty) (no description set)
224+
Working copy (@) now at: zsuskuln c2934cfb (empty) (no description set)
205225
Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
206226
Added 0 files, modified 0 files, removed 1 files
207227
[EOF]
@@ -212,17 +232,22 @@ fn test_git_colocated_unborn_bookmark() {
212232
b"refs/heads/master"
213233
);
214234
insta::assert_snapshot!(get_log_output(&work_dir), @r"
215-
@ 2b17ac719c7db025e2514f5708d2b0328fc6b268
216-
│ ○ 1d68db605e7f3722d6869beab15183f0e41fd45c
235+
@ c2934cfbfb196d2c473959667beffcc19e71e5e8
236+
│ ○ e6669bb3438ef218fa618e1047a1911d2b3410dd
217237
├─╯
218238
◆ 0000000000000000000000000000000000000000
219239
[EOF]
220240
");
241+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
242+
Workspace is currently colocated with Git.
243+
Last imported/exported Git HEAD: (none)
244+
[EOF]
245+
");
221246
// Staged change shouldn't persist.
222247
checkout_index();
223248
insta::assert_snapshot!(work_dir.run_jj(["status"]), @r"
224249
The working copy has no changes.
225-
Working copy (@) : kkmpptxz 2b17ac71 (empty) (no description set)
250+
Working copy (@) : zsuskuln c2934cfb (empty) (no description set)
226251
Parent commit (@-): zzzzzzzz 00000000 (empty) (no description set)
227252
[EOF]
228253
");
@@ -233,29 +258,34 @@ fn test_git_colocated_unborn_bookmark() {
233258
let output = work_dir.run_jj(["new"]);
234259
insta::assert_snapshot!(output, @r"
235260
------- stderr -------
236-
Working copy (@) now at: royxmykx c5b52bf2 (empty) (no description set)
237-
Parent commit (@-) : kkmpptxz 54ca7830 (no description set)
261+
Working copy (@) now at: vruxwmqv 2d7a8abb (empty) (no description set)
262+
Parent commit (@-) : zsuskuln ff536684 (no description set)
238263
[EOF]
239264
");
240265
assert!(git_repo.head().unwrap().is_detached());
241266
insta::assert_snapshot!(
242267
git_repo.head_id().unwrap().to_string(),
243-
@"54ca78301ccd2e0da397694ab34160d539a40e86"
268+
@"ff5366846b039b25c6c4998fa74dca821c246243"
244269
);
245270
insta::assert_snapshot!(get_log_output(&work_dir), @r"
246-
@ c5b52bf20a14ca728cbb2a56b9dffabc266251bd
247-
54ca78301ccd2e0da397694ab34160d539a40e86 git_head()
248-
│ ○ 1d68db605e7f3722d6869beab15183f0e41fd45c
271+
@ 2d7a8abb601ebf559df4037279e9f2e851a75e63
272+
ff5366846b039b25c6c4998fa74dca821c246243 git_head()
273+
│ ○ e6669bb3438ef218fa618e1047a1911d2b3410dd
249274
├─╯
250275
◆ 0000000000000000000000000000000000000000
251276
[EOF]
252277
");
278+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
279+
Workspace is currently colocated with Git.
280+
Last imported/exported Git HEAD: ff5366846b039b25c6c4998fa74dca821c246243
281+
[EOF]
282+
");
253283
// Staged change shouldn't persist.
254284
checkout_index();
255285
insta::assert_snapshot!(work_dir.run_jj(["status"]), @r"
256286
The working copy has no changes.
257-
Working copy (@) : royxmykx c5b52bf2 (empty) (no description set)
258-
Parent commit (@-): kkmpptxz 54ca7830 (no description set)
287+
Working copy (@) : vruxwmqv 2d7a8abb (empty) (no description set)
288+
Parent commit (@-): zsuskuln ff536684 (no description set)
259289
[EOF]
260290
");
261291

@@ -270,27 +300,32 @@ fn test_git_colocated_unborn_bookmark() {
270300
let output = work_dir.run_jj(["new", "root()"]);
271301
insta::assert_snapshot!(output, @r"
272302
------- stderr -------
273-
Working copy (@) now at: znkkpsqq 2b2f7cb0 (empty) (no description set)
303+
Working copy (@) now at: wqnwkozp 88e8407a (empty) (no description set)
274304
Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
275305
Added 0 files, modified 0 files, removed 2 files
276306
[EOF]
277307
");
278308
assert!(git_repo.head().unwrap().is_unborn());
279309
insta::assert_snapshot!(get_log_output(&work_dir), @r"
280-
@ 2b2f7cb00d53f5c0675efb09cbe1a826ce1167a4
281-
│ ○ 6c3d40f5a3260d762cd52a8ff6d09883c88d8db5
282-
│ ○ 54ca78301ccd2e0da397694ab34160d539a40e86 master
310+
@ 88e8407a4f0a5e6f40a7c6c494106764adc00fed
311+
│ ○ 2dd7385602e703388fd266b939bba6f57a1439d3
312+
│ ○ ff5366846b039b25c6c4998fa74dca821c246243 master
283313
├─╯
284-
│ ○ 1d68db605e7f3722d6869beab15183f0e41fd45c
314+
│ ○ e6669bb3438ef218fa618e1047a1911d2b3410dd
285315
├─╯
286316
◆ 0000000000000000000000000000000000000000
287317
[EOF]
288318
");
319+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
320+
Workspace is currently colocated with Git.
321+
Last imported/exported Git HEAD: (none)
322+
[EOF]
323+
");
289324
// Staged change shouldn't persist.
290325
checkout_index();
291326
insta::assert_snapshot!(work_dir.run_jj(["status"]), @r"
292327
The working copy has no changes.
293-
Working copy (@) : znkkpsqq 2b2f7cb0 (empty) (no description set)
328+
Working copy (@) : wqnwkozp 88e8407a (empty) (no description set)
294329
Parent commit (@-): zzzzzzzz 00000000 (empty) (no description set)
295330
[EOF]
296331
");
@@ -300,21 +335,26 @@ fn test_git_colocated_unborn_bookmark() {
300335
let output = work_dir.run_jj(["new"]);
301336
insta::assert_snapshot!(output, @r"
302337
------- stderr -------
303-
Working copy (@) now at: wqnwkozp 4253b9c0 (empty) (no description set)
304-
Parent commit (@-) : znkkpsqq b8df84db (no description set)
338+
Working copy (@) now at: uyznsvlq 2fb16499 (empty) (no description set)
339+
Parent commit (@-) : wqnwkozp bb21bc2d (no description set)
305340
[EOF]
306341
");
307342
insta::assert_snapshot!(get_log_output(&work_dir), @r"
308-
@ 4253b9c0f70fd5287c2af4e96b779da6066757fd
309-
b8df84db65f6a75ace38ceebca6ed8be781ec754 git_head()
310-
│ ○ 6c3d40f5a3260d762cd52a8ff6d09883c88d8db5
311-
│ ○ 54ca78301ccd2e0da397694ab34160d539a40e86 master
343+
@ 2fb16499a987e632407402e38976ed250c939c42
344+
bb21bc2dce2af92973fdd6d42686d77bd16bc466 git_head()
345+
│ ○ 2dd7385602e703388fd266b939bba6f57a1439d3
346+
│ ○ ff5366846b039b25c6c4998fa74dca821c246243 master
312347
├─╯
313-
│ ○ 1d68db605e7f3722d6869beab15183f0e41fd45c
348+
│ ○ e6669bb3438ef218fa618e1047a1911d2b3410dd
314349
├─╯
315350
◆ 0000000000000000000000000000000000000000
316351
[EOF]
317352
");
353+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
354+
Workspace is currently colocated with Git.
355+
Last imported/exported Git HEAD: bb21bc2dce2af92973fdd6d42686d77bd16bc466
356+
[EOF]
357+
");
318358
}
319359

320360
#[test]
@@ -634,6 +674,11 @@ fn test_git_colocated_checkout_non_empty_working_copy() {
634674
◆ 0000000000000000000000000000000000000000
635675
[EOF]
636676
");
677+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
678+
Workspace is currently colocated with Git.
679+
Last imported/exported Git HEAD: 97358f54806c7cd005ed5ade68a779595efbae7e
680+
[EOF]
681+
");
637682
}
638683

639684
#[test]
@@ -951,6 +996,11 @@ fn test_git_colocated_undo_head_move() {
951996
◆ 0000000000000000000000000000000000000000
952997
[EOF]
953998
");
999+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
1000+
Workspace is currently colocated with Git.
1001+
Last imported/exported Git HEAD: e8849ae12c709f2321908879bc724fdb2ab8a781
1002+
[EOF]
1003+
");
9541004

9551005
// HEAD should be unset
9561006
work_dir.run_jj(["undo"]).success();
@@ -960,28 +1010,38 @@ fn test_git_colocated_undo_head_move() {
9601010
◆ 0000000000000000000000000000000000000000
9611011
[EOF]
9621012
");
1013+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
1014+
Workspace is currently colocated with Git.
1015+
Last imported/exported Git HEAD: (none)
1016+
[EOF]
1017+
");
9631018

9641019
// Create commit on non-root commit
9651020
work_dir.run_jj(["new"]).success();
9661021
work_dir.run_jj(["new"]).success();
9671022
insta::assert_snapshot!(get_log_output(&work_dir), @r"
968-
@ 47762194c5b3d9a9280ee7cfd2b9db16158b1b3c
969-
e7d0d5fdaf96051d0dacec1e74d9413d64a15822 git_head()
1023+
@ 5e37f1b8313299eb1b62221eefcf32881b0dc4c6
1024+
23e6e06a7471634da3567ef975fadf883082658f git_head()
9701025
○ e8849ae12c709f2321908879bc724fdb2ab8a781
9711026
◆ 0000000000000000000000000000000000000000
9721027
[EOF]
9731028
");
1029+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
1030+
Workspace is currently colocated with Git.
1031+
Last imported/exported Git HEAD: 23e6e06a7471634da3567ef975fadf883082658f
1032+
[EOF]
1033+
");
9741034
assert!(git_repo.head().unwrap().is_detached());
9751035
insta::assert_snapshot!(
9761036
git_repo.head_id().unwrap().to_string(),
977-
@"e7d0d5fdaf96051d0dacec1e74d9413d64a15822");
1037+
@"23e6e06a7471634da3567ef975fadf883082658f");
9781038

9791039
// HEAD should be moved back
9801040
let output = work_dir.run_jj(["undo"]);
9811041
insta::assert_snapshot!(output, @r"
9821042
------- stderr -------
983-
Restored to operation: 28f10852fc94 (2001-02-03 08:05:12) new empty commit
984-
Working copy (@) now at: royxmykx e7d0d5fd (empty) (no description set)
1043+
Restored to operation: b528a8c9176f (2001-02-03 08:05:14) new empty commit
1044+
Working copy (@) now at: vruxwmqv 23e6e06a (empty) (no description set)
9851045
Parent commit (@-) : qpvuntsm e8849ae1 (empty) (no description set)
9861046
[EOF]
9871047
");
@@ -990,11 +1050,16 @@ fn test_git_colocated_undo_head_move() {
9901050
git_repo.head_id().unwrap().to_string(),
9911051
@"e8849ae12c709f2321908879bc724fdb2ab8a781");
9921052
insta::assert_snapshot!(get_log_output(&work_dir), @r"
993-
@ e7d0d5fdaf96051d0dacec1e74d9413d64a15822
1053+
@ 23e6e06a7471634da3567ef975fadf883082658f
9941054
○ e8849ae12c709f2321908879bc724fdb2ab8a781 git_head()
9951055
◆ 0000000000000000000000000000000000000000
9961056
[EOF]
9971057
");
1058+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
1059+
Workspace is currently colocated with Git.
1060+
Last imported/exported Git HEAD: e8849ae12c709f2321908879bc724fdb2ab8a781
1061+
[EOF]
1062+
");
9981063
}
9991064

10001065
#[test]
@@ -1637,6 +1702,11 @@ fn test_git_colocated_operation_cleanup() {
16371702
◆ 0000000000000000000000000000000000000000
16381703
[EOF]
16391704
");
1705+
insta::assert_snapshot!(get_colocation_status(&work_dir), @r"
1706+
Workspace is currently colocated with Git.
1707+
Last imported/exported Git HEAD: cf3bb116ded416d9b202e71303f260e504c2eeb9
1708+
[EOF]
1709+
");
16401710

16411711
// Check that the operation was correctly aborted.
16421712
assert!(!std::fs::exists(work_dir.root().join(".git").join("rebase-merge")).unwrap());
@@ -1654,3 +1724,14 @@ fn get_bookmark_output(work_dir: &TestWorkDir) -> CommandOutput {
16541724
// --quiet to suppress deleted bookmarks hint
16551725
work_dir.run_jj(["bookmark", "list", "--all-remotes", "--quiet"])
16561726
}
1727+
1728+
#[must_use]
1729+
fn get_colocation_status(work_dir: &TestWorkDir) -> CommandOutput {
1730+
work_dir.run_jj([
1731+
"git",
1732+
"colocation",
1733+
"status",
1734+
"--ignore-working-copy",
1735+
"--quiet", // suppress hint
1736+
])
1737+
}

0 commit comments

Comments
 (0)