| 1 |
// Copyright 2026 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 clap_complete::ArgValueCandidates; |
| 16 |
use itertools::Itertools as _; |
| 17 |
use jj_lib::repo::Repo as _; |
| 18 |
use jj_lib::str_util::StringExpression; |
| 19 |
|
| 20 |
use super::resolve_trackable_remote_tags; |
| 21 |
use super::trackable_remote_tags_matching; |
| 22 |
use super::warn_unmatched_local_or_remote_tags; |
| 23 |
use super::warn_unmatched_remotes; |
| 24 |
use crate::cli_util::CommandHelper; |
| 25 |
use crate::cli_util::default_ignored_remote_name; |
| 26 |
use crate::command_error::CommandError; |
| 27 |
use crate::command_error::cli_error; |
| 28 |
use crate::complete; |
| 29 |
use crate::revset_util::parse_name_patterns_or_remote_symbols; |
| 30 |
use crate::revset_util::parse_union_name_patterns; |
| 31 |
use crate::ui::Ui; |
| 32 |
|
| 33 |
/// Start tracking given remote tags |
| 34 |
/// |
| 35 |
/// A tracked remote tag will be imported as a local tag of the same name. |
| 36 |
/// Changes to it will propagate to the existing local tag on future pulls. |
| 37 |
#[derive(clap::Args, Clone, Debug)] |
| 38 |
pub struct TagTrackArgs { |
| 39 |
/// Tag name patterns or remote tag symbols to track |
| 40 |
/// |
| 41 |
/// `TAG` matches tag names using glob syntax by default. You can also use |
| 42 |
/// other [string pattern syntax]. |
| 43 |
/// |
| 44 |
/// `TAG@REMOTE` resolves to a remote tag exactly. |
| 45 |
/// |
| 46 |
/// [string pattern syntax]: |
| 47 |
/// https://docs.jj-vcs.dev/latest/revsets/#string-patterns |
| 48 |
#[arg(required = true, value_name = "TAG[@REMOTE]")] |
| 49 |
names: Vec<String>, |
| 50 |
|
| 51 |
/// Remote names to track |
| 52 |
/// |
| 53 |
/// By default, the specified pattern matches remote names with glob syntax. |
| 54 |
/// You can also use other [string pattern syntax]. |
| 55 |
/// |
| 56 |
/// If no remote names are given, all remote tags matching the tag names |
| 57 |
/// will be tracked. |
| 58 |
/// |
| 59 |
/// [string pattern syntax]: |
| 60 |
/// https://docs.jj-vcs.dev/latest/revsets/#string-patterns |
| 61 |
#[arg(long = "remote", value_name = "REMOTE")] |
| 62 |
// TODO: Make this skip the remotes already tracked |
| 63 |
#[arg(add = ArgValueCandidates::new(complete::git_remotes))] |
| 64 |
remotes: Option<Vec<String>>, |
| 65 |
} |
| 66 |
|
| 67 |
pub async fn cmd_tag_track( |
| 68 |
ui: &mut Ui, |
| 69 |
command: &CommandHelper, |
| 70 |
args: &TagTrackArgs, |
| 71 |
) -> Result<(), CommandError> { |
| 72 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 73 |
let repo = workspace_command.repo().clone(); |
| 74 |
let view = repo.view(); |
| 75 |
|
| 76 |
let (tag_exprs, remote_symbols) = parse_name_patterns_or_remote_symbols(ui, &args.names)?; |
| 77 |
// Reject mixed syntax. It is confusing if the default @<remote> or |
| 78 |
// user-specified --remote flag applies only to <tag> patterns. |
| 79 |
if !tag_exprs.is_empty() && !remote_symbols.is_empty() { |
| 80 |
return Err(cli_error( |
| 81 |
"Cannot specify both <tag> patterns and <tag>@<remote> symbols", |
| 82 |
)); |
| 83 |
} else if args.remotes.is_some() && !remote_symbols.is_empty() { |
| 84 |
return Err(cli_error( |
| 85 |
"--remote cannot be used with <tag>@<remote> symbols", |
| 86 |
)); |
| 87 |
} |
| 88 |
let matched_refs = if !remote_symbols.is_empty() { |
| 89 |
resolve_trackable_remote_tags(ui, view, &remote_symbols)? |
| 90 |
} else { |
| 91 |
let ignored_remote = default_ignored_remote_name(repo.store()) |
| 92 |
// suppress unmatched remotes warning for default-ignored remote |
| 93 |
.filter(|name| view.get_remote_view(name).is_some()); |
| 94 |
let tag_expr = StringExpression::union_all(tag_exprs); |
| 95 |
let remote_expr = match (&args.remotes, ignored_remote) { |
| 96 |
(Some(text), _) => parse_union_name_patterns(ui, text)?, |
| 97 |
(None, Some(ignored)) => StringExpression::exact(ignored).negated(), |
| 98 |
(None, None) => StringExpression::all(), |
| 99 |
}; |
| 100 |
let tag_matcher = tag_expr.to_matcher(); |
| 101 |
let remote_matcher = remote_expr.to_matcher(); |
| 102 |
let matched_refs = |
| 103 |
trackable_remote_tags_matching(view, &tag_matcher, &remote_matcher).collect(); |
| 104 |
warn_unmatched_local_or_remote_tags(ui, view, &tag_expr)?; |
| 105 |
warn_unmatched_remotes(ui, view, &remote_expr)?; |
| 106 |
matched_refs |
| 107 |
}; |
| 108 |
|
| 109 |
let mut symbols = Vec::new(); |
| 110 |
for (symbol, remote_ref) in matched_refs { |
| 111 |
if remote_ref.is_tracked() { |
| 112 |
writeln!(ui.warning_default(), "Remote tag already tracked: {symbol}")?; |
| 113 |
} else { |
| 114 |
symbols.push(symbol); |
| 115 |
} |
| 116 |
} |
| 117 |
let mut tx = workspace_command.start_transaction(); |
| 118 |
for &symbol in &symbols { |
| 119 |
tx.repo_mut().track_remote_tag(symbol).await?; |
| 120 |
} |
| 121 |
if !symbols.is_empty() { |
| 122 |
writeln!( |
| 123 |
ui.status(), |
| 124 |
"Started tracking {} remote tags.", |
| 125 |
symbols.len() |
| 126 |
)?; |
| 127 |
} |
| 128 |
tx.finish( |
| 129 |
ui, |
| 130 |
format!("track remote tag {}", symbols.iter().join(", ")), |
| 131 |
) |
| 132 |
.await?; |
| 133 |
Ok(()) |
| 134 |
} |