add-remote 3.0.1

An interactive CLI tool to add a remote fork to a local Git repository.
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
use super::input_getter::{get_bool, get_string, get_uint};
use colour::{cyan_ln, dark_cyan_ln, green_ln, prnt_ln, red_ln, yellow, yellow_ln};
use reqwest::{
    self,
    blocking::Client,
    header::{HeaderValue, AUTHORIZATION, LINK, USER_AGENT},
};
use serde_json::{self, Value};
use std::{
    collections::HashMap,
    io::{self, Stdin},
    path::PathBuf,
    process::{self, Command},
};

/// Base URL for sending GET requests to GitLab for retrieving info about repositories.
const GITLAB_API: &str = "https://gitlab.com/api/v4/projects/";
/// Base URL for sending GET requests to GitHub for retrieving info about repositories.
const GITHUB_API: &str = "https://api.github.com/repos/";

/// The Gitlab/GitHub username of the owner of a repository or fork.
#[derive(Clone, Default, PartialEq, Eq, Hash, Debug)]
struct Owner(pub String);

/// The Gitlab/GitHub name of a repository or fork.
#[derive(Clone, Default, Debug)]
struct Name(pub String);

/// The name given to a local remote.
#[derive(Default, Debug)]
struct RemoteAlias(pub String);

/// The URL of a repository of fork.
#[derive(Clone, Debug)]
#[allow(clippy::enum_variant_names)]
enum Url {
    GitLabHttps(String),
    GitLabSsh(String),
    GitHubHttps(String),
    GitHubSsh(String),
}

impl Url {
    fn new(url: &str) -> Option<(Self, Owner, Name)> {
        let is_https = url.starts_with("https://");
        if !is_https && !url.starts_with("git@git") {
            return None;
        }
        let mut owner_and_repo = url.trim_start_matches("git@gitlab.com:");
        owner_and_repo = owner_and_repo.trim_start_matches("https://gitlab.com/");
        let checked_url = if owner_and_repo == url {
            owner_and_repo = url.trim_start_matches("git@github.com:");
            owner_and_repo = owner_and_repo.trim_start_matches("https://github.com/");
            if owner_and_repo == url {
                return None;
            }
            if is_https {
                Url::GitHubHttps(url.to_string())
            } else {
                Url::GitHubSsh(url.to_string())
            }
        } else if is_https {
            Url::GitLabHttps(url.to_string())
        } else {
            Url::GitLabSsh(url.to_string())
        };
        owner_and_repo = owner_and_repo.trim_end_matches(".git");
        let (owner, name) = Self::split_owner_and_repo(owner_and_repo);
        Some((checked_url, owner, name))
    }

    fn split_owner_and_repo(owner_and_repo: &str) -> (Owner, Name) {
        let mut split_itr = owner_and_repo.splitn(2, '/');
        (
            Owner(split_itr.next().unwrap().to_string()),
            Name(split_itr.next().unwrap().to_string()),
        )
    }

    fn change_to_https(&mut self) {
        match self.clone() {
            Url::GitLabHttps(_) | Url::GitHubHttps(_) => (),
            Url::GitLabSsh(url) => {
                *self = Url::GitLabHttps(format!(
                    "https://gitlab.com/{}",
                    url.trim_start_matches("git@gitlab.com:")
                        .trim_end_matches(".git")
                ));
            }
            Url::GitHubSsh(url) => {
                *self = Url::GitHubHttps(format!(
                    "https://github.com/{}",
                    url.trim_start_matches("git@github.com:")
                        .trim_end_matches(".git")
                ));
            }
        }
    }

    fn value(&self) -> &str {
        match self {
            Url::GitLabHttps(url)
            | Url::GitLabSsh(url)
            | Url::GitHubHttps(url)
            | Url::GitHubSsh(url) => &url,
        }
    }

    fn is_https(&self) -> bool {
        match self {
            Url::GitLabHttps(_) | Url::GitHubHttps(_) => true,
            Url::GitLabSsh(_) | Url::GitHubSsh(_) => false,
        }
    }

    fn is_git_lab(&self) -> bool {
        match self {
            Url::GitLabHttps(_) | Url::GitLabSsh(_) => true,
            Url::GitHubHttps(_) | Url::GitHubSsh(_) => false,
        }
    }
}

/// The main container for a repository's details.
#[derive(Debug)]
pub struct Repo {
    /// The GitLab Personal Access Token taken from git config.
    gitlab_token: Option<String>,
    /// The GitHub Personal Access Token taken from git config, base64-encoded.
    github_token: Option<String>,
    /// The collection of remotes for this repository.
    local_remotes: HashMap<Owner, (Name, RemoteAlias, Url)>,
    /// The collection of known forks (and the actual main "fork" a.k.a the source) which aren't
    /// already included in `local_remotes`.
    available_forks: Vec<(Owner, Url)>,
    /// The owner of the main fork/source.
    main_fork_owner: Owner,
    /// The name of the main fork/source.
    main_fork_name: Name,
    /// The URL of the main fork/source.
    main_fork_url: Url,
    /// The full path to the Git binary.
    git: PathBuf,
    /// Console's stdin stream.
    stdin: Stdin,
    /// The index of `available_forks` chosen by the user for addition as a remote.
    chosen_fork_index: usize,
    /// The name chosen by the user to use when adding the new remote.
    chosen_remote_alias: RemoteAlias,
}

impl Default for Repo {
    fn default() -> Self {
        let mut repo = Self::new_uninitialised();
        repo.gitlab_token = repo.get_from_gitconfig("add-remote.gitLabToken");
        repo.github_token = repo
            .get_from_gitconfig("add-remote.gitHubToken")
            .map(base64::encode);
        repo.populate_local_remotes();
        repo.populate_main_fork_details();
        repo.populate_available_forks();
        repo
    }
}

impl Repo {
    /// Whether there any further remotes which _can_ be added.
    pub fn has_no_available_forks(&self) -> bool {
        self.available_forks.is_empty()
    }

    /// Displays the collection of available forks.
    pub fn show_available_forks(&self) {
        prnt_ln!("Available forks:");
        let first_column_width = self.available_forks.len().to_string().len() + 2;
        for (index, &(ref owner, _)) in self.available_forks.iter().enumerate() {
            prnt_ln!("{:<width$}{}", index, owner.0, width = first_column_width);
        }
    }

    /// Runs `git remote -v` and returns the output.
    pub fn git_remote_verbose_output(&self) -> String {
        let output = Command::new(&self.git)
            .args(&["remote", "-v"])
            .output()
            .unwrap();
        assert!(output.status.success(), "Failed to run 'git remote -v'");
        let stdout = String::from_utf8_lossy(&output.stdout);
        stdout.trim().to_string()
    }

    /// Ask the user to chooses which available fork to add as a new remote.
    pub fn choose_fork(&mut self) {
        let default = self.suggest_fork();
        loop {
            if let Some(value) = default {
                yellow!("Choose fork (enter index number) [{}]: ", value);
            } else {
                yellow!("Choose fork (enter index number): ");
            }
            #[allow(clippy::cast_possible_truncation)]
            match get_uint(&mut self.stdin.lock(), default) {
                Err(error) => {
                    red_ln!("{}", error);
                }
                Ok(value) if value < self.available_forks.len() as u64 => {
                    self.chosen_fork_index = value as usize;
                    return;
                }
                Ok(_) => {
                    red_ln!("Must be one of the listed indices.");
                }
            }
        }
    }

    /// Ask the user to choose the name for the new remote.
    pub fn choose_local_remote_alias(&mut self) -> bool {
        let default = self.suggest_alias();
        loop {
            yellow!("Choose name to assign to remote [{}]: ", default);
            match get_string(&mut self.stdin.lock()) {
                Err(error) => {
                    red_ln!("{}", error);
                }
                Ok(value) => {
                    if value.is_empty() {
                        self.chosen_remote_alias = RemoteAlias(default);
                        return false;
                    } else {
                        self.chosen_remote_alias = RemoteAlias(value);
                        return true;
                    }
                }
            }
        }
    }

    /// Ask the user whether to add the alias to the global git-config and if so, then try to add
    /// it.
    pub fn offer_to_set_alias(&self) {
        let fork_name = &(self.available_forks[self.chosen_fork_index].0).0;
        let alias = &self.chosen_remote_alias.0;
        loop {
            yellow!(
                "Do you want to set this alias '{}' -> '{}' in your global git-config? [Y/n]: ",
                fork_name,
                alias
            );
            match get_bool(&mut self.stdin.lock(), Some(true)) {
                Err(error) => {
                    red_ln!("{}", error);
                }
                Ok(false) => return,
                Ok(true) => {
                    let git_config_arg = format!("add-remote.forkAlias.{}", fork_name);
                    let output = Command::new(&self.git)
                        .args(&[
                            "config",
                            "--global",
                            "--replace-all",
                            &git_config_arg,
                            alias,
                        ])
                        .output()
                        .unwrap();
                    if output.status.success() {
                        green_ln!(
                            "Alias '{}' -> '{}' successfully set in your global git-config",
                            fork_name,
                            alias
                        );
                    } else {
                        red_ln!(
                            "Failed to run 'git config --global --replace-all {} {}'",
                            git_config_arg,
                            alias
                        );
                    }
                    return;
                }
            }
        }
    }

    /// Process the user's choices, i.e. add the new remote.  Also calls `git fetch` for the new
    /// remote and displays the remotes when complete.
    pub fn set_remote(&self) {
        prnt_ln!("");
        let remotes_before = self.git_remote_verbose_output();

        // Add the remote.
        let chosen_url = self.get_chosen_url();
        let chosen_alias = &self.chosen_remote_alias.0;
        let mut command = Command::new(&self.git);
        let _ = command.args(&["remote", "add", chosen_alias, chosen_url.value()]);
        let output = command.output().unwrap();
        if !output.status.success() {
            red_ln!("Failed to run {:?}:", command);
            prnt_ln!("{}", String::from_utf8_lossy(&output.stdout));
            prnt_ln!("{}", String::from_utf8_lossy(&output.stderr));
            process::exit(-4);
        }

        // Disable pushing for the new remote.
        command = Command::new(&self.git);
        let _ = command.args(&["remote", "set-url", "--push", chosen_alias, "disable_push"]);
        let output = command.output().unwrap();
        assert!(output.status.success());

        // Fetch from the new remote.
        cyan_ln!("Fetching from {}\n", chosen_url.value());
        command = Command::new(&self.git);
        let _ = command.args(&["fetch", chosen_alias]);
        let output = command.output().unwrap();
        assert!(output.status.success());

        // Display the remotes, with the new one highlighted in green.
        let remotes_after = self.git_remote_verbose_output();
        let mut before_itr = remotes_before.lines();
        let mut line_before = before_itr.next();
        for line in remotes_after.lines() {
            if line_before.unwrap_or_default() == line {
                prnt_ln!("{}", line);
                line_before = before_itr.next();
            } else {
                dark_cyan_ln!("{}", line);
            }
        }

        let mut branches = self.git_branch_verbose_output(chosen_alias);
        if branches.is_empty() {
            branches = self.git_branch_verbose_output(&chosen_alias.to_lowercase());
        }
        prnt_ln!("\n{}", branches);
    }

    fn get_chosen_url(&self) -> Url {
        let mut chosen_url = self.available_forks[self.chosen_fork_index].1.clone();
        // If the chosen fork has an SSH URL, but all the locals are HTTPS URLs, change the chosen
        // one to HTTPS.
        if !chosen_url.is_https()
            && self
                .local_remotes
                .values()
                .all(|(_, _, url)| url.is_https())
        {
            chosen_url.change_to_https();
        }
        chosen_url
    }

    fn new_uninitialised() -> Self {
        let git = find_git::git_path().expect("Unable to find Git executable.");
        Self {
            gitlab_token: None,
            github_token: None,
            local_remotes: HashMap::new(),
            available_forks: Vec::new(),
            main_fork_owner: Owner::default(),
            main_fork_name: Name::default(),
            main_fork_url: Url::GitLabHttps(String::new()),
            git,
            stdin: io::stdin(),
            chosen_fork_index: 1 << 31,
            chosen_remote_alias: RemoteAlias::default(),
        }
    }

    /// Query GitHub's API and return the contents of the response along with an optional link to
    /// the next page if one exists.  Panics on failure.
    fn send_get(request: &str, authorisation: Option<&String>) -> (String, Option<String>) {
        let client = Client::new();
        let mut request_builder = client.get(request).header(
            USER_AGENT,
            format!("Add-Remote/{}", env!("CARGO_PKG_VERSION")),
        );
        if let Some(auth) = authorisation {
            request_builder = request_builder.header(AUTHORIZATION, format!("Basic {}", auth));
        }
        let response = request_builder.send().unwrap();
        if !response.status().is_success() {
            panic!(
                "\nFailed to GET {}\nResponse status: {}\nResponse headers:\n{:?}\nResponse \
                body:\n{:?}\n\nNote that Personal Access Tokens are required in some cases.\nFor \
                full details, see https://github.com/Fraser999/Add-Remote#personal-access-tokens.",
                request,
                response.status(),
                response.headers().clone(),
                response.text()
            );
        }
        let next_page_link = response
            .headers()
            .get(LINK)
            .and_then(Self::get_link_to_next_from_header);
        let content = response.text().unwrap();
        (content, next_page_link)
    }

    fn get_link_to_next_from_header(header_value: &HeaderValue) -> Option<String> {
        let search_str = "rel=\"next\"";
        let lhs_trim: &[_] = &[' ', '<'];
        let rhs_trim: &[_] = &[' ', '>', ';'];
        header_value.to_str().unwrap().split(',').find_map(|link| {
            if link.contains(search_str) {
                Some(
                    link.trim_start_matches(lhs_trim)
                        .trim_end_matches(search_str)
                        .trim_end_matches(rhs_trim)
                        .to_string(),
                )
            } else {
                None
            }
        })
    }

    /// Calls `git remote show` and `git remote get-url <name>` for each remote found to populate
    /// `local_remotes`.  If the initial Git command fails, we assume it's because this process is
    /// not being executed from within a Git repository, so we print an error message to that effect
    /// exit with a non-zero code.
    fn populate_local_remotes(&mut self) {
        let local_remotes_output = Command::new(&self.git)
            .args(&["remote", "show"])
            .output()
            .unwrap();
        // Get list of local remotes.
        if !local_remotes_output.status.success() {
            red_ln!(
                "Failed to execute 'git remote show'.  Execute this program from inside a Git \
                 repository."
            );
            process::exit(-1);
        }
        let stdout = String::from_utf8_lossy(&local_remotes_output.stdout);
        let local_remotes = stdout.trim().to_string();

        // For each, get the URL, and break this down to get the owner too.
        for remote_alias in local_remotes.lines() {
            let url_output = Command::new(&self.git)
                .args(&["remote", "get-url", remote_alias])
                .output()
                .unwrap();
            assert!(
                url_output.status.success(),
                "Failed to run 'git remote get-url {}'",
                remote_alias
            );
            let stdout = String::from_utf8_lossy(&url_output.stdout);
            if let Some((url, owner, name)) = Url::new(stdout.trim()) {
                let _ = self
                    .local_remotes
                    .insert(owner, (name, RemoteAlias(remote_alias.to_string()), url));
            }
        }
        if self.local_remotes.is_empty() {
            red_ln!(
                "This repository doesn't appear to be hosted on GitLab or GitHub.  'add-remote' \
                 can only be used with GitLab or GitHub projects."
            );
            process::exit(-2);
        }
    }

    /// Send GET to Gitlab/GitHub to allow retrieval of the main fork/source's details.
    fn populate_main_fork_details(&mut self) {
        let (owner, name, url) = self
            .local_remotes
            .iter()
            .map(|(owner, (name, _, url))| (owner.clone(), name.clone(), url.clone()))
            .next()
            .unwrap();
        if url.is_git_lab() {
            if self.gitlab_token.is_none() {
                red_ln!(
                    "This repository is hosted on GitLab.  To use 'add-remote' with a GitLab \
                     project, you must add a GitLab Personal Access Token with \"api\" scope \
                     to your git config under the key 'add-remote.gitLabToken'.  For full \
                     details, see \
                     https://github.com/Fraser999/Add-Remote#personal-access-tokens."
                );
                process::exit(-3);
            };
            self.main_fork_owner = owner;
            self.main_fork_name = name;
            while self.get_gitlab_parent() {}
        } else {
            let request = format!("{}{}/{}", GITHUB_API, owner.0, name.0);
            let response = Self::send_get(&request, self.github_token.as_ref()).0;
            let response_as_json: Value = serde_json::from_str(&response).unwrap();
            self.main_fork_owner = match response_as_json["source"]["owner"]["login"] {
                Value::Null => Owner(
                    response_as_json["owner"]["login"]
                        .as_str()
                        .unwrap()
                        .to_string(),
                ),
                Value::String(ref owner) => Owner(owner.trim_matches('"').to_string()),
                _ => unreachable!(),
            };
            self.main_fork_name = match response_as_json["source"]["name"] {
                Value::Null => Name(response_as_json["name"].as_str().unwrap().to_string()),
                Value::String(ref name) => Name(name.trim_matches('"').to_string()),
                _ => unreachable!(),
            };
            self.main_fork_url = match response_as_json["source"]["ssh_url"] {
                Value::Null => {
                    Url::GitHubSsh(response_as_json["ssh_url"].as_str().unwrap().to_string())
                }
                Value::String(ref url) => Url::GitHubHttps(url.trim_matches('"').to_string()),
                _ => unreachable!(),
            };
        }
    }

    /// If the GitLab repo defined by `self.main_fork_owner` and `self.main_fork_name` is a fork,
    /// these values are updated to those of the forked-from project and `true` is returned.
    /// Otherwise, if it's not a fork they are left unmodified, `self.main_fork_url` is set, and
    /// `false` is returned.
    fn get_gitlab_parent(&mut self) -> bool {
        let request = format!(
            "{}{}%2F{}?private_token={}",
            GITLAB_API,
            self.main_fork_owner.0,
            self.main_fork_name.0.replace("/", "%2F"),
            self.gitlab_token.as_ref().unwrap()
        );
        let response = Self::send_get(&request, None).0;
        let response_as_json: Value = serde_json::from_str(&response).unwrap();
        if let Value::Null = response_as_json["forked_from_project"] {
            self.main_fork_url = Url::GitLabSsh(
                response_as_json["ssh_url_to_repo"]
                    .as_str()
                    .unwrap()
                    .to_string(),
            );
            return false;
        }
        let (owner, name) = Url::split_owner_and_repo(
            response_as_json["forked_from_project"]["path_with_namespace"]
                .as_str()
                .unwrap(),
        );
        self.main_fork_owner = owner;
        self.main_fork_name = name;
        true
    }

    /// Send GET to Gitlab/GitHub to retrieve the list of forks and their details.
    fn populate_available_forks(&mut self) {
        let first_url = self
            .local_remotes
            .values()
            .map(|(_, _, url)| url.clone())
            .next()
            .unwrap();
        let (mut optional_request, authorisation) = if first_url.is_git_lab() {
            let request = Some(format!(
                "{}{}%2F{}/forks?per_page=200;private_token={}",
                GITLAB_API,
                self.main_fork_owner.0,
                self.main_fork_name.0.replace("/", "%2F"),
                self.gitlab_token.as_ref().unwrap()
            ));
            (request, None)
        } else {
            let request = Some(format!(
                "{}{}/{}/forks?per_page=100",
                GITHUB_API, self.main_fork_owner.0, self.main_fork_name.0
            ));
            let authorisation = self.github_token.as_ref();
            (request, authorisation)
        };

        while let Some(request) = optional_request {
            let (response, next_page_link) = Self::send_get(&request, authorisation);
            let response_as_json: Value = serde_json::from_str(&response).unwrap();
            if let Value::Array(values) = response_as_json {
                for value in &values {
                    let (owner, url) = if first_url.is_git_lab() {
                        let (owner, _) = Url::split_owner_and_repo(
                            value["path_with_namespace"].as_str().unwrap(),
                        );
                        let url = value["ssh_url_to_repo"].as_str().unwrap().to_string();
                        let subfork_count = value["forks_count"].as_u64().unwrap();
                        if owner != self.main_fork_owner && subfork_count > 0 {
                            yellow_ln!(
                                "{} which is a fork of {} has {} fork{} being ignored.",
                                url,
                                self.main_fork_url.value(),
                                subfork_count,
                                if subfork_count > 1 { "s" } else { "" },
                            );
                        }
                        (owner, Url::GitLabSsh(url))
                    } else {
                        let owner = value["owner"]["login"].as_str().unwrap().to_string();
                        let url = value["ssh_url"].as_str().unwrap().to_string();
                        (Owner(owner), Url::GitHubSsh(url))
                    };
                    if !self.local_remotes.contains_key(&owner) {
                        self.available_forks.push((owner, url));
                    }
                }
            }
            optional_request = next_page_link;
        }
        // Add the main fork/source's details too if required.
        if !self.local_remotes.contains_key(&self.main_fork_owner) {
            self.available_forks
                .push((self.main_fork_owner.clone(), self.main_fork_url.clone()));
        }
        self.available_forks
            .sort_by_key(|&(ref owner, _)| owner.0.to_lowercase());
    }

    /// Suggests an index of `available_forks` to use as a default for the user's choice.  Favours
    /// the available one if there is only one available, then the main fork/source owner, then the
    /// Git config value of `add-remote.preferredFork` if it's set, otherwise returns `None`.
    fn suggest_fork(&self) -> Option<u64> {
        // Return 0 if there's only one available.
        if self.available_forks.len() == 1 {
            return Some(0);
        }
        // Choose the main fork/source owner if available.
        if let Ok(index) = self
            .available_forks
            .binary_search_by_key(&self.main_fork_owner.0.to_lowercase(), |&(ref owner, _)| {
                owner.0.to_lowercase()
            })
        {
            return Some(index as u64);
        }
        // Next look for `add-remote.preferredFork` in Git config.
        self.get_from_gitconfig("add-remote.preferredFork")
            .and_then(|preferred| {
                self.available_forks
                    .binary_search_by_key(&preferred, |&(ref owner, _)| owner.0.to_lowercase())
                    .ok()
            })
            .map(|index| index as u64)
    }

    /// Suggests a name to use for the remote.  Uses the Git config value for
    /// `add-remote.mainForkOwnerAlias` (or "upstream" if this is not set) if the chosen fork is the
    /// main fork/source, then falls back to the map of known users (entries under the Git config
    /// subkey of `add-remote.forkAlias`), and finally suggests the owner name.
    fn suggest_alias(&self) -> String {
        let chosen_owner = &self.available_forks[self.chosen_fork_index].0;
        let alias_arg = if *chosen_owner == self.main_fork_owner {
            "add-remote.mainForkOwnerAlias".to_string()
        } else {
            format!("add-remote.forkAlias.{}", chosen_owner.0)
        };
        self.get_from_gitconfig(&alias_arg).unwrap_or_else(|| {
            if *chosen_owner == self.main_fork_owner {
                "upstream".to_string()
            } else {
                chosen_owner.0.clone()
            }
        })
    }

    fn get_from_gitconfig(&self, key: &str) -> Option<String> {
        let output = Command::new(&self.git)
            .args(&["config", key])
            .output()
            .unwrap();
        if output.status.success() {
            Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
        } else {
            None
        }
    }

    /// Runs `git branch --list <Alias>/* -vr --sort=-committerdate` and returns the output.
    fn git_branch_verbose_output(&self, alias: &str) -> String {
        let alias_arg = format!("{}/*", alias);
        let output = Command::new(&self.git)
            .args(&[
                "branch",
                "--list",
                &alias_arg,
                "-vr",
                "--sort=-committerdate",
            ])
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "Failed to run 'git branch --list {} -vr --sort=-committerdate'",
            alias_arg
        );
        String::from_utf8_lossy(&output.stdout).to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn populate_available_forks() {
        let mut repo = Repo::new_uninitialised();
        let _ = repo.local_remotes.insert(
            Owner("Fraser999".to_string()),
            (
                Name("cargo".to_string()),
                RemoteAlias("origin".to_string()),
                Url::GitHubSsh("git@github.com:Fraser999/cargo.git".to_string()),
            ),
        );
        repo.populate_main_fork_details();
        repo.populate_available_forks();
        repo.show_available_forks();
        assert!(repo.available_forks.len() > 101);
    }
}