Skip to content

Commit 3876e0b

Browse files
committed
cli: complete: omit @Remote part from bookmarks to track/untrack
1 parent b9371ff commit 3876e0b

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

cli/src/complete.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ pub fn tracked_bookmarks() -> Vec<CompletionCandidate> {
113113
Ok(String::from_utf8_lossy(&output.stdout)
114114
.lines()
115115
.map(split_help_text)
116-
.map(|(name, help)| CompletionCandidate::new(name).help(help))
116+
.filter_map(|(symbol, help)| Some((symbol.split_once('@')?, help)))
117+
// There may be multiple remote bookmarks to untrack. Just pick the
118+
// first one for help text.
119+
.dedup_by(|((name1, _), _), ((name2, _), _)| name1 == name2)
120+
.map(|((name, _remote), help)| CompletionCandidate::new(name).help(help))
117121
.collect())
118122
})
119123
}
@@ -177,9 +181,13 @@ pub fn untracked_bookmarks() -> Vec<CompletionCandidate> {
177181
.retain(|(bookmark, _help)| !already_tracked_bookmarks.contains(&bookmark.as_str()));
178182

179183
Ok(possible_bookmarks_to_track
180-
.into_iter()
181-
.map(|(bookmark, help)| {
182-
CompletionCandidate::new(bookmark).help(Some(help.to_string().into()))
184+
.iter()
185+
.filter_map(|(symbol, help)| Some((symbol.split_once('@')?, help)))
186+
// There may be multiple remote bookmarks to track. Just pick the
187+
// first one for help text.
188+
.dedup_by(|((name1, _), _), ((name2, _), _)| name1 == name2)
189+
.map(|((name, _remote), help)| {
190+
CompletionCandidate::new(name).help(Some(help.to_string().into()))
183191
})
184192
.collect())
185193
})

cli/tests/test_completion.rs

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ fn test_bookmark_names() {
136136
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
137137
let work_dir = test_env.work_dir("repo");
138138

139-
test_env.run_jj_in(".", ["git", "init", "origin"]).success();
139+
test_env
140+
.run_jj_in(".", ["git", "init", "--colocate", "origin"])
141+
.success();
140142
let origin_dir = test_env.work_dir("origin");
141-
let origin_git_repo_path = origin_dir
142-
.root()
143-
.join(".jj")
144-
.join("repo")
145-
.join("store")
146-
.join("git");
143+
test_env
144+
.run_jj_in(".", ["git", "init", "--colocate", "upstream"])
145+
.success();
146+
let _upstream_dir = test_env.work_dir("upstream");
147147

148148
work_dir
149149
.run_jj(["bookmark", "create", "-r@", "aaa-local"])
@@ -154,13 +154,10 @@ fn test_bookmark_names() {
154154

155155
// add various remote branches
156156
work_dir
157-
.run_jj([
158-
"git",
159-
"remote",
160-
"add",
161-
"origin",
162-
origin_git_repo_path.to_str().unwrap(),
163-
])
157+
.run_jj(["git", "remote", "add", "origin", "../origin"])
158+
.success();
159+
work_dir
160+
.run_jj(["git", "remote", "add", "upstream", "../upstream"])
164161
.success();
165162
work_dir
166163
.run_jj(["bookmark", "create", "-r@", "aaa-tracked"])
@@ -174,8 +171,18 @@ fn test_bookmark_names() {
174171
work_dir
175172
.run_jj(["desc", "-r", "bbb-tracked", "-m", "x"])
176173
.success();
174+
175+
work_dir
176+
.run_jj(["bookmark", "track", "--remote=origin", "glob:*-tracked"])
177+
.success();
178+
work_dir
179+
.run_jj(["bookmark", "track", "--remote=upstream", "aaa-tracked"])
180+
.success();
177181
work_dir
178-
.run_jj(["git", "push", "--allow-new", "--bookmark", "glob:*-tracked"])
182+
.run_jj(["git", "push", "--remote=origin", "--tracked"])
183+
.success();
184+
work_dir
185+
.run_jj(["git", "push", "--remote=upstream", "--tracked"])
179186
.success();
180187

181188
origin_dir
@@ -190,8 +197,20 @@ fn test_bookmark_names() {
190197
origin_dir
191198
.run_jj(["desc", "-r", "bbb-untracked", "-m", "x"])
192199
.success();
193-
origin_dir.run_jj(["git", "export"]).success();
194-
work_dir.run_jj(["git", "fetch"]).success();
200+
work_dir.run_jj(["git", "fetch", "--all-remotes"]).success();
201+
202+
insta::assert_snapshot!(work_dir.run_jj(["bookmark", "list", "--all"]), @r"
203+
aaa-local: qpvuntsm fe38a82d (empty) x
204+
aaa-tracked: qpvuntsm fe38a82d (empty) x
205+
@origin: qpvuntsm fe38a82d (empty) x
206+
@upstream: qpvuntsm fe38a82d (empty) x
207+
aaa-untracked@origin: rlvkpnrz 434ae005 (empty) x
208+
bbb-local: qpvuntsm fe38a82d (empty) x
209+
bbb-tracked: qpvuntsm fe38a82d (empty) x
210+
@origin: qpvuntsm fe38a82d (empty) x
211+
bbb-untracked@origin: rlvkpnrz 434ae005 (empty) x
212+
[EOF]
213+
");
195214

196215
// Every shell hook is a little different, e.g. the zsh hooks add some
197216
// additional environment variables. But this is irrelevant for the purpose
@@ -264,14 +283,14 @@ fn test_bookmark_names() {
264283

265284
let output = work_dir.complete_fish(["bookmark", "track", "a"]);
266285
insta::assert_snapshot!(output, @r"
267-
aaa-local@origin x
268-
aaa-untracked@origin x
286+
aaa-local x
287+
aaa-untracked x
269288
[EOF]
270289
");
271290

272291
let output = work_dir.complete_fish(["bookmark", "untrack", "a"]);
273292
insta::assert_snapshot!(output, @r"
274-
aaa-tracked@origin x
293+
aaa-tracked x
275294
[EOF]
276295
");
277296

0 commit comments

Comments
 (0)