Skip to content

Commit

Permalink
git: force git subprocess to not localize error messages
Browse files Browse the repository at this point in the history
Since we parse error messages, we need to disable translation at all.
  • Loading branch information
yuja committed Feb 5, 2025
1 parent f673733 commit e091172
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cli/tests/test_git_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::path::Path;

use test_case::test_case;

use crate::common::get_stderr_string;
use crate::common::TestEnvironment;

fn add_commit_to_branch(git_repo: &git2::Repository, branch: &str) -> git2::Oid {
Expand Down Expand Up @@ -1147,6 +1148,34 @@ fn test_git_fetch_bookmarks_some_missing(subprocess: bool) {
}
}

#[test]
fn test_git_fetch_bookmarks_missing_with_subprocess_localized_message() {
let test_env = TestEnvironment::default();
test_env.add_config("git.subprocess = true");
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");
add_git_remote(&test_env, &repo_path, "origin");

// "fatal: couldn't find remote ref %s" shouldn't be localized.
let assert = test_env
.jj_cmd(&repo_path, &["git", "fetch", "--branch=unknown"])
// Initialize locale as "en_US" which is the most common.
.env("LC_ALL", "en_US.UTF-8")
// Set some other locale variables for testing.
.env("LC_MESSAGES", "en_US.UTF-8")
.env("LANG", "en_US.UTF-8")
// GNU gettext prioritizes LANGUAGE if translation is enabled. It works
// no matter if system locale exists or not.
.env("LANGUAGE", "zh_TW")
.assert()
.success();
let stderr = test_env.normalize_output(&get_stderr_string(&assert));
insta::assert_snapshot!(stderr, @r"
Warning: No branch matching `unknown` found on any specified/configured remote
Nothing changed.
");
}

// See `test_undo_restore_commands.rs` for fetch-undo-push and fetch-undo-fetch
// of the same bookmarks for various kinds of undo.
#[test_case(false; "use git2 for remote calls")]
Expand Down
3 changes: 3 additions & 0 deletions lib/src/git_subprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ impl<'a> GitSubprocessContext<'a> {
.arg("--bare")
.arg("--git-dir")
.arg(&self.git_dir)
// Disable translation and other locale-dependent behavior so we can
// parse the output. LC_ALL precedes LC_* and LANG.
.env("LC_ALL", "C")
.stdin(Stdio::null())
.stderr(Stdio::piped());

Expand Down

0 comments on commit e091172

Please sign in to comment.