Debian Codegraph @

cli/src/commands/git/import.rs

raw
1 // Copyright 2020-2023 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 jj_lib::git;
16 use jj_lib::git::GitSettings;
17
18 use crate::cli_util::CommandHelper;
19 use crate::command_error::CommandError;
20 use crate::git_util::load_git_import_options;
21 use crate::git_util::print_git_import_stats;
22 use crate::ui::Ui;
23
24 /// Update repo with changes made in the underlying Git repo
25 ///
26 /// Commits that are no longer reachable from any branch in the Git repo will be
27 /// considered abandoned in the Git repo, and will be abandoned in the jj
28 /// repo to match the Git repo. Set `git.abandon-unreachable-commits` to `false`
29 /// to disable this behavior.
30 ///
31 /// If a working-copy commit gets abandoned, it will be given a new, empty
32 /// commit. This is true in general; it is not specific to this command.
33 ///
34 /// By default, this command does nothing in colocated workspaces because the
35 /// import happens automatically. Use `--ignore-working-copy` to forcibly import
36 /// changes.
37 #[derive(clap::Args, Clone, Debug)]
38 pub struct GitImportArgs {}
39
40 pub async fn cmd_git_import(
41 ui: &mut Ui,
42 command: &CommandHelper,
43 _args: &GitImportArgs,
44 ) -> Result<(), CommandError> {
45 let mut workspace_command = command.workspace_helper(ui).await?;
46 if command.is_working_copy_writable() && workspace_command.working_copy_shared_with_git() {
47 // Git refs are imported during the snapshot.
48 writeln!(ui.status(), "No import needed in colocated workspaces.")?;
49 return Ok(());
50 }
51
52 let git_settings = GitSettings::from_settings(workspace_command.settings())?;
53 let remote_settings = workspace_command.settings().remote_settings()?;
54 let import_options = load_git_import_options(ui, &git_settings, &remote_settings)?;
55 let mut tx = workspace_command.start_transaction();
56 let stats = git::import_refs(tx.repo_mut(), &import_options).await?;
57 print_git_import_stats(ui, &tx, &stats)?;
58 tx.finish(ui, "import git refs").await?;
59 Ok(())
60 }