-
Notifications
You must be signed in to change notification settings - Fork 962
Expand file tree
/
Copy pathclone.rs
More file actions
444 lines (412 loc) · 16.2 KB
/
clone.rs
File metadata and controls
444 lines (412 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright 2020-2023 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::fs;
use std::io;
use std::io::Write as _;
use std::num::NonZeroU32;
use std::path::Path;
use itertools::Itertools as _;
use jj_lib::file_util;
use jj_lib::git;
use jj_lib::git::FetchTagsOverride;
use jj_lib::git::GitFetch;
use jj_lib::git::GitFetchRefExpression;
use jj_lib::git::GitSettings;
use jj_lib::git::expand_fetch_refspecs;
use jj_lib::ref_name::RefName;
use jj_lib::ref_name::RefNameBuf;
use jj_lib::ref_name::RemoteName;
use jj_lib::ref_name::RemoteNameBuf;
use jj_lib::repo::Repo as _;
use jj_lib::str_util::StringExpression;
use jj_lib::workspace::Workspace;
use super::write_repository_level_trunk_alias;
use crate::cli_util::CommandHelper;
use crate::cli_util::WorkspaceCommandHelper;
use crate::command_error::CommandError;
use crate::command_error::cli_error;
use crate::command_error::user_error;
use crate::command_error::user_error_with_message;
use crate::commands::git::FetchTagsMode;
use crate::commands::git::maybe_add_gitignore;
use crate::config::ConfigEnv;
use crate::git_util::GitSubprocessUi;
use crate::git_util::absolute_git_url;
use crate::git_util::load_git_import_options;
use crate::git_util::print_git_import_stats;
use crate::revset_util::parse_union_name_patterns;
use crate::ui::Ui;
/// Create a new repo backed by a clone of a Git repo
#[derive(clap::Args, Clone, Debug)]
pub struct GitCloneArgs {
/// URL or path of the Git repo to clone
///
/// Local path will be resolved to absolute form.
#[arg(value_hint = clap::ValueHint::Url)]
source: String,
/// Specifies the target directory for the Jujutsu repository clone.
/// If not provided, defaults to a directory named after the last component
/// of the source URL. The full directory path will be created if it
/// doesn't exist.
#[arg(value_hint = clap::ValueHint::DirPath)]
destination: Option<String>,
/// Name of the newly created remote
#[arg(long = "remote", default_value = "origin")]
remote_name: RemoteNameBuf,
/// Colocate the Jujutsu repo with the git repo
///
/// Specifies that the `jj` repo should also be a valid `git` repo, allowing
/// the use of both `jj` and `git` commands in the same directory.
///
/// The repository will contain a `.git` dir in the top-level. Regular Git
/// tools will be able to operate on the repo.
///
/// **This is the default**, and this option has no effect, unless the
/// [git.colocate config] is set to `false`.
///
/// [git.colocate config]:
/// https://docs.jj-vcs.dev/latest/config/#default-colocation
#[arg(long)]
colocate: bool,
/// Disable colocation of the Jujutsu repo with the git repo
///
/// Prevent Git tools that are unaware of `jj` and regular Git commands from
/// operating on the repo. The Git repository that stores most of the repo
/// data will be hidden inside a sub-directory of the `.jj` directory.
///
/// See [colocation docs] for some minor advantages of non-colocated
/// workspaces.
///
/// [colocation docs]:
/// https://docs.jj-vcs.dev/latest/git-compatibility/#colocated-jujutsugit-repos
#[arg(long, conflicts_with = "colocate")]
no_colocate: bool,
/// Create a shallow clone of the given depth
#[arg(long)]
depth: Option<NonZeroU32>,
/// Configure when to fetch tags
///
/// Unless otherwise specified, the initial clone will fetch all tags,
/// while all subsequent fetches will only fetch included tags.
#[arg(long, value_enum)]
fetch_tags: Option<FetchTagsMode>,
/// Name of the branch to fetch and use as the parent of the working-copy
/// change (can be repeated)
///
/// If not present, all branches are fetched and the repository's default
/// branch is used as parent of the working-copy change.
///
/// By default, the specified pattern matches branch names with glob syntax,
/// but only `*` is expanded. Other wildcard characters such as `?` are
/// *not* supported. Patterns can be repeated or combined with [logical
/// operators] to specify multiple branches, but only union and negative
/// intersection are supported. If there are multiple matching branches, the
/// first exact branch name is used as the working-copy parent.
///
/// Examples: `push-*`, `(push-* | foo/*) ~ foo/unwanted`
///
/// [logical operators]:
/// https://docs.jj-vcs.dev/latest/revsets/#string-patterns
#[arg(long = "branch", short, alias = "bookmark", value_name = "BRANCH")]
branches: Option<Vec<String>>,
// TODO: add --tag option and save it in jj's repo config? (#7819)
}
fn clone_destination_for_source(source: &str) -> Option<&str> {
let destination = source.strip_suffix(".git").unwrap_or(source);
let destination = destination.strip_suffix('/').unwrap_or(destination);
destination
.rsplit_once(&['/', '\\', ':'][..])
.map(|(_, name)| name)
}
pub async fn cmd_git_clone(
ui: &mut Ui,
command: &CommandHelper,
args: &GitCloneArgs,
) -> Result<(), CommandError> {
let remote_name = &args.remote_name;
if command.global_args().at_operation.is_some() {
return Err(cli_error("--at-op is not respected"));
}
let source = absolute_git_url(command.cwd(), &args.source)?;
let wc_path_str = args
.destination
.as_deref()
.or_else(|| clone_destination_for_source(&source))
.ok_or_else(|| user_error("No destination specified and wasn't able to guess it"))?;
let wc_path = command.cwd().join(wc_path_str);
let wc_path_existed = wc_path.exists();
if wc_path_existed && !file_util::is_empty_dir(&wc_path)? {
return Err(user_error(
"Destination path exists and is not an empty directory",
));
}
// will create a tree dir in case if was deleted after last check
fs::create_dir_all(&wc_path)
.map_err(|err| user_error_with_message(format!("Failed to create {wc_path_str}"), err))?;
let colocate = if command.settings().get_bool("git.colocate")? {
!args.no_colocate
} else {
args.colocate
};
let ref_expr = {
let bookmark = match &args.branches {
Some(texts) => parse_union_name_patterns(ui, texts)?,
None => StringExpression::all(),
};
GitFetchRefExpression {
bookmark,
// TODO: disable implicit fetching and set this to "all" (#7528)
tag: StringExpression::none(),
}
};
// Canonicalize because fs::remove_dir_all() doesn't seem to like e.g.
// `/some/path/.`
let canonical_wc_path = dunce::canonicalize(&wc_path)
.map_err(|err| user_error_with_message(format!("Failed to create {wc_path_str}"), err))?;
let clone_result: Result<_, CommandError> = async {
let (workspace_command, config_env) =
init_workspace(ui, command, &canonical_wc_path, colocate).await?;
let mut workspace_command = configure_remote(
ui,
command,
workspace_command,
remote_name,
&source,
// If not explicitly specified on the CLI, configure the remote for only fetching
// included tags for future fetches.
args.fetch_tags.unwrap_or(FetchTagsMode::Included),
&ref_expr,
)
.await?;
let default_branch = fetch_new_remote(
ui,
&mut workspace_command,
remote_name,
// If we add default fetch patterns to jj's config, these patterns
// will be loaded here?
&ref_expr,
args.depth,
args.fetch_tags,
)
.await?;
Ok((workspace_command, default_branch, config_env))
}
.await;
if clone_result.is_err() {
let clean_up_dirs = || -> io::Result<()> {
let sub_dirs = [Some(".jj"), colocate.then_some(".git")];
for &name in sub_dirs.iter().flatten() {
let dir = canonical_wc_path.join(name);
fs::remove_dir_all(&dir).or_else(|err| match err.kind() {
io::ErrorKind::NotFound => Ok(()),
_ => Err(err),
})?;
}
if !wc_path_existed {
fs::remove_dir(&canonical_wc_path)?;
}
Ok(())
};
if let Err(err) = clean_up_dirs() {
writeln!(
ui.warning_default(),
"Failed to clean up {}: {}",
canonical_wc_path.display(),
err
)
.ok();
}
}
let (mut workspace_command, (working_branch, working_is_default), config_env) = clone_result?;
if let Some(name) = &working_branch {
let working_symbol = name.to_remote_symbol(remote_name);
if working_is_default {
write_repository_level_trunk_alias(ui, &config_env, working_symbol)?;
}
let working_branch_remote_ref = workspace_command
.repo()
.view()
.get_remote_bookmark(working_symbol);
if let Some(commit_id) = working_branch_remote_ref.target.as_normal().cloned() {
let mut tx = workspace_command.start_transaction();
if let Ok(commit) = tx.repo().store().get_commit(&commit_id) {
tx.check_out(&commit)?;
}
tx.finish(
ui,
format!("check out git remote's branch: {}", name.as_symbol()),
)
.await?;
}
}
if colocate {
writeln!(
ui.hint_default(),
r"Running `git clean -xdf` will remove `.jj/`!",
)?;
}
Ok(())
}
async fn init_workspace(
ui: &Ui,
command: &CommandHelper,
wc_path: &Path,
colocate: bool,
) -> Result<(WorkspaceCommandHelper, ConfigEnv), CommandError> {
let (settings, config_env) = command.settings_for_new_workspace(ui, wc_path)?;
let (workspace, repo) = if colocate {
Workspace::init_colocated_git(&settings, wc_path).await?
} else {
Workspace::init_internal_git(&settings, wc_path).await?
};
let workspace_command = command.for_workable_repo(ui, workspace, repo)?;
maybe_add_gitignore(&workspace_command)?;
Ok((workspace_command, config_env))
}
async fn configure_remote(
ui: &Ui,
command: &CommandHelper,
mut workspace_command: WorkspaceCommandHelper,
remote_name: &RemoteName,
source: &str,
fetch_tags: FetchTagsMode,
ref_expr: &GitFetchRefExpression,
) -> Result<WorkspaceCommandHelper, CommandError> {
let mut tx = workspace_command.start_transaction();
git::add_remote(
tx.repo_mut(),
remote_name,
source,
None,
fetch_tags.as_fetch_tags(),
&ref_expr.bookmark,
)?;
tx.finish(ui, format!("add git remote {}", remote_name.as_symbol()))
.await?;
// Reload workspace to apply new remote configuration to
// gix::ThreadSafeRepository behind the store.
let workspace = command.load_workspace_at(
workspace_command.workspace_root(),
workspace_command.settings(),
)?;
let op = workspace
.repo_loader()
.load_operation(workspace_command.repo().op_id())
.await?;
let repo = workspace.repo_loader().load_at(&op).await?;
command.for_workable_repo(ui, workspace, repo)
}
async fn fetch_new_remote(
ui: &Ui,
workspace_command: &mut WorkspaceCommandHelper,
remote_name: &RemoteName,
ref_expr: &GitFetchRefExpression,
depth: Option<NonZeroU32>,
fetch_tags: Option<FetchTagsMode>,
) -> Result<(Option<RefNameBuf>, bool), CommandError> {
writeln!(
ui.status(),
r#"Fetching into new repo in "{}""#,
workspace_command.workspace_root().display()
)?;
let settings = workspace_command.settings();
let git_settings = GitSettings::from_settings(settings)?;
let remote_settings = settings.remote_settings()?;
let subprocess_options = git_settings.to_subprocess_options();
let import_options = load_git_import_options(ui, &git_settings, &remote_settings)?;
let should_track_default = settings.get_bool("git.track-default-bookmark-on-clone")?;
let mut tx = workspace_command.start_transaction();
let (default_branch, import_stats) = {
let mut git_fetch = GitFetch::new(tx.repo_mut(), subprocess_options, &import_options)?;
let fetch_refspecs = expand_fetch_refspecs(remote_name, ref_expr.clone())?;
git_fetch.fetch(
remote_name,
fetch_refspecs,
&mut GitSubprocessUi::new(ui),
depth,
match fetch_tags {
// If not explicitly specified on the CLI, override the remote
// configuration and fetch all tags by default since this is
// the Git default behavior.
None => Some(FetchTagsOverride::AllTags),
// Technically by this point the remote should already be
// configured based on the CLI parameters so we shouldn't *need*
// to apply an override here but all the cases are expanded here
// for clarity.
Some(FetchTagsMode::All) => Some(FetchTagsOverride::AllTags),
Some(FetchTagsMode::None) => Some(FetchTagsOverride::NoTags),
Some(FetchTagsMode::Included) => None,
},
)?;
let import_stats = git_fetch.import_refs().await?;
let default_branch = git_fetch.get_default_branch(remote_name)?;
(default_branch, import_stats)
};
// Warn unmatched exact patterns, and record the first matching branch as
// the working branch. If there are no matching exact patterns, use the
// default branch of the remote.
let mut missing_branches = vec![];
let mut working_branch = None;
let bookmark_matcher = ref_expr.bookmark.to_matcher();
let exact_bookmarks = ref_expr
.bookmark
.exact_strings()
.filter(|name| bookmark_matcher.is_match(name)) // exclude negative patterns
.map(RefName::new);
for name in exact_bookmarks {
let symbol = name.to_remote_symbol(remote_name);
if tx.repo().view().get_remote_bookmark(symbol).is_absent() {
missing_branches.push(name);
} else if working_branch.is_none() {
working_branch = Some(name);
}
}
if working_branch.is_none() {
working_branch = default_branch.as_deref().filter(|name| {
let symbol = name.to_remote_symbol(remote_name);
tx.repo().view().get_remote_bookmark(symbol).is_present()
});
}
if !missing_branches.is_empty() {
writeln!(
ui.warning_default(),
"No matching branches found on remote: {}",
missing_branches
.iter()
.map(|name| name.as_symbol())
.join(", ")
)?;
}
// TODO: warn missing tags if we add --tag=pattern
let working_is_default = working_branch == default_branch.as_deref();
if let Some(name) = working_branch
&& working_is_default
&& should_track_default
{
// For convenience, create local bookmark as Git would do.
let remote_symbol = name.to_remote_symbol(remote_name);
tx.repo_mut().track_remote_bookmark(remote_symbol)?;
}
print_git_import_stats(ui, &tx, &import_stats)?;
if git_settings.auto_local_bookmark && !should_track_default {
writeln!(
ui.hint_default(),
"`git.track-default-bookmark-on-clone=false` has no effect if \
`git.auto-local-bookmark` is enabled."
)?;
}
tx.finish(ui, "fetch from git remote into empty repo")
.await?;
Ok((working_branch.map(ToOwned::to_owned), working_is_default))
}