rust/rhg/src/main.rs
author Mitchell Kember <mkember@janestreet.com>
Fri, 24 Jan 2025 12:01:12 -0500
changeset 52767 6183949219b2
parent 52760 94e2547e6f3d
permissions -rw-r--r--
rhg: implement rhg annotate This initial implementation produces the same output as Python for all the files I've tried, and is usually 1.5-9x faster. The algorithm is mostly the same, but one key difference is that the Rust implementation only converts filelog revisions to changelog revisions if they will actually appear in the output. This does not support all the command line flags yet. In particular, --template, --include, --exclude, --skip, and whitespace-related flags will cause fallback to Python. Also, --rev 'wdir()' (often used by editor plugins) is not supported. There is also no pager.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45530
b1cea0dc9db0 rhg: Add debug timing
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45529
diff changeset
     1
extern crate log;
48336
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
     2
use crate::error::CommandError;
52756
bbf1c52252ae rust: add encoding.rs
Mitchell Kember <mkember@janestreet.com>
parents: 52558
diff changeset
     3
use crate::ui::Ui;
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
     4
use clap::{command, Arg, ArgMatches};
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
     5
use format_bytes::{format_bytes, join};
49512
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49506
diff changeset
     6
use hg::config::{Config, ConfigSource, PlainInfo};
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
     7
use hg::repo::{Repo, RepoError};
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
     8
use hg::utils::files::{get_bytes_from_os_str, get_path_from_bytes};
52760
94e2547e6f3d rust: move code from utils to utils::strings
Mitchell Kember <mkember@janestreet.com>
parents: 52756
diff changeset
     9
use hg::utils::strings::SliceExt;
49192
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
    10
use hg::{exit_codes, requirements};
49512
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49506
diff changeset
    11
use std::borrow::Cow;
52524
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
    12
use std::collections::{HashMap, HashSet};
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
    13
use std::ffi::OsString;
49149
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
    14
use std::os::unix::prelude::CommandExt;
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
    15
use std::path::PathBuf;
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
    16
use std::process::Command;
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    17
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
    18
mod blackbox;
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48732
diff changeset
    19
mod color;
44982
bacf6c7ef01b rhg: add Command trait for subcommands implemented by rhg
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44981
diff changeset
    20
mod error;
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44982
diff changeset
    21
mod ui;
48174
9ecf802b06e0 rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents: 48077
diff changeset
    22
pub mod utils {
9ecf802b06e0 rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents: 48077
diff changeset
    23
    pub mod path_utils;
9ecf802b06e0 rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents: 48077
diff changeset
    24
}
44981
cf04f62d1579 rhg: add rhg crate
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    25
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
    26
fn main_with_result(
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
    27
    argv: Vec<OsString>,
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
    28
    process_start_time: &blackbox::ProcessStartTime,
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
    29
    ui: &ui::Ui,
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
    30
    repo: Result<&Repo, &NoRepoInCwdError>,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
    31
    config: &Config,
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
    32
) -> Result<(), CommandError> {
48734
3e2b4bb286e7 rhg: Colorize `rhg status` output when appropriate
Simon Sapin <simon.sapin@octobus.net>
parents: 48733
diff changeset
    33
    check_unsupported(config, repo)?;
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
    34
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    35
    let app = command!()
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    36
        .subcommand_required(true)
46608
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    37
        .arg(
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    38
            Arg::new("repository")
46608
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    39
                .help("repository root directory")
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    40
                .short('R')
46608
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    41
                .value_name("REPO")
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    42
                // Both ok: `hg -R ./foo log` or `hg log -R ./foo`
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    43
                .global(true),
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    44
        )
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    45
        .arg(
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    46
            Arg::new("config")
46608
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    47
                .help("set/override config option (use 'section.name=value')")
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    48
                .value_name("CONFIG")
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    49
                .global(true)
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    50
                .long("config")
46608
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    51
                // Ok: `--config section.key1=val --config section.key2=val2`
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    52
                // Not ok: `--config section.key1=val section.key2=val2`
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    53
                .action(clap::ArgAction::Append),
46608
4e4c70401028 rhg: Use clap?s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    54
        )
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    55
        .arg(
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    56
            Arg::new("cwd")
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    57
                .help("change working directory")
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    58
                .value_name("DIR")
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    59
                .long("cwd")
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    60
                .global(true),
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    61
        )
48732
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    62
        .arg(
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    63
            Arg::new("color")
48732
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    64
                .help("when to colorize (boolean, always, auto, never, or debug)")
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    65
                .value_name("TYPE")
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    66
                .long("color")
48732
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    67
                .global(true),
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    68
        )
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
    69
        .version("0.0.1");
52524
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
    70
    let subcommands = subcommands();
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
    71
    let app = subcommands.add_args(app);
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    72
49914
58074252db3c rust: run `cargo clippy`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49640
diff changeset
    73
    let matches = app.try_get_matches_from(argv.iter())?;
46503
d8730ff51d5a rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46502
diff changeset
    74
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    75
    let (subcommand_name, subcommand_args) =
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
    76
        matches.subcommand().expect("subcommand required");
48075
f11f233546ce rhg: fallback if the current command has any generic hook defined
Rapha?l Gom?s <rgomes@octobus.net>
parents: 47951
diff changeset
    77
48076
b44e1184b7e1 rhg: fallback if `defaults` config is set for the current command
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48075
diff changeset
    78
    // Mercurial allows users to define "defaults" for commands, fallback
b44e1184b7e1 rhg: fallback if `defaults` config is set for the current command
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48075
diff changeset
    79
    // if a default is detected for the current command
50883
12476986d89c rhg: allow setting defaults.cmd to an empty string
Spencer Baugh <sbaugh@janestreet.com>
parents: 50806
diff changeset
    80
    let defaults = config.get_str(b"defaults", subcommand_name.as_bytes())?;
12476986d89c rhg: allow setting defaults.cmd to an empty string
Spencer Baugh <sbaugh@janestreet.com>
parents: 50806
diff changeset
    81
    match defaults {
12476986d89c rhg: allow setting defaults.cmd to an empty string
Spencer Baugh <sbaugh@janestreet.com>
parents: 50806
diff changeset
    82
        // Programmatic usage might set defaults to an empty string to unset
12476986d89c rhg: allow setting defaults.cmd to an empty string
Spencer Baugh <sbaugh@janestreet.com>
parents: 50806
diff changeset
    83
        // it; allow that
12476986d89c rhg: allow setting defaults.cmd to an empty string
Spencer Baugh <sbaugh@janestreet.com>
parents: 50806
diff changeset
    84
        None | Some("") => {}
12476986d89c rhg: allow setting defaults.cmd to an empty string
Spencer Baugh <sbaugh@janestreet.com>
parents: 50806
diff changeset
    85
        Some(_) => {
12476986d89c rhg: allow setting defaults.cmd to an empty string
Spencer Baugh <sbaugh@janestreet.com>
parents: 50806
diff changeset
    86
            let msg = "`defaults` config set";
12476986d89c rhg: allow setting defaults.cmd to an empty string
Spencer Baugh <sbaugh@janestreet.com>
parents: 50806
diff changeset
    87
            return Err(CommandError::unsupported(msg));
12476986d89c rhg: allow setting defaults.cmd to an empty string
Spencer Baugh <sbaugh@janestreet.com>
parents: 50806
diff changeset
    88
        }
48076
b44e1184b7e1 rhg: fallback if `defaults` config is set for the current command
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48075
diff changeset
    89
    }
b44e1184b7e1 rhg: fallback if `defaults` config is set for the current command
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48075
diff changeset
    90
48075
f11f233546ce rhg: fallback if the current command has any generic hook defined
Rapha?l Gom?s <rgomes@octobus.net>
parents: 47951
diff changeset
    91
    for prefix in ["pre", "post", "fail"].iter() {
f11f233546ce rhg: fallback if the current command has any generic hook defined
Rapha?l Gom?s <rgomes@octobus.net>
parents: 47951
diff changeset
    92
        // Mercurial allows users to define generic hooks for commands,
f11f233546ce rhg: fallback if the current command has any generic hook defined
Rapha?l Gom?s <rgomes@octobus.net>
parents: 47951
diff changeset
    93
        // fallback if any are detected
f11f233546ce rhg: fallback if the current command has any generic hook defined
Rapha?l Gom?s <rgomes@octobus.net>
parents: 47951
diff changeset
    94
        let item = format!("{}-{}", prefix, subcommand_name);
50761
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50321
diff changeset
    95
        let hook_for_command =
8ff187fbbfea rust-config: add config getters that don't fall back to defaults
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50321
diff changeset
    96
            config.get_str_no_default(b"hooks", item.as_bytes())?;
48075
f11f233546ce rhg: fallback if the current command has any generic hook defined
Rapha?l Gom?s <rgomes@octobus.net>
parents: 47951
diff changeset
    97
        if hook_for_command.is_some() {
f11f233546ce rhg: fallback if the current command has any generic hook defined
Rapha?l Gom?s <rgomes@octobus.net>
parents: 47951
diff changeset
    98
            let msg = format!("{}-{} hook defined", prefix, subcommand_name);
f11f233546ce rhg: fallback if the current command has any generic hook defined
Rapha?l Gom?s <rgomes@octobus.net>
parents: 47951
diff changeset
    99
            return Err(CommandError::unsupported(msg));
f11f233546ce rhg: fallback if the current command has any generic hook defined
Rapha?l Gom?s <rgomes@octobus.net>
parents: 47951
diff changeset
   100
        }
f11f233546ce rhg: fallback if the current command has any generic hook defined
Rapha?l Gom?s <rgomes@octobus.net>
parents: 47951
diff changeset
   101
    }
52524
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   102
    let run = subcommands.run_fn(subcommand_name)
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
   103
        .expect("unknown subcommand name from clap despite Command::subcommand_required");
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   104
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   105
    let invocation = CliInvocation {
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   106
        ui,
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   107
        subcommand_args,
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   108
        config,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   109
        repo,
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   110
    };
48077
ba773bd99203 rhg: fall back if subrepos are detected
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48076
diff changeset
   111
ba773bd99203 rhg: fall back if subrepos are detected
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48076
diff changeset
   112
    if let Ok(repo) = repo {
ba773bd99203 rhg: fall back if subrepos are detected
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48076
diff changeset
   113
        // We don't support subrepos, fallback if the subrepos file is present
ba773bd99203 rhg: fall back if subrepos are detected
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48076
diff changeset
   114
        if repo.working_directory_vfs().join(".hgsub").exists() {
ba773bd99203 rhg: fall back if subrepos are detected
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48076
diff changeset
   115
            let msg = "subrepos (.hgsub is present)";
ba773bd99203 rhg: fall back if subrepos are detected
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48076
diff changeset
   116
            return Err(CommandError::unsupported(msg));
ba773bd99203 rhg: fall back if subrepos are detected
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48076
diff changeset
   117
        }
ba773bd99203 rhg: fall back if subrepos are detected
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48076
diff changeset
   118
    }
ba773bd99203 rhg: fall back if subrepos are detected
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48076
diff changeset
   119
48415
9ff246cd6200 rhg: don't run `blackbox` if not activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48409
diff changeset
   120
    if config.is_extension_enabled(b"blackbox") {
9ff246cd6200 rhg: don't run `blackbox` if not activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48409
diff changeset
   121
        let blackbox =
9ff246cd6200 rhg: don't run `blackbox` if not activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48409
diff changeset
   122
            blackbox::Blackbox::new(&invocation, process_start_time)?;
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   123
        blackbox.log_command_start(argv.iter());
48415
9ff246cd6200 rhg: don't run `blackbox` if not activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48409
diff changeset
   124
        let result = run(&invocation);
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   125
        blackbox.log_command_end(
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   126
            argv.iter(),
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   127
            exit_code(
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   128
                &result,
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   129
                // TODO: show a warning or combine with original error if
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   130
                // `get_bool` returns an error
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   131
                config
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   132
                    .get_bool(b"ui", b"detailed-exit-code")
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   133
                    .unwrap_or(false),
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   134
            ),
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   135
        );
48415
9ff246cd6200 rhg: don't run `blackbox` if not activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48409
diff changeset
   136
        result
9ff246cd6200 rhg: don't run `blackbox` if not activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48409
diff changeset
   137
    } else {
9ff246cd6200 rhg: don't run `blackbox` if not activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48409
diff changeset
   138
        run(&invocation)
9ff246cd6200 rhg: don't run `blackbox` if not activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48409
diff changeset
   139
    }
46591
21d3b40b4c0e rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46505
diff changeset
   140
}
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   141
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   142
fn rhg_main(argv: Vec<OsString>) -> ! {
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   143
    // Run this first, before we find out if the blackbox extension is even
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   144
    // enabled, in order to include everything in-between in the duration
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   145
    // measurements. Reading config files can be slow if they’re on NFS.
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   146
    let process_start_time = blackbox::ProcessStartTime::now();
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   147
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   148
    env_logger::init();
46591
21d3b40b4c0e rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46505
diff changeset
   149
50321
14b57943ae6d rust: fix thread cap (for real this time)
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49931
diff changeset
   150
    // Make sure nothing in a future version of `rhg` sets the global
14b57943ae6d rust: fix thread cap (for real this time)
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49931
diff changeset
   151
    // threadpool before we can cap default threads. (This is also called
14b57943ae6d rust: fix thread cap (for real this time)
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49931
diff changeset
   152
    // in core because Python uses the same code path, we're adding a
14b57943ae6d rust: fix thread cap (for real this time)
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49931
diff changeset
   153
    // redundant check.)
14b57943ae6d rust: fix thread cap (for real this time)
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49931
diff changeset
   154
    hg::utils::cap_default_rayon_threads()
14b57943ae6d rust: fix thread cap (for real this time)
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49931
diff changeset
   155
        .expect("Rayon threadpool already initialized");
14b57943ae6d rust: fix thread cap (for real this time)
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49931
diff changeset
   156
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   157
    let early_args = EarlyArgs::parse(&argv);
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   158
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   159
    let initial_current_dir = early_args.cwd.map(|cwd| {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   160
        let cwd = get_path_from_bytes(&cwd);
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   161
        std::env::current_dir()
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   162
            .and_then(|initial| {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   163
                std::env::set_current_dir(cwd)?;
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   164
                Ok(initial)
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   165
            })
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   166
            .unwrap_or_else(|error| {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   167
                exit(
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   168
                    &argv,
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   169
                    &None,
48731
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
   170
                    &Ui::new_infallible(&Config::empty()),
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   171
                    OnUnsupported::Abort,
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   172
                    Err(CommandError::abort(format!(
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   173
                        "abort: {}: '{}'",
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   174
                        error,
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   175
                        cwd.display()
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   176
                    ))),
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   177
                    false,
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   178
                )
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   179
            })
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   180
    });
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   181
47406
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   182
    let mut non_repo_config =
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   183
        Config::load_non_repo().unwrap_or_else(|error| {
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   184
            // Normally this is decided based on config, but we don’t have that
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   185
            // available. As of this writing config loading never returns an
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   186
            // "unsupported" error but that is not enforced by the type system.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   187
            let on_unsupported = OnUnsupported::Abort;
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   188
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   189
            exit(
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   190
                &argv,
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   191
                &initial_current_dir,
48731
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
   192
                &Ui::new_infallible(&Config::empty()),
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   193
                on_unsupported,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   194
                Err(error.into()),
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   195
                false,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   196
            )
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   197
        });
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   198
47406
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   199
    non_repo_config
48732
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   200
        .load_cli_args(early_args.config, early_args.color)
47406
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   201
        .unwrap_or_else(|error| {
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   202
            exit(
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   203
                &argv,
47406
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   204
                &initial_current_dir,
48731
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
   205
                &Ui::new_infallible(&non_repo_config),
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   206
                OnUnsupported::from_config(&non_repo_config),
47406
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   207
                Err(error.into()),
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   208
                non_repo_config
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   209
                    .get_bool(b"ui", b"detailed-exit-code")
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   210
                    .unwrap_or(false),
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   211
            )
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   212
        });
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   213
46729
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   214
    if let Some(repo_path_bytes) = &early_args.repo {
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   215
        lazy_static::lazy_static! {
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   216
            static ref SCHEME_RE: regex::bytes::Regex =
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   217
                // Same as `_matchscheme` in `mercurial/util.py`
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   218
                regex::bytes::Regex::new("^[a-zA-Z0-9+.\\-]+:").unwrap();
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   219
        }
49914
58074252db3c rust: run `cargo clippy`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49640
diff changeset
   220
        if SCHEME_RE.is_match(repo_path_bytes) {
46729
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   221
            exit(
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   222
                &argv,
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   223
                &initial_current_dir,
48731
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
   224
                &Ui::new_infallible(&non_repo_config),
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   225
                OnUnsupported::from_config(&non_repo_config),
46729
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   226
                Err(CommandError::UnsupportedFeature {
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   227
                    message: format_bytes!(
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   228
                        b"URL-like --repository {}",
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   229
                        repo_path_bytes
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   230
                    ),
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   231
                }),
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   232
                // TODO: show a warning or combine with original error if
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   233
                // `get_bool` returns an error
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   234
                non_repo_config
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   235
                    .get_bool(b"ui", b"detailed-exit-code")
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   236
                    .unwrap_or(false),
46729
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   237
            )
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   238
        }
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   239
    }
49914
58074252db3c rust: run `cargo clippy`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49640
diff changeset
   240
    let repo_arg = early_args.repo.unwrap_or_default();
47404
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   241
    let repo_path: Option<PathBuf> = {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   242
        if repo_arg.is_empty() {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   243
            None
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   244
        } else {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   245
            let local_config = {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   246
                if std::env::var_os("HGRCSKIPREPO").is_none() {
47405
88119fffecc8 rhg: look for repository in ancestors also instead of cwd only
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47404
diff changeset
   247
                    // TODO: handle errors from find_repo_root
88119fffecc8 rhg: look for repository in ancestors also instead of cwd only
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47404
diff changeset
   248
                    if let Ok(current_dir_path) = Repo::find_repo_root() {
47404
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   249
                        let config_files = vec![
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   250
                            ConfigSource::AbsPath(
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   251
                                current_dir_path.join(".hg/hgrc"),
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   252
                            ),
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   253
                            ConfigSource::AbsPath(
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   254
                                current_dir_path.join(".hg/hgrc-not-shared"),
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   255
                            ),
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   256
                        ];
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   257
                        // TODO: handle errors from
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   258
                        // `load_from_explicit_sources`
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   259
                        Config::load_from_explicit_sources(config_files).ok()
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   260
                    } else {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   261
                        None
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   262
                    }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   263
                } else {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   264
                    None
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   265
                }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   266
            };
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   267
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   268
            let non_repo_config_val = {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   269
                let non_repo_val = non_repo_config.get(b"paths", &repo_arg);
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   270
                match &non_repo_val {
49914
58074252db3c rust: run `cargo clippy`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49640
diff changeset
   271
                    Some(val) if !val.is_empty() => home::home_dir()
47404
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   272
                        .unwrap_or_else(|| PathBuf::from("~"))
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   273
                        .join(get_path_from_bytes(val))
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   274
                        .canonicalize()
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   275
                        // TODO: handle error and make it similar to python
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   276
                        // implementation maybe?
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   277
                        .ok(),
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   278
                    _ => None,
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   279
                }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   280
            };
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   281
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   282
            let config_val = match &local_config {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   283
                None => non_repo_config_val,
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   284
                Some(val) => {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   285
                    let local_config_val = val.get(b"paths", &repo_arg);
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   286
                    match &local_config_val {
49914
58074252db3c rust: run `cargo clippy`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49640
diff changeset
   287
                        Some(val) if !val.is_empty() => {
47404
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   288
                            // presence of a local_config assures that
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   289
                            // current_dir
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   290
                            // wont result in an Error
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   291
                            let canpath = hg::utils::current_dir()
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   292
                                .unwrap()
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   293
                                .join(get_path_from_bytes(val))
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   294
                                .canonicalize();
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   295
                            canpath.ok().or(non_repo_config_val)
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   296
                        }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   297
                        _ => non_repo_config_val,
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   298
                    }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   299
                }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   300
            };
49931
fba29deebfe7 rust-clippy: fix remaining warnings in `rhg`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49914
diff changeset
   301
            config_val
fba29deebfe7 rust-clippy: fix remaining warnings in `rhg`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49914
diff changeset
   302
                .or_else(|| Some(get_path_from_bytes(&repo_arg).to_path_buf()))
47404
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   303
        }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   304
    };
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   305
49566
05ef5f097df4 rhg: stop shadowing `exit` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49515
diff changeset
   306
    let simple_exit =
49515
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   307
        |ui: &Ui, config: &Config, result: Result<(), CommandError>| -> ! {
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   308
            exit(
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   309
                &argv,
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   310
                &initial_current_dir,
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   311
                ui,
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   312
                OnUnsupported::from_config(config),
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   313
                result,
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   314
                // TODO: show a warning or combine with original error if
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   315
                // `get_bool` returns an error
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   316
                non_repo_config
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   317
                    .get_bool(b"ui", b"detailed-exit-code")
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   318
                    .unwrap_or(false),
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   319
            )
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   320
        };
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   321
    let early_exit = |config: &Config, error: CommandError| -> ! {
49914
58074252db3c rust: run `cargo clippy`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49640
diff changeset
   322
        simple_exit(&Ui::new_infallible(config), config, Err(error))
49515
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   323
    };
47404
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   324
    let repo_result = match Repo::find(&non_repo_config, repo_path.to_owned())
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   325
    {
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   326
        Ok(repo) => Ok(repo),
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   327
        Err(RepoError::NotFound { at }) if repo_path.is_none() => {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   328
            // Not finding a repo is not fatal yet, if `-R` was not given
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   329
            Err(NoRepoInCwdError { cwd: at })
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   330
        }
49515
e84064e88e5d rhg: share some code
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49514
diff changeset
   331
        Err(error) => early_exit(&non_repo_config, error.into()),
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   332
    };
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   333
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   334
    let config = if let Ok(repo) = &repo_result {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   335
        repo.config()
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   336
    } else {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   337
        &non_repo_config
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   338
    };
49512
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49506
diff changeset
   339
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49506
diff changeset
   340
    let mut config_cow = Cow::Borrowed(config);
49513
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
   341
    config_cow.to_mut().apply_plain(PlainInfo::from_env());
49514
e37416d432e9 rhg: support tweakdefaults
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49513
diff changeset
   342
    if !ui::plain(Some("tweakdefaults"))
e37416d432e9 rhg: support tweakdefaults
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49513
diff changeset
   343
        && config_cow
e37416d432e9 rhg: support tweakdefaults
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49513
diff changeset
   344
            .as_ref()
e37416d432e9 rhg: support tweakdefaults
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49513
diff changeset
   345
            .get_bool(b"ui", b"tweakdefaults")
49914
58074252db3c rust: run `cargo clippy`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49640
diff changeset
   346
            .unwrap_or_else(|error| early_exit(config, error.into()))
49514
e37416d432e9 rhg: support tweakdefaults
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49513
diff changeset
   347
    {
e37416d432e9 rhg: support tweakdefaults
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49513
diff changeset
   348
        config_cow.to_mut().tweakdefaults()
e37416d432e9 rhg: support tweakdefaults
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49513
diff changeset
   349
    };
49512
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49506
diff changeset
   350
    let config = config_cow.as_ref();
49914
58074252db3c rust: run `cargo clippy`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49640
diff changeset
   351
    let ui = Ui::new(config)
58074252db3c rust: run `cargo clippy`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49640
diff changeset
   352
        .unwrap_or_else(|error| early_exit(config, error.into()));
49569
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   353
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   354
    if let Ok(true) = config.get_bool(b"rhg", b"fallback-immediately") {
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   355
        exit(
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   356
            &argv,
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   357
            &initial_current_dir,
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   358
            &ui,
50806
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   359
            OnUnsupported::fallback(config),
49569
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   360
            Err(CommandError::unsupported(
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   361
                "`rhg.fallback-immediately is true`",
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   362
            )),
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   363
            false,
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   364
        )
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   365
    }
5318ac25dfdc rhg: add a config option to fall back immediately
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49566
diff changeset
   366
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   367
    let result = main_with_result(
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   368
        argv.iter().map(|s| s.to_owned()).collect(),
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   369
        &process_start_time,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   370
        &ui,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   371
        repo_result.as_ref(),
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   372
        config,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   373
    );
49914
58074252db3c rust: run `cargo clippy`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49640
diff changeset
   374
    simple_exit(&ui, config, result)
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   375
}
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   376
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   377
fn main() -> ! {
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   378
    rhg_main(std::env::args_os().collect())
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   379
}
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   380
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   381
fn exit_code(
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   382
    result: &Result<(), CommandError>,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   383
    use_detailed_exit_code: bool,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   384
) -> i32 {
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   385
    match result {
47407
6e49769b7f97 rhg: add exit code to HgError::Abort()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47406
diff changeset
   386
        Ok(()) => exit_codes::OK,
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   387
        Err(CommandError::Abort {
49488
9f14126cfc4c rust: add support for hints in error messages
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49484
diff changeset
   388
            detailed_exit_code, ..
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   389
        }) => {
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   390
            if use_detailed_exit_code {
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   391
                *detailed_exit_code
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   392
            } else {
47407
6e49769b7f97 rhg: add exit code to HgError::Abort()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47406
diff changeset
   393
                exit_codes::ABORT
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   394
            }
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   395
        }
47407
6e49769b7f97 rhg: add exit code to HgError::Abort()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47406
diff changeset
   396
        Err(CommandError::Unsuccessful) => exit_codes::UNSUCCESSFUL,
46445
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
   397
        // Exit with a specific code and no error message to let a potential
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
   398
        // wrapper script fallback to Python-based Mercurial.
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   399
        Err(CommandError::UnsupportedFeature { .. }) => {
47407
6e49769b7f97 rhg: add exit code to HgError::Abort()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47406
diff changeset
   400
            exit_codes::UNIMPLEMENTED
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   401
        }
49149
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   402
        Err(CommandError::InvalidFallback { .. }) => {
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   403
            exit_codes::INVALID_FALLBACK
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   404
        }
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   405
    }
44981
cf04f62d1579 rhg: add rhg crate
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   406
}
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   407
51117
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50883
diff changeset
   408
fn exit(
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50883
diff changeset
   409
    original_args: &[OsString],
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   410
    initial_current_dir: &Option<PathBuf>,
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   411
    ui: &Ui,
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   412
    mut on_unsupported: OnUnsupported,
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   413
    result: Result<(), CommandError>,
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   414
    use_detailed_exit_code: bool,
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   415
) -> ! {
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   416
    if let (
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   417
        OnUnsupported::Fallback { executable },
48770
f19be290756a rhg: signal when falling back in logs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48734
diff changeset
   418
        Err(CommandError::UnsupportedFeature { message }),
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   419
    ) = (&on_unsupported, &result)
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   420
    {
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   421
        let mut args = original_args.iter();
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   422
        let executable = match executable {
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   423
            None => {
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   424
                exit_no_fallback(
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   425
                    ui,
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   426
                    OnUnsupported::Abort,
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   427
                    Err(CommandError::abort(
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   428
                        "abort: 'rhg.on-unsupported=fallback' without \
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   429
                                'rhg.fallback-executable' set.",
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   430
                    )),
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   431
                    false,
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   432
                );
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   433
            }
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   434
            Some(executable) => executable,
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   435
        };
49914
58074252db3c rust: run `cargo clippy`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49640
diff changeset
   436
        let executable_path = get_path_from_bytes(executable);
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   437
        let this_executable = args.next().expect("exepcted argv[0] to exist");
49931
fba29deebfe7 rust-clippy: fix remaining warnings in `rhg`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49914
diff changeset
   438
        if executable_path == *this_executable {
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   439
            // Avoid spawning infinitely many processes until resource
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   440
            // exhaustion.
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   441
            let _ = ui.write_stderr(&format_bytes!(
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   442
                b"Blocking recursive fallback. The 'rhg.fallback-executable = {}' config \
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   443
                points to `rhg` itself.\n",
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   444
                executable
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   445
            ));
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   446
            on_unsupported = OnUnsupported::Abort
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   447
        } else {
48770
f19be290756a rhg: signal when falling back in logs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48734
diff changeset
   448
            log::debug!("falling back (see trace-level log)");
52756
bbf1c52252ae rust: add encoding.rs
Mitchell Kember <mkember@janestreet.com>
parents: 52558
diff changeset
   449
            log::trace!("{}", String::from_utf8_lossy(message));
49149
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   450
            if let Err(err) = which::which(executable_path) {
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   451
                exit_no_fallback(
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   452
                    ui,
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   453
                    OnUnsupported::Abort,
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   454
                    Err(CommandError::InvalidFallback {
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   455
                        path: executable.to_owned(),
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   456
                        err: err.to_string(),
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   457
                    }),
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   458
                    use_detailed_exit_code,
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   459
                )
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   460
            }
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   461
            // `args` is now `argv[1..]` since we’ve already consumed
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   462
            // `argv[0]`
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   463
            let mut command = Command::new(executable_path);
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   464
            command.args(args);
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   465
            if let Some(initial) = initial_current_dir {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   466
                command.current_dir(initial);
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   467
            }
49149
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   468
            // We don't use subprocess because proper signal handling is harder
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   469
            // and we don't want to keep `rhg` around after a fallback anyway.
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   470
            // For example, if `rhg` is run in the background and falls back to
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   471
            // `hg` which, in turn, waits for a signal, we'll get stuck if
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   472
            // we're doing plain subprocess.
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   473
            //
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   474
            // If `exec` returns, we can only assume our process is very broken
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   475
            // (see its documentation), so only try to forward the error code
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   476
            // when exiting.
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   477
            let err = command.exec();
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   478
            std::process::exit(
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   479
                err.raw_os_error().unwrap_or(exit_codes::ABORT),
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   480
            );
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   481
        }
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   482
    }
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   483
    exit_no_fallback(ui, on_unsupported, result, use_detailed_exit_code)
46748
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   484
}
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   485
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   486
fn exit_no_fallback(
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   487
    ui: &Ui,
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   488
    on_unsupported: OnUnsupported,
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   489
    result: Result<(), CommandError>,
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   490
    use_detailed_exit_code: bool,
46748
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   491
) -> ! {
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   492
    match &result {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   493
        Ok(_) => {}
46744
b1f2c2b336ec rhg: `cat` command: print error messages for missing files
Simon Sapin <simon.sapin@octobus.net>
parents: 46736
diff changeset
   494
        Err(CommandError::Unsuccessful) => {}
49488
9f14126cfc4c rust: add support for hints in error messages
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49484
diff changeset
   495
        Err(CommandError::Abort { message, hint, .. }) => {
9f14126cfc4c rust: add support for hints in error messages
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49484
diff changeset
   496
            // Ignore errors when writing to stderr, we’re already exiting
9f14126cfc4c rust: add support for hints in error messages
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49484
diff changeset
   497
            // with failure code so there’s not much more we can do.
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   498
            if !message.is_empty() {
46731
3d692e724d06 rhg: Align config file parse error formatting with Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46729
diff changeset
   499
                let _ = ui.write_stderr(&format_bytes!(b"{}\n", message));
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   500
            }
49488
9f14126cfc4c rust: add support for hints in error messages
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49484
diff changeset
   501
            if let Some(hint) = hint {
9f14126cfc4c rust: add support for hints in error messages
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49484
diff changeset
   502
                let _ = ui.write_stderr(&format_bytes!(b"({})\n", hint));
9f14126cfc4c rust: add support for hints in error messages
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49484
diff changeset
   503
            }
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   504
        }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   505
        Err(CommandError::UnsupportedFeature { message }) => {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   506
            match on_unsupported {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   507
                OnUnsupported::Abort => {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   508
                    let _ = ui.write_stderr(&format_bytes!(
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   509
                        b"unsupported feature: {}\n",
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   510
                        message
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   511
                    ));
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   512
                }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   513
                OnUnsupported::AbortSilent => {}
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   514
                OnUnsupported::Fallback { .. } => unreachable!(),
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   515
            }
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   516
        }
49149
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   517
        Err(CommandError::InvalidFallback { path, err }) => {
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   518
            let _ = ui.write_stderr(&format_bytes!(
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   519
                b"abort: invalid fallback '{}': {}\n",
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   520
                path,
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   521
                err.as_bytes(),
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   522
            ));
006688e36e12 rhg: use `Command::exec` instead of `Command::status`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   523
        }
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   524
    }
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   525
    std::process::exit(exit_code(&result, use_detailed_exit_code))
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   526
}
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   527
51152
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   528
mod commands {
52767
6183949219b2 rhg: implement rhg annotate
Mitchell Kember <mkember@janestreet.com>
parents: 52760
diff changeset
   529
    pub mod annotate;
51152
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   530
    pub mod cat;
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   531
    pub mod config;
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   532
    pub mod debugdata;
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   533
    pub mod debugignorerhg;
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   534
    pub mod debugrequirements;
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   535
    pub mod debugrhgsparse;
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   536
    pub mod files;
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   537
    pub mod root;
52557
b89c934e6269 rust-hgignore: add a scripting command to print the hgignore regexp
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52524
diff changeset
   538
    pub mod script_hgignore;
51152
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   539
    pub mod status;
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   540
}
ac3859a8b796 rhg: support rhg status --rev --rev
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51117
diff changeset
   541
52522
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   542
pub type RunFn = fn(&CliInvocation) -> Result<(), CommandError>;
46484
a6e4e4650bac rhg: Parse system and user configuration at program start
Simon Sapin <simon.sapin@octobus.net>
parents: 46445
diff changeset
   543
52522
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   544
struct SubCommand {
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   545
    run: RunFn,
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   546
    args: clap::Command,
52558
2d52ae3c5f76 rhg: simplify SubCommand macro a bit more
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52557
diff changeset
   547
    /// used for reporting name collisions
52524
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   548
    origin: String,
52522
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   549
}
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   550
52558
2d52ae3c5f76 rhg: simplify SubCommand macro a bit more
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52557
diff changeset
   551
impl SubCommand {
2d52ae3c5f76 rhg: simplify SubCommand macro a bit more
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52557
diff changeset
   552
    fn name(&self) -> String {
2d52ae3c5f76 rhg: simplify SubCommand macro a bit more
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52557
diff changeset
   553
        self.args.get_name().to_string()
2d52ae3c5f76 rhg: simplify SubCommand macro a bit more
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52557
diff changeset
   554
    }
2d52ae3c5f76 rhg: simplify SubCommand macro a bit more
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52557
diff changeset
   555
}
2d52ae3c5f76 rhg: simplify SubCommand macro a bit more
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52557
diff changeset
   556
52522
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   557
macro_rules! subcommand {
52523
021c1b1671e5 rhg: consistently use the command name given in clap::command!(<...>) macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52522
diff changeset
   558
    ($command: ident) => {{
52522
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   559
        SubCommand {
52558
2d52ae3c5f76 rhg: simplify SubCommand macro a bit more
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52557
diff changeset
   560
            args: commands::$command::args(),
52522
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   561
            run: commands::$command::run,
52558
2d52ae3c5f76 rhg: simplify SubCommand macro a bit more
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52557
diff changeset
   562
            origin: stringify!($command).to_string(),
45542
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45537
diff changeset
   563
        }
52523
021c1b1671e5 rhg: consistently use the command name given in clap::command!(<...>) macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52522
diff changeset
   564
    }};
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   565
}
52524
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   566
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   567
struct Subcommands {
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   568
    commands: Vec<clap::Command>,
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   569
    run: HashMap<String, (String, RunFn)>,
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   570
}
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   571
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   572
/// `Subcommands` construction
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   573
impl Subcommands {
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   574
    pub fn new() -> Self {
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   575
        Self {
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   576
            commands: vec![],
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   577
            run: HashMap::new(),
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   578
        }
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   579
    }
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   580
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   581
    pub fn add(&mut self, subcommand: SubCommand) {
52558
2d52ae3c5f76 rhg: simplify SubCommand macro a bit more
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52557
diff changeset
   582
        let name = subcommand.name();
52524
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   583
        if let Some((origin_old, _)) = self
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   584
            .run
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   585
            .insert(name.clone(), (subcommand.origin.clone(), subcommand.run))
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   586
        {
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   587
            panic!(
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   588
                "command `{}` is defined in two places (`{}` and `{}`)",
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   589
                name, origin_old, subcommand.origin
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   590
            )
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   591
        }
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   592
        self.commands.push(subcommand.args)
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   593
    }
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   594
}
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   595
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   596
/// `Subcommands` querying
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   597
impl Subcommands {
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   598
    pub fn add_args(&self, mut app: clap::Command) -> clap::Command {
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   599
        for cmd in self.commands.iter() {
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   600
            app = app.subcommand(cmd)
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   601
        }
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   602
        app
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   603
    }
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   604
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   605
    pub fn run_fn(&self, name: &str) -> Option<RunFn> {
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   606
        let (_, run) = self.run.get(name)?;
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   607
        Some(*run)
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   608
    }
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   609
}
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   610
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   611
fn subcommands() -> Subcommands {
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   612
    let subcommands = vec![
52767
6183949219b2 rhg: implement rhg annotate
Mitchell Kember <mkember@janestreet.com>
parents: 52760
diff changeset
   613
        subcommand!(annotate),
52522
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   614
        subcommand!(cat),
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   615
        subcommand!(debugdata),
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   616
        subcommand!(debugrequirements),
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   617
        subcommand!(debugignorerhg),
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   618
        subcommand!(debugrhgsparse),
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   619
        subcommand!(files),
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   620
        subcommand!(root),
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   621
        subcommand!(config),
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   622
        subcommand!(status),
52557
b89c934e6269 rust-hgignore: add a scripting command to print the hgignore regexp
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52524
diff changeset
   623
        subcommand!(script_hgignore),
52524
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   624
    ];
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   625
    let mut commands = Subcommands::new();
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   626
    for cmd in subcommands {
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   627
        commands.add(cmd)
52522
92c6c8ab6f94 rhg: simplify the subcommands macro
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51152
diff changeset
   628
    }
52524
a0587c1b633a rhg: add a collision detection to complain about duplicated commands
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52523
diff changeset
   629
    commands
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   630
}
46822
c71e8d9e7f2a rhg: Initial support for the 'status' command
Georges Racinet <georges.racinet@octobus.net>
parents: 46820
diff changeset
   631
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   632
pub struct CliInvocation<'a> {
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   633
    ui: &'a Ui,
49640
37bc3edef76f rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49569
diff changeset
   634
    subcommand_args: &'a ArgMatches,
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   635
    config: &'a Config,
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   636
    /// References inside `Result` is a bit peculiar but allow
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   637
    /// `invocation.repo?` to work out with `&CliInvocation` since this
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   638
    /// `Result` type is `Copy`.
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   639
    repo: Result<&'a Repo, &'a NoRepoInCwdError>,
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   640
}
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   641
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   642
struct NoRepoInCwdError {
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   643
    cwd: PathBuf,
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   644
}
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   645
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   646
/// CLI arguments to be parsed "early" in order to be able to read
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   647
/// configuration before using Clap. Ideally we would also use Clap for this,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   648
/// see <https://github.com/clap-rs/clap/discussions/2366>.
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   649
///
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   650
/// These arguments are still declared when we do use Clap later, so that Clap
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   651
/// does not return an error for their presence.
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   652
struct EarlyArgs {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   653
    /// Values of all `--config` arguments. (Possibly none)
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   654
    config: Vec<Vec<u8>>,
48732
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   655
    /// Value of all the `--color` argument, if any.
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   656
    color: Option<Vec<u8>>,
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   657
    /// Value of the `-R` or `--repository` argument, if any.
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   658
    repo: Option<Vec<u8>>,
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   659
    /// Value of the `--cwd` argument, if any.
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   660
    cwd: Option<Vec<u8>>,
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   661
}
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   662
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   663
impl EarlyArgs {
49070
137a93125902 rhg: refactor to pass argv down, instead of caling args_os()
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48985
diff changeset
   664
    fn parse<'a>(args: impl IntoIterator<Item = &'a OsString>) -> Self {
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   665
        let mut args = args.into_iter().map(get_bytes_from_os_str);
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   666
        let mut config = Vec::new();
48732
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   667
        let mut color = None;
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   668
        let mut repo = None;
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   669
        let mut cwd = None;
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   670
        // Use `while let` instead of `for` so that we can also call
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   671
        // `args.next()` inside the loop.
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   672
        while let Some(arg) = args.next() {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   673
            if arg == b"--config" {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   674
                if let Some(value) = args.next() {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   675
                    config.push(value)
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   676
                }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   677
            } else if let Some(value) = arg.drop_prefix(b"--config=") {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   678
                config.push(value.to_owned())
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   679
            }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   680
48732
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   681
            if arg == b"--color" {
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   682
                if let Some(value) = args.next() {
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   683
                    color = Some(value)
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   684
                }
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   685
            } else if let Some(value) = arg.drop_prefix(b"--color=") {
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   686
                color = Some(value.to_owned())
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   687
            }
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   688
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   689
            if arg == b"--cwd" {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   690
                if let Some(value) = args.next() {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   691
                    cwd = Some(value)
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   692
                }
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   693
            } else if let Some(value) = arg.drop_prefix(b"--cwd=") {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   694
                cwd = Some(value.to_owned())
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   695
            }
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   696
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   697
            if arg == b"--repository" || arg == b"-R" {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   698
                if let Some(value) = args.next() {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   699
                    repo = Some(value)
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   700
                }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   701
            } else if let Some(value) = arg.drop_prefix(b"--repository=") {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   702
                repo = Some(value.to_owned())
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   703
            } else if let Some(value) = arg.drop_prefix(b"-R") {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   704
                repo = Some(value.to_owned())
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   705
            }
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   706
        }
48732
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   707
        Self {
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   708
            config,
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   709
            color,
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   710
            repo,
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   711
            cwd,
d4a5c2197208 rhg: Add parsing for the --color global CLI argument
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   712
        }
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   713
    }
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   714
}
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   715
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   716
/// What to do when encountering some unsupported feature.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   717
///
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   718
/// See `HgError::UnsupportedFeature` and `CommandError::UnsupportedFeature`.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   719
enum OnUnsupported {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   720
    /// Print an error message describing what feature is not supported,
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   721
    /// and exit with code 252.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   722
    Abort,
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   723
    /// Silently exit with code 252.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   724
    AbortSilent,
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   725
    /// Try running a Python implementation
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   726
    Fallback { executable: Option<Vec<u8>> },
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   727
}
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   728
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   729
impl OnUnsupported {
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   730
    const DEFAULT: Self = OnUnsupported::Abort;
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   731
50806
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   732
    fn fallback_executable(config: &Config) -> Option<Vec<u8>> {
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   733
        config
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   734
            .get(b"rhg", b"fallback-executable")
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   735
            .map(|x| x.to_owned())
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   736
    }
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   737
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   738
    fn fallback(config: &Config) -> Self {
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   739
        OnUnsupported::Fallback {
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   740
            executable: Self::fallback_executable(config),
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   741
        }
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   742
    }
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   743
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   744
    fn from_config(config: &Config) -> Self {
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   745
        match config
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   746
            .get(b"rhg", b"on-unsupported")
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   747
            .map(|value| value.to_ascii_lowercase())
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   748
            .as_deref()
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   749
        {
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   750
            Some(b"abort") => OnUnsupported::Abort,
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   751
            Some(b"abort-silent") => OnUnsupported::AbortSilent,
50806
53d77f96e049 rhg: tweak rhg fallback code structure
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50761
diff changeset
   752
            Some(b"fallback") => Self::fallback(config),
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   753
            None => Self::DEFAULT,
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   754
            Some(_) => {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   755
                // TODO: warn about unknown config value
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   756
                Self::DEFAULT
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   757
            }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   758
        }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   759
    }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   760
}
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   761
48439
9cf5ac8c7109 rhg: support the new extension suboptions syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48415
diff changeset
   762
/// The `*` extension is an edge-case for config sub-options that apply to all
9cf5ac8c7109 rhg: support the new extension suboptions syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48415
diff changeset
   763
/// extensions. For now, only `:required` exists, but that may change in the
9cf5ac8c7109 rhg: support the new extension suboptions syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48415
diff changeset
   764
/// future.
49481
a0d189b2e871 rhg: don't fallback if `strip` or `rebase` are activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49477
diff changeset
   765
const SUPPORTED_EXTENSIONS: &[&[u8]] = &[
a0d189b2e871 rhg: don't fallback if `strip` or `rebase` are activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49477
diff changeset
   766
    b"blackbox",
a0d189b2e871 rhg: don't fallback if `strip` or `rebase` are activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49477
diff changeset
   767
    b"share",
a0d189b2e871 rhg: don't fallback if `strip` or `rebase` are activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49477
diff changeset
   768
    b"sparse",
a0d189b2e871 rhg: don't fallback if `strip` or `rebase` are activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49477
diff changeset
   769
    b"narrow",
a0d189b2e871 rhg: don't fallback if `strip` or `rebase` are activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49477
diff changeset
   770
    b"*",
a0d189b2e871 rhg: don't fallback if `strip` or `rebase` are activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49477
diff changeset
   771
    b"strip",
a0d189b2e871 rhg: don't fallback if `strip` or `rebase` are activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49477
diff changeset
   772
    b"rebase",
a0d189b2e871 rhg: don't fallback if `strip` or `rebase` are activated
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49477
diff changeset
   773
];
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   774
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   775
fn check_extensions(config: &Config) -> Result<(), CommandError> {
48972
6b31c0676147 rhg: add support for ignoring all extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   776
    if let Some(b"*") = config.get(b"rhg", b"ignored-extensions") {
6b31c0676147 rhg: add support for ignoring all extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   777
        // All extensions are to be ignored, nothing to do here
6b31c0676147 rhg: add support for ignoring all extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   778
        return Ok(());
6b31c0676147 rhg: add support for ignoring all extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   779
    }
6b31c0676147 rhg: add support for ignoring all extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48770
diff changeset
   780
48439
9cf5ac8c7109 rhg: support the new extension suboptions syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48415
diff changeset
   781
    let enabled: HashSet<&[u8]> = config
49477
f3cd2d6eeef9 rhg: support "!" syntax for disabling extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49195
diff changeset
   782
        .iter_section(b"extensions")
f3cd2d6eeef9 rhg: support "!" syntax for disabling extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49195
diff changeset
   783
        .filter_map(|(extension, value)| {
f3cd2d6eeef9 rhg: support "!" syntax for disabling extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49195
diff changeset
   784
            if value == b"!" {
f3cd2d6eeef9 rhg: support "!" syntax for disabling extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49195
diff changeset
   785
                // Filter out disabled extensions
f3cd2d6eeef9 rhg: support "!" syntax for disabling extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49195
diff changeset
   786
                return None;
f3cd2d6eeef9 rhg: support "!" syntax for disabling extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49195
diff changeset
   787
            }
48439
9cf5ac8c7109 rhg: support the new extension suboptions syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48415
diff changeset
   788
            // Ignore extension suboptions. Only `required` exists for now.
9cf5ac8c7109 rhg: support the new extension suboptions syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48415
diff changeset
   789
            // `rhg` either supports an extension or doesn't, so it doesn't
9cf5ac8c7109 rhg: support the new extension suboptions syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48415
diff changeset
   790
            // make sense to consider the loading of an extension.
49477
f3cd2d6eeef9 rhg: support "!" syntax for disabling extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49195
diff changeset
   791
            let actual_extension =
f3cd2d6eeef9 rhg: support "!" syntax for disabling extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49195
diff changeset
   792
                extension.split_2(b':').unwrap_or((extension, b"")).0;
f3cd2d6eeef9 rhg: support "!" syntax for disabling extensions
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49195
diff changeset
   793
            Some(actual_extension)
48439
9cf5ac8c7109 rhg: support the new extension suboptions syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48415
diff changeset
   794
        })
9cf5ac8c7109 rhg: support the new extension suboptions syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48415
diff changeset
   795
        .collect();
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   796
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   797
    let mut unsupported = enabled;
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   798
    for supported in SUPPORTED_EXTENSIONS {
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   799
        unsupported.remove(supported);
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   800
    }
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   801
47951
cff41e168c25 rhg: Switch rhg.ignored-extensions config to Python-compatible list syntax
Simon Sapin <simon.sapin@octobus.net>
parents: 47407
diff changeset
   802
    if let Some(ignored_list) = config.get_list(b"rhg", b"ignored-extensions")
46734
1a036d33bc18 rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 46733
diff changeset
   803
    {
1a036d33bc18 rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 46733
diff changeset
   804
        for ignored in ignored_list {
47951
cff41e168c25 rhg: Switch rhg.ignored-extensions config to Python-compatible list syntax
Simon Sapin <simon.sapin@octobus.net>
parents: 47407
diff changeset
   805
            unsupported.remove(ignored.as_slice());
46734
1a036d33bc18 rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 46733
diff changeset
   806
        }
1a036d33bc18 rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 46733
diff changeset
   807
    }
1a036d33bc18 rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 46733
diff changeset
   808
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   809
    if unsupported.is_empty() {
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   810
        Ok(())
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   811
    } else {
48985
c75dae6e4ca7 rhg: sort unsupported extensions in error message
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48972
diff changeset
   812
        let mut unsupported: Vec<_> = unsupported.into_iter().collect();
c75dae6e4ca7 rhg: sort unsupported extensions in error message
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48972
diff changeset
   813
        // Sort the extensions to get a stable output
c75dae6e4ca7 rhg: sort unsupported extensions in error message
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48972
diff changeset
   814
        unsupported.sort();
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   815
        Err(CommandError::UnsupportedFeature {
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   816
            message: format_bytes!(
46734
1a036d33bc18 rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 46733
diff changeset
   817
                b"extensions: {} (consider adding them to 'rhg.ignored-extensions' config)",
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   818
                join(unsupported, b", ")
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   819
            ),
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   820
        })
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   821
    }
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   822
}
48336
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   823
49192
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   824
/// Array of tuples of (auto upgrade conf, feature conf, local requirement)
49931
fba29deebfe7 rust-clippy: fix remaining warnings in `rhg`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49914
diff changeset
   825
#[allow(clippy::type_complexity)]
49192
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   826
const AUTO_UPGRADES: &[((&str, &str), (&str, &str), &str)] = &[
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   827
    (
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   828
        ("format", "use-share-safe.automatic-upgrade-of-mismatching-repositories"),
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   829
        ("format", "use-share-safe"),
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   830
        requirements::SHARESAFE_REQUIREMENT,
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   831
    ),
49194
e4b31016e194 auto-upgrade: introduce a way to auto-upgrade to/from tracked-hint
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49192
diff changeset
   832
    (
e4b31016e194 auto-upgrade: introduce a way to auto-upgrade to/from tracked-hint
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49192
diff changeset
   833
        ("format", "use-dirstate-tracked-hint.automatic-upgrade-of-mismatching-repositories"),
e4b31016e194 auto-upgrade: introduce a way to auto-upgrade to/from tracked-hint
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49192
diff changeset
   834
        ("format", "use-dirstate-tracked-hint"),
e4b31016e194 auto-upgrade: introduce a way to auto-upgrade to/from tracked-hint
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49192
diff changeset
   835
        requirements::DIRSTATE_TRACKED_HINT_V1,
e4b31016e194 auto-upgrade: introduce a way to auto-upgrade to/from tracked-hint
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49192
diff changeset
   836
    ),
49195
411d591e0a27 auto-upgrade: introduce a way to auto-upgrade to/from dirstate-v2
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49194
diff changeset
   837
    (
49500
3a53871048dc rhg: fix bugs around [use-dirstate-tracked-hint] and repo auto-upgrade
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49195
diff changeset
   838
        ("format", "use-dirstate-v2.automatic-upgrade-of-mismatching-repositories"),
49195
411d591e0a27 auto-upgrade: introduce a way to auto-upgrade to/from dirstate-v2
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49194
diff changeset
   839
        ("format", "use-dirstate-v2"),
411d591e0a27 auto-upgrade: introduce a way to auto-upgrade to/from dirstate-v2
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49194
diff changeset
   840
        requirements::DIRSTATE_V2_REQUIREMENT,
411d591e0a27 auto-upgrade: introduce a way to auto-upgrade to/from dirstate-v2
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49194
diff changeset
   841
    ),
49192
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   842
];
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   843
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   844
/// Mercurial allows users to automatically upgrade their repository.
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   845
/// `rhg` does not have the ability to upgrade yet, so fallback if an upgrade
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   846
/// is needed.
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   847
fn check_auto_upgrade(
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   848
    config: &Config,
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   849
    reqs: &HashSet<String>,
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   850
) -> Result<(), CommandError> {
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   851
    for (upgrade_conf, feature_conf, local_req) in AUTO_UPGRADES.iter() {
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   852
        let auto_upgrade = config
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   853
            .get_bool(upgrade_conf.0.as_bytes(), upgrade_conf.1.as_bytes())?;
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   854
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   855
        if auto_upgrade {
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   856
            let want_it = config.get_bool(
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   857
                feature_conf.0.as_bytes(),
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   858
                feature_conf.1.as_bytes(),
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   859
            )?;
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   860
            let have_it = reqs.contains(*local_req);
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   861
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   862
            let action = match (want_it, have_it) {
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   863
                (true, false) => Some("upgrade"),
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   864
                (false, true) => Some("downgrade"),
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   865
                _ => None,
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   866
            };
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   867
            if let Some(action) = action {
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   868
                let message = format!(
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   869
                    "automatic {} {}.{}",
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   870
                    action, upgrade_conf.0, upgrade_conf.1
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   871
                );
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   872
                return Err(CommandError::unsupported(message));
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   873
            }
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   874
        }
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   875
    }
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   876
    Ok(())
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   877
}
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   878
48340
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   879
fn check_unsupported(
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   880
    config: &Config,
48510
7f633432ca92 rhg: Sub-repositories are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48439
diff changeset
   881
    repo: Result<&Repo, &NoRepoInCwdError>,
48340
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   882
) -> Result<(), CommandError> {
48336
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   883
    check_extensions(config)?;
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   884
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   885
    if std::env::var_os("HG_PENDING").is_some() {
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   886
        // TODO: only if the value is `== repo.working_directory`?
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   887
        // What about relative v.s. absolute paths?
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   888
        Err(CommandError::unsupported("$HG_PENDING"))?
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   889
    }
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   890
48510
7f633432ca92 rhg: Sub-repositories are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48439
diff changeset
   891
    if let Ok(repo) = repo {
7f633432ca92 rhg: Sub-repositories are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48439
diff changeset
   892
        if repo.has_subrepos()? {
7f633432ca92 rhg: Sub-repositories are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48439
diff changeset
   893
            Err(CommandError::unsupported("sub-repositories"))?
7f633432ca92 rhg: Sub-repositories are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48439
diff changeset
   894
        }
49192
2ab79873786e auto-upgrade: introduce a way to auto-upgrade to/from share-safe
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49164
diff changeset
   895
        check_auto_upgrade(config, repo.requirements())?;
48510
7f633432ca92 rhg: Sub-repositories are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48439
diff changeset
   896
    }
7f633432ca92 rhg: Sub-repositories are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48439
diff changeset
   897
48339
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   898
    if config.has_non_empty_section(b"encode") {
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   899
        Err(CommandError::unsupported("[encode] config"))?
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   900
    }
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   901
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   902
    if config.has_non_empty_section(b"decode") {
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   903
        Err(CommandError::unsupported("[decode] config"))?
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   904
    }
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   905
48336
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   906
    Ok(())
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   907
}