Debian Codegraph @

cli/src/commands/git/remote/list.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 std::io::Write as _;
16
17 use bstr::BString;
18 use gix::Remote;
19 use jj_lib::git;
20 use jj_lib::ref_name::RemoteName;
21 use jj_lib::repo::Repo as _;
22
23 use crate::cli_util::CommandHelper;
24 use crate::command_error::CommandError;
25 use crate::ui::Ui;
26
27 /// List Git remotes
28 #[derive(clap::Args, Clone, Debug)]
29 pub struct GitRemoteListArgs {}
30
31 pub async fn cmd_git_remote_list(
32 ui: &mut Ui,
33 command: &CommandHelper,
34 _args: &GitRemoteListArgs,
35 ) -> Result<(), CommandError> {
36 let workspace_command = command.workspace_helper(ui).await?;
37 let git_repo = git::get_git_repo(workspace_command.repo().store())?;
38 for remote_name in git_repo.remote_names() {
39 let Ok(remote_name) = str::from_utf8(&remote_name).map(RemoteName::new) else {
40 continue; // ignore non-UTF-8 remote names which we don't support
41 };
42 let Some(remote) = git::try_find_active_remote(&git_repo, remote_name)? else {
43 continue; // ignore empty [remote "<name>"] section
44 };
45 let fetch_url = get_url(&remote, gix::remote::Direction::Fetch);
46 let push_url = get_url(&remote, gix::remote::Direction::Push);
47 if fetch_url == push_url {
48 writeln!(
49 ui.stdout(),
50 "{remote_name} {fetch_url}",
51 remote_name = remote_name.as_symbol()
52 )?;
53 } else {
54 writeln!(
55 ui.stdout(),
56 "{remote_name} {fetch_url} (push: {push_url})",
57 remote_name = remote_name.as_symbol()
58 )?;
59 }
60 }
61 Ok(())
62 }
63
64 fn get_url(remote: &Remote, direction: gix::remote::Direction) -> BString {
65 remote
66 .url(direction)
67 .map(|url| url.to_bstring())
68 .unwrap_or_else(|| "<no URL>".into())
69 }