rust/rhg/src/main.rs
author Raphaël Gomès <rgomes@octobus.net>
Fri, 26 Nov 2021 15:07:59 +0100
changeset 48415 9ff246cd6200
parent 48409 005ae1a343f8
child 48439 9cf5ac8c7109
permissions -rw-r--r--
rhg: don't run `blackbox` if not activated You currently have no way of turning off blackbox. Aside from being a bug, this can be annoying if you use `rhg` in your shell prompt, but also because the current implementation of blackbox is quite slow due to `user` querying. Differential Revision: https://phab.mercurial-scm.org/D11813
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;
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
     3
use crate::ui::Ui;
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
     4
use clap::App;
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
     5
use clap::AppSettings;
46503
d8730ff51d5a rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46502
diff changeset
     6
use clap::Arg;
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
     7
use clap::ArgMatches;
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
     8
use format_bytes::{format_bytes, join};
47404
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
     9
use hg::config::{Config, ConfigSource};
47407
6e49769b7f97 rhg: add exit code to HgError::Abort()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47406
diff changeset
    10
use hg::exit_codes;
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
    11
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
    12
use hg::utils::files::{get_bytes_from_os_str, get_path_from_bytes};
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
    13
use hg::utils::SliceExt;
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
    14
use std::ffi::OsString;
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;
44982
bacf6c7ef01b rhg: add Command trait for subcommands implemented by rhg
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44981
diff changeset
    19
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
    20
mod ui;
48174
9ecf802b06e0 rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents: 48077
diff changeset
    21
pub mod utils {
9ecf802b06e0 rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents: 48077
diff changeset
    22
    pub mod path_utils;
9ecf802b06e0 rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents: 48077
diff changeset
    23
}
44981
cf04f62d1579 rhg: add rhg crate
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    24
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
    25
fn main_with_result(
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
    26
    process_start_time: &blackbox::ProcessStartTime,
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
    27
    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
    28
    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
    29
    config: &Config,
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
    30
) -> Result<(), CommandError> {
48340
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
    31
    check_unsupported(config, ui)?;
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
    32
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    33
    let app = App::new("rhg")
46608
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    34
        .global_setting(AppSettings::AllowInvalidUtf8)
46746
eb14264b98e8 rhg: Fall back to Python for --version
Simon Sapin <simon.sapin@octobus.net>
parents: 46744
diff changeset
    35
        .global_setting(AppSettings::DisableVersion)
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    36
        .setting(AppSettings::SubcommandRequired)
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    37
        .setting(AppSettings::VersionlessSubcommands)
46608
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    38
        .arg(
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    39
            Arg::with_name("repository")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    40
                .help("repository root directory")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    41
                .short("-R")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    42
                .long("--repository")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    43
                .value_name("REPO")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    44
                .takes_value(true)
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    45
                // 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
    46
                .global(true),
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    47
        )
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    48
        .arg(
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    49
            Arg::with_name("config")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    50
                .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
    51
                .long("--config")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    52
                .value_name("CONFIG")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    53
                .takes_value(true)
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    54
                .global(true)
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    55
                // 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
    56
                .multiple(true)
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    57
                // Not ok: `--config section.key1=val section.key2=val2`
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    58
                .number_of_values(1),
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
    59
        )
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    60
        .arg(
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    61
            Arg::with_name("cwd")
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    62
                .help("change working directory")
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    63
                .long("--cwd")
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    64
                .value_name("DIR")
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    65
                .takes_value(true)
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    66
                .global(true),
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
    67
        )
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
    68
        .version("0.0.1");
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
    69
    let app = add_subcommand_args(app);
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    70
46591
21d3b40b4c0e rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46505
diff changeset
    71
    let matches = app.clone().get_matches_safe()?;
46503
d8730ff51d5a rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46502
diff changeset
    72
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
    73
    let (subcommand_name, subcommand_matches) = matches.subcommand();
48075
f11f233546ce rhg: fallback if the current command has any generic hook defined
Raphaël Gomès <rgomes@octobus.net>
parents: 47951
diff changeset
    74
48076
b44e1184b7e1 rhg: fallback if `defaults` config is set for the current command
Raphaël Gomès <rgomes@octobus.net>
parents: 48075
diff changeset
    75
    // 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
    76
    // if a default is detected for the current command
b44e1184b7e1 rhg: fallback if `defaults` config is set for the current command
Raphaël Gomès <rgomes@octobus.net>
parents: 48075
diff changeset
    77
    let defaults = config.get_str(b"defaults", subcommand_name.as_bytes());
b44e1184b7e1 rhg: fallback if `defaults` config is set for the current command
Raphaël Gomès <rgomes@octobus.net>
parents: 48075
diff changeset
    78
    if defaults?.is_some() {
b44e1184b7e1 rhg: fallback if `defaults` config is set for the current command
Raphaël Gomès <rgomes@octobus.net>
parents: 48075
diff changeset
    79
        let msg = "`defaults` config set";
b44e1184b7e1 rhg: fallback if `defaults` config is set for the current command
Raphaël Gomès <rgomes@octobus.net>
parents: 48075
diff changeset
    80
        return Err(CommandError::unsupported(msg));
b44e1184b7e1 rhg: fallback if `defaults` config is set for the current command
Raphaël Gomès <rgomes@octobus.net>
parents: 48075
diff changeset
    81
    }
b44e1184b7e1 rhg: fallback if `defaults` config is set for the current command
Raphaël Gomès <rgomes@octobus.net>
parents: 48075
diff changeset
    82
48075
f11f233546ce rhg: fallback if the current command has any generic hook defined
Raphaël Gomès <rgomes@octobus.net>
parents: 47951
diff changeset
    83
    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
    84
        // 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
    85
        // 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
    86
        let item = format!("{}-{}", 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
    87
        let hook_for_command = config.get_str(b"hooks", item.as_bytes())?;
f11f233546ce rhg: fallback if the current command has any generic hook defined
Raphaël Gomès <rgomes@octobus.net>
parents: 47951
diff changeset
    88
        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
    89
            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
    90
            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
    91
        }
f11f233546ce rhg: fallback if the current command has any generic hook defined
Raphaël Gomès <rgomes@octobus.net>
parents: 47951
diff changeset
    92
    }
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
    93
    let run = subcommand_run_fn(subcommand_name)
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
    94
        .expect("unknown subcommand name from clap despite AppSettings::SubcommandRequired");
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
    95
    let subcommand_args = subcommand_matches
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
    96
        .expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired");
45050
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    97
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
    98
    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
    99
        ui,
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   100
        subcommand_args,
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   101
        config,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   102
        repo,
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   103
    };
48077
ba773bd99203 rhg: fall back if subrepos are detected
Raphaël Gomès <rgomes@octobus.net>
parents: 48076
diff changeset
   104
ba773bd99203 rhg: fall back if subrepos are detected
Raphaël Gomès <rgomes@octobus.net>
parents: 48076
diff changeset
   105
    if let Ok(repo) = repo {
ba773bd99203 rhg: fall back if subrepos are detected
Raphaël Gomès <rgomes@octobus.net>
parents: 48076
diff changeset
   106
        // 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
   107
        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
   108
            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
   109
            return Err(CommandError::unsupported(msg));
ba773bd99203 rhg: fall back if subrepos are detected
Raphaël Gomès <rgomes@octobus.net>
parents: 48076
diff changeset
   110
        }
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
48415
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   113
    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
   114
        let blackbox =
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   115
            blackbox::Blackbox::new(&invocation, process_start_time)?;
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   116
        blackbox.log_command_start();
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   117
        let result = run(&invocation);
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   118
        blackbox.log_command_end(exit_code(
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   119
            &result,
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   120
            // TODO: show a warning or combine with original error if
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   121
            // `get_bool` returns an error
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   122
            config
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   123
                .get_bool(b"ui", b"detailed-exit-code")
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   124
                .unwrap_or(false),
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   125
        ));
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   126
        result
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   127
    } else {
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   128
        run(&invocation)
9ff246cd6200 rhg: don't run `blackbox` if not activated
Raphaël Gomès <rgomes@octobus.net>
parents: 48409
diff changeset
   129
    }
46591
21d3b40b4c0e rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46505
diff changeset
   130
}
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   131
46591
21d3b40b4c0e rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46505
diff changeset
   132
fn main() {
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   133
    // 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
   134
    // 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
   135
    // 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
   136
    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
   137
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   138
    env_logger::init();
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   139
    let ui = ui::Ui::new();
46591
21d3b40b4c0e rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46505
diff changeset
   140
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   141
    let early_args = EarlyArgs::parse(std::env::args_os());
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   142
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   143
    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
   144
        let cwd = get_path_from_bytes(&cwd);
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   145
        std::env::current_dir()
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   146
            .and_then(|initial| {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   147
                std::env::set_current_dir(cwd)?;
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   148
                Ok(initial)
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   149
            })
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   150
            .unwrap_or_else(|error| {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   151
                exit(
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   152
                    &None,
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   153
                    &ui,
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   154
                    OnUnsupported::Abort,
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   155
                    Err(CommandError::abort(format!(
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   156
                        "abort: {}: '{}'",
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   157
                        error,
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   158
                        cwd.display()
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   159
                    ))),
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   160
                    false,
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   161
                )
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   162
            })
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   163
    });
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   164
47406
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   165
    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
   166
        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
   167
            // 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
   168
            // 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
   169
            // "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
   170
            let on_unsupported = OnUnsupported::Abort;
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   171
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   172
            exit(
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   173
                &initial_current_dir,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   174
                &ui,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   175
                on_unsupported,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   176
                Err(error.into()),
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   177
                false,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   178
            )
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   179
        });
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   180
47406
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   181
    non_repo_config
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   182
        .load_cli_args_config(early_args.config)
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   183
        .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
   184
            exit(
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   185
                &initial_current_dir,
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   186
                &ui,
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   187
                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
   188
                Err(error.into()),
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   189
                non_repo_config
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   190
                    .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
   191
                    .unwrap_or(false),
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   192
            )
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   193
        });
3237ed4dcda4 rhg: split non_repo_config and `--config` loading in different functions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47405
diff changeset
   194
46729
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   195
    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
   196
        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
   197
            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
   198
                // 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
   199
                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
   200
        }
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   201
        if SCHEME_RE.is_match(&repo_path_bytes) {
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   202
            exit(
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   203
                &initial_current_dir,
46729
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   204
                &ui,
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   205
                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
   206
                Err(CommandError::UnsupportedFeature {
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   207
                    message: format_bytes!(
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   208
                        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
   209
                        repo_path_bytes
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   210
                    ),
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   211
                }),
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   212
                // 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
   213
                // `get_bool` returns an error
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   214
                non_repo_config
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   215
                    .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
   216
                    .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
   217
            )
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   218
        }
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46667
diff changeset
   219
    }
47404
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   220
    let repo_arg = early_args.repo.unwrap_or(Vec::new());
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   221
    let repo_path: Option<PathBuf> = {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   222
        if repo_arg.is_empty() {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   223
            None
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   224
        } else {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   225
            let local_config = {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   226
                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
   227
                    // 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
   228
                    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
   229
                        let config_files = vec![
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   230
                            ConfigSource::AbsPath(
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   231
                                current_dir_path.join(".hg/hgrc"),
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   232
                            ),
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   233
                            ConfigSource::AbsPath(
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   234
                                current_dir_path.join(".hg/hgrc-not-shared"),
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   235
                            ),
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   236
                        ];
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   237
                        // TODO: handle errors from
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   238
                        // `load_from_explicit_sources`
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   239
                        Config::load_from_explicit_sources(config_files).ok()
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   240
                    } else {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   241
                        None
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   242
                    }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   243
                } else {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   244
                    None
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   245
                }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   246
            };
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   247
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   248
            let non_repo_config_val = {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   249
                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
   250
                match &non_repo_val {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   251
                    Some(val) if val.len() > 0 => home::home_dir()
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   252
                        .unwrap_or_else(|| PathBuf::from("~"))
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   253
                        .join(get_path_from_bytes(val))
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   254
                        .canonicalize()
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   255
                        // 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
   256
                        // implementation maybe?
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   257
                        .ok(),
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   258
                    _ => None,
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   259
                }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   260
            };
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   261
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   262
            let config_val = match &local_config {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   263
                None => non_repo_config_val,
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   264
                Some(val) => {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   265
                    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
   266
                    match &local_config_val {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   267
                        Some(val) if val.len() > 0 => {
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   268
                            // presence of a local_config assures that
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   269
                            // current_dir
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   270
                            // wont result in an Error
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   271
                            let canpath = hg::utils::current_dir()
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   272
                                .unwrap()
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
                            canpath.ok().or(non_repo_config_val)
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   276
                        }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   277
                        _ => non_repo_config_val,
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   278
                    }
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
            config_val.or(Some(get_path_from_bytes(&repo_arg).to_path_buf()))
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   282
        }
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   283
    };
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   284
ebdef6283798 rhg: read [paths] for `--repository` value
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46822
diff changeset
   285
    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
   286
    {
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   287
        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
   288
        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
   289
            // 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
   290
            Err(NoRepoInCwdError { cwd: at })
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   291
        }
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   292
        Err(error) => exit(
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   293
            &initial_current_dir,
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   294
            &ui,
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   295
            OnUnsupported::from_config(&non_repo_config),
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   296
            Err(error.into()),
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   297
            // 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
   298
            // `get_bool` returns an error
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   299
            non_repo_config
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   300
                .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
   301
                .unwrap_or(false),
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   302
        ),
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   303
    };
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   304
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   305
    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
   306
        repo.config()
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   307
    } else {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   308
        &non_repo_config
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   309
    };
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   310
    let on_unsupported = OnUnsupported::from_config(config);
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   311
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   312
    let result = main_with_result(
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   313
        &process_start_time,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   314
        &ui,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   315
        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
   316
        config,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   317
    );
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   318
    exit(
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   319
        &initial_current_dir,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   320
        &ui,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   321
        on_unsupported,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   322
        result,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   323
        // TODO: show a warning or combine with original error if `get_bool`
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   324
        // returns an error
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   325
        config
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   326
            .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
   327
            .unwrap_or(false),
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   328
    )
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   329
}
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   330
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   331
fn exit_code(
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   332
    result: &Result<(), CommandError>,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   333
    use_detailed_exit_code: bool,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   334
) -> i32 {
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   335
    match result {
47407
6e49769b7f97 rhg: add exit code to HgError::Abort()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47406
diff changeset
   336
        Ok(()) => exit_codes::OK,
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   337
        Err(CommandError::Abort {
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   338
            message: _,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   339
            detailed_exit_code,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   340
        }) => {
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   341
            if use_detailed_exit_code {
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   342
                *detailed_exit_code
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   343
            } else {
47407
6e49769b7f97 rhg: add exit code to HgError::Abort()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47406
diff changeset
   344
                exit_codes::ABORT
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   345
            }
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   346
        }
47407
6e49769b7f97 rhg: add exit code to HgError::Abort()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47406
diff changeset
   347
        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
   348
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45923
diff changeset
   349
        // 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
   350
        // 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
   351
        Err(CommandError::UnsupportedFeature { .. }) => {
47407
6e49769b7f97 rhg: add exit code to HgError::Abort()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47406
diff changeset
   352
            exit_codes::UNIMPLEMENTED
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   353
        }
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46596
diff changeset
   354
    }
44981
cf04f62d1579 rhg: add rhg crate
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   355
}
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   356
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   357
fn exit(
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   358
    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
   359
    ui: &Ui,
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   360
    mut on_unsupported: OnUnsupported,
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   361
    result: Result<(), CommandError>,
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   362
    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
   363
) -> ! {
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   364
    if let (
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   365
        OnUnsupported::Fallback { executable },
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   366
        Err(CommandError::UnsupportedFeature { .. }),
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   367
    ) = (&on_unsupported, &result)
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   368
    {
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   369
        let mut args = std::env::args_os();
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   370
        let executable = match executable {
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   371
            None => {
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   372
                exit_no_fallback(
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   373
                    ui,
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   374
                    OnUnsupported::Abort,
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   375
                    Err(CommandError::abort(
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   376
                        "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
   377
                                'rhg.fallback-executable' set.",
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   378
                    )),
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   379
                    false,
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   380
                );
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   381
            }
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   382
            Some(executable) => executable,
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   383
        };
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   384
        let executable_path = get_path_from_bytes(&executable);
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   385
        let this_executable = args.next().expect("exepcted argv[0] to exist");
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   386
        if executable_path == &PathBuf::from(this_executable) {
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   387
            // 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
   388
            // exhaustion.
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   389
            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
   390
                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
   391
                points to `rhg` itself.\n",
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   392
                executable
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   393
            ));
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   394
            on_unsupported = OnUnsupported::Abort
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   395
        } else {
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   396
            // `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
   397
            // `argv[0]`
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   398
            let mut command = Command::new(executable_path);
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   399
            command.args(args);
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   400
            if let Some(initial) = initial_current_dir {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   401
                command.current_dir(initial);
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   402
            }
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   403
            let result = command.status();
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   404
            match result {
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   405
                Ok(status) => std::process::exit(
47407
6e49769b7f97 rhg: add exit code to HgError::Abort()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 47406
diff changeset
   406
                    status.code().unwrap_or(exit_codes::ABORT),
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   407
                ),
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   408
                Err(error) => {
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   409
                    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
   410
                        b"tried to fall back to a '{}' sub-process but got error {}\n",
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   411
                        executable, format_bytes::Utf8(error)
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   412
                    ));
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   413
                    on_unsupported = OnUnsupported::Abort
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   414
                }
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   415
            }
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   416
        }
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   417
    }
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   418
    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
   419
}
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   420
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   421
fn exit_no_fallback(
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   422
    ui: &Ui,
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   423
    on_unsupported: OnUnsupported,
bde90e9b4507 rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents: 46746
diff changeset
   424
    result: Result<(), CommandError>,
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   425
    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
   426
) -> ! {
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   427
    match &result {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   428
        Ok(_) => {}
46744
b1f2c2b336ec rhg: `cat` command: print error messages for missing files
Simon Sapin <simon.sapin@octobus.net>
parents: 46736
diff changeset
   429
        Err(CommandError::Unsuccessful) => {}
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   430
        Err(CommandError::Abort {
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   431
            message,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   432
            detailed_exit_code: _,
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   433
        }) => {
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   434
            if !message.is_empty() {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   435
                // Ignore errors when writing to stderr, we’re already exiting
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   436
                // with failure code so there’s not much more we can do.
46731
3d692e724d06 rhg: Align config file parse error formatting with Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46729
diff changeset
   437
                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
   438
            }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   439
        }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   440
        Err(CommandError::UnsupportedFeature { message }) => {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   441
            match on_unsupported {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   442
                OnUnsupported::Abort => {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   443
                    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
   444
                        b"unsupported feature: {}\n",
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   445
                        message
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   446
                    ));
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   447
                }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   448
                OnUnsupported::AbortSilent => {}
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   449
                OnUnsupported::Fallback { .. } => unreachable!(),
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   450
            }
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   451
        }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   452
    }
46820
821929d59e01 rhg: add support for detailed exit code for ConfigParseError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46748
diff changeset
   453
    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
   454
}
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   455
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   456
macro_rules! subcommands {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   457
    ($( $command: ident )+) => {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   458
        mod commands {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   459
            $(
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   460
                pub mod $command;
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   461
            )+
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   462
        }
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   463
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   464
        fn add_subcommand_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   465
            app
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   466
            $(
46608
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46601
diff changeset
   467
                .subcommand(commands::$command::args())
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   468
            )+
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   469
        }
46484
a6e4e4650bac rhg: Parse system and user configuration at program start
Simon Sapin <simon.sapin@octobus.net>
parents: 46445
diff changeset
   470
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   471
        pub type RunFn = fn(&CliInvocation) -> Result<(), CommandError>;
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   472
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   473
        fn subcommand_run_fn(name: &str) -> Option<RunFn> {
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   474
            match name {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   475
                $(
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   476
                    stringify!($command) => Some(commands::$command::run),
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   477
                )+
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   478
                _ => None,
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   479
            }
45542
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45537
diff changeset
   480
        }
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   481
    };
45529
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   482
}
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   483
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   484
subcommands! {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   485
    cat
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   486
    debugdata
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   487
    debugrequirements
48355
6d4daf51283c rhg: implement the debugignorerhg subcommand
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48353
diff changeset
   488
    debugignorerhg
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   489
    files
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   490
    root
46505
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46504
diff changeset
   491
    config
46822
c71e8d9e7f2a rhg: Initial support for the 'status' command
Georges Racinet <georges.racinet@octobus.net>
parents: 46820
diff changeset
   492
    status
46502
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46501
diff changeset
   493
}
46822
c71e8d9e7f2a rhg: Initial support for the 'status' command
Georges Racinet <georges.racinet@octobus.net>
parents: 46820
diff changeset
   494
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   495
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
   496
    ui: &'a Ui,
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46591
diff changeset
   497
    subcommand_args: &'a ArgMatches<'a>,
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   498
    config: &'a Config,
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   499
    /// 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
   500
    /// `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
   501
    /// `Result` type is `Copy`.
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   502
    repo: Result<&'a Repo, &'a NoRepoInCwdError>,
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   503
}
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   504
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   505
struct NoRepoInCwdError {
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   506
    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
   507
}
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   508
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   509
/// 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
   510
/// 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
   511
/// 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
   512
///
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   513
/// 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
   514
/// 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
   515
struct EarlyArgs {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   516
    /// 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
   517
    config: Vec<Vec<u8>>,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   518
    /// 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
   519
    repo: Option<Vec<u8>>,
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   520
    /// Value of the `--cwd` argument, if any.
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   521
    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
   522
}
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   523
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   524
impl EarlyArgs {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   525
    fn parse(args: impl IntoIterator<Item = OsString>) -> Self {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   526
        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
   527
        let mut config = Vec::new();
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   528
        let mut repo = None;
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   529
        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
   530
        // 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
   531
        // `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
   532
        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
   533
            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
   534
                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
   535
                    config.push(value)
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   536
                }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   537
            } 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
   538
                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
   539
            }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   540
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   541
            if arg == b"--cwd" {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   542
                if let Some(value) = args.next() {
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   543
                    cwd = Some(value)
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   544
                }
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   545
            } 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
   546
                cwd = Some(value.to_owned())
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   547
            }
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   548
46665
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   549
            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
   550
                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
   551
                    repo = Some(value)
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   552
                }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46608
diff changeset
   553
            } 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
   554
                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
   555
            } 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
   556
                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
   557
            }
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   558
        }
46736
2255e7eb39e5 rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents: 46734
diff changeset
   559
        Self { config, repo, cwd }
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   560
    }
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
   561
}
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   562
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   563
/// 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
   564
///
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   565
/// 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
   566
enum OnUnsupported {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   567
    /// 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
   568
    /// and exit with code 252.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   569
    Abort,
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   570
    /// Silently exit with code 252.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   571
    AbortSilent,
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   572
    /// 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
   573
    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
   574
}
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   575
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   576
impl OnUnsupported {
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   577
    const DEFAULT: Self = OnUnsupported::Abort;
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   578
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   579
    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
   580
        match config
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   581
            .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
   582
            .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
   583
            .as_deref()
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   584
        {
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   585
            Some(b"abort") => OnUnsupported::Abort,
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   586
            Some(b"abort-silent") => OnUnsupported::AbortSilent,
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   587
            Some(b"fallback") => OnUnsupported::Fallback {
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   588
                executable: config
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   589
                    .get(b"rhg", b"fallback-executable")
48353
8960295b9246 rhg: only complain about poorly configured fallback when falling back
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48340
diff changeset
   590
                    .map(|x| x.to_owned()),
46667
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   591
            },
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
   592
            None => Self::DEFAULT,
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   593
            Some(_) => {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   594
                // 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
   595
                Self::DEFAULT
46666
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   596
            }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   597
        }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   598
    }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46665
diff changeset
   599
}
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   600
48409
005ae1a343f8 rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48355
diff changeset
   601
const SUPPORTED_EXTENSIONS: &[&[u8]] =
005ae1a343f8 rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48355
diff changeset
   602
    &[b"blackbox", b"share", b"sparse", b"narrow"];
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   603
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   604
fn check_extensions(config: &Config) -> Result<(), CommandError> {
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   605
    let enabled = config.get_section_keys(b"extensions");
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   606
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   607
    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
   608
    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
   609
        unsupported.remove(supported);
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   610
    }
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   611
47951
cff41e168c25 rhg: Switch rhg.ignored-extensions config to Python-compatible list syntax
Simon Sapin <simon.sapin@octobus.net>
parents: 47407
diff changeset
   612
    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
   613
    {
1a036d33bc18 rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 46733
diff changeset
   614
        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
   615
            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
   616
        }
1a036d33bc18 rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 46733
diff changeset
   617
    }
1a036d33bc18 rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 46733
diff changeset
   618
46733
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   619
    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
   620
        Ok(())
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   621
    } else {
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   622
        Err(CommandError::UnsupportedFeature {
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   623
            message: format_bytes!(
46734
1a036d33bc18 rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 46733
diff changeset
   624
                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
   625
                join(unsupported, b", ")
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   626
            ),
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   627
        })
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   628
    }
1bac7764ceef rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents: 46731
diff changeset
   629
}
48336
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   630
48340
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   631
fn check_unsupported(
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   632
    config: &Config,
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   633
    ui: &ui::Ui,
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   634
) -> Result<(), CommandError> {
48336
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   635
    check_extensions(config)?;
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   636
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   637
    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
   638
        // 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
   639
        // What about relative v.s. absolute paths?
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   640
        Err(CommandError::unsupported("$HG_PENDING"))?
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   641
    }
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   642
48339
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   643
    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
   644
        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
   645
    }
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   646
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   647
    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
   648
        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
   649
    }
a2e278b5e265 rhg: [encode] and [decode] config sections are not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48336
diff changeset
   650
48340
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   651
    if let Some(color) = config.get(b"ui", b"color") {
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   652
        if (color == b"always" || color == b"debug") && !ui.plain() {
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   653
            Err(CommandError::unsupported("colored output"))?
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   654
        }
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   655
    }
d71b9902e2de rhg: Colored output is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48339
diff changeset
   656
48336
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   657
    Ok(())
5b9865032533 rhg: $HG_PENDING is not supported
Simon Sapin <simon.sapin@octobus.net>
parents: 48174
diff changeset
   658
}