Skip to content

Commit 027ff8a

Browse files
committed
Add CLI flag for setting a new default remote name.
1 parent 7a8a3b7 commit 027ff8a

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

cli-flags.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ args:
1313
- CLEAR_PROJECT_ID
1414
- NEW_DOMAIN_KEY
1515
- CLEAR_DOMAIN_KEY
16+
- NEW_DEFAULT_REMOTE
1617
- GENERATE_COMPLETIONS
1718
- NEW_PROJECT_ID:
1819
long: set-project-id
@@ -24,6 +25,7 @@ args:
2425
- CLEAR_PROJECT_ID
2526
- NEW_DOMAIN_KEY
2627
- CLEAR_DOMAIN_KEY
28+
- NEW_DEFAULT_REMOTE
2729
- GENERATE_COMPLETIONS
2830
- CLEAR_PROJECT_ID:
2931
long: clear-project-id
@@ -33,6 +35,7 @@ args:
3335
conflicts_with:
3436
- NEW_DOMAIN_KEY
3537
- CLEAR_DOMAIN_KEY
38+
- NEW_DEFAULT_REMOTE
3639
- GENERATE_COMPLETIONS
3740
- NEW_DOMAIN_KEY:
3841
long: set-domain-key
@@ -42,12 +45,21 @@ args:
4245
required: false
4346
conflicts_with:
4447
- CLEAR_DOMAIN_KEY
48+
- NEW_DEFAULT_REMOTE
4549
- GENERATE_COMPLETIONS
4650
- CLEAR_DOMAIN_KEY:
4751
long: clear-domain-key
4852
help: Clear the API key for the current repository's domain
4953
takes_value: false
5054
required: false
55+
conflicts_with:
56+
- NEW_DEFAULT_REMOTE
57+
- GENERATE_COMPLETIONS
58+
- NEW_DEFAULT_REMOTE:
59+
long: set-default-remote
60+
help: Set the name of the default remote for the repository
61+
takes_value: true
62+
required: false
5163
conflicts_with:
5264
- GENERATE_COMPLETIONS
5365
- GENERATE_COMPLETIONS:
@@ -77,11 +89,13 @@ args:
7789
- NEW_DOMAIN_KEY
7890
- CLEAR_DOMAIN_KEY
7991
- LIST_MR
92+
- NEW_DEFAULT_REMOTE
8093
- GENERATE_COMPLETIONS
8194
conflicts_with:
8295
- NEW_PROJECT_ID
8396
- CLEAR_PROJECT_ID
8497
- NEW_DOMAIN_KEY
8598
- CLEAR_DOMAIN_KEY
8699
- LIST_MR
100+
- NEW_DEFAULT_REMOTE
87101
- GENERATE_COMPLETIONS

src/git.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ fn slugify_domain(domain: &str) -> String {
2828
pub fn get_remotes() -> HashSet<String> {
2929
let repo = Repository::open_from_env().expect("Couldn't find repository");
3030
match repo.remotes() {
31-
Ok(remotes) => remotes.into_iter().filter_map(|rem| rem).map(|rem| String::from(rem)).collect(),
32-
Err(_) => HashSet::new()
31+
Ok(remotes) => remotes
32+
.into_iter()
33+
.filter_map(|rem| rem)
34+
.map(String::from)
35+
.collect(),
36+
Err(_) => HashSet::new(),
3337
}
3438
}
3539

@@ -89,8 +93,7 @@ pub fn get_project_config(field_name: &str) -> Option<String> {
8993
pub fn set_project_config(field_name: &str, value: &str) {
9094
let repo = Repository::open_from_env().expect("Couldn't find repository");
9195
let mut cfg = repo.config().unwrap();
92-
cfg.set_str(&format!("req.{}", field_name), value)
93-
.unwrap();
96+
cfg.set_str(&format!("req.{}", field_name), value).unwrap();
9497
}
9598

9699
/// Get a value for the given global git-req config
@@ -161,10 +164,7 @@ pub fn checkout_branch(
161164
}
162165
}
163166
None => {
164-
warn!(
165-
"No default remote found. Using {}",
166-
remote_name
167-
);
167+
warn!("No default remote found. Using {}", remote_name);
168168
format!("{}/{}", remote_name, local_branch_name)
169169
}
170170
};

src/main.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ fn set_project_id(remote_name: &str, new_id: &str) {
128128
eprintln!("{}", "New project ID set!".green());
129129
}
130130

131+
/// Set the default remote for the repository
132+
fn set_default_remote(remote_name: &str) {
133+
trace!("Setting default remote {}", remote_name);
134+
git::set_project_config("defaultremote", remote_name);
135+
eprintln!("{}", "New default remote set!".green());
136+
}
137+
131138
/// Print the open requests
132139
fn list_open_requests(remote_name: &str) {
133140
info!("Getting open requests");
@@ -178,7 +185,9 @@ fn get_remote_name(matches: &ArgMatches) -> String {
178185
}
179186
print!("Remote name: ");
180187
let _ = stdout().flush();
181-
stdin().read_line(&mut new_remote_name).expect("Did not input a name");
188+
stdin()
189+
.read_line(&mut new_remote_name)
190+
.expect("Did not input a name");
182191
trace!("New remote: {}", &new_remote_name);
183192
if !git::get_remotes().contains(new_remote_name.trim()) {
184193
panic!("Invalid remote name provided")
@@ -191,7 +200,11 @@ fn get_remote_name(matches: &ArgMatches) -> String {
191200
}
192201
};
193202
// Not using Clap's default_value because of https://github.com/clap-rs/clap/issues/1140
194-
String::from(matches.value_of("REMOTE_NAME").unwrap_or(&default_remote_name))
203+
String::from(
204+
matches
205+
.value_of("REMOTE_NAME")
206+
.unwrap_or(&default_remote_name),
207+
)
195208
}
196209

197210
fn build_cli(cfg: &yaml_rust::Yaml) -> App {
@@ -223,6 +236,8 @@ fn main() {
223236
clear_domain_key(&get_remote_name(&matches));
224237
} else if let Some(domain_key) = matches.value_of("NEW_DOMAIN_KEY") {
225238
set_domain_key(&get_remote_name(&matches), domain_key);
239+
} else if let Some(remote_name) = matches.value_of("NEW_DEFAULT_REMOTE") {
240+
set_default_remote(remote_name);
226241
} else if let Some(shell_name) = matches.value_of("GENERATE_COMPLETIONS") {
227242
let mut app = build_cli(&cfg);
228243
generate_completion(&mut app, &shell_name);

0 commit comments

Comments
 (0)