From ba21dc9e329c49d2f3d913572f2ee1d698da878d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Sat, 7 Sep 2024 14:49:00 -0600 Subject: [PATCH] Apply `cargo fmt` --- crates/deno_task_shell/src/shell/types.rs | 73 ++++++++++++----------- crates/shell/src/main.rs | 3 +- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/crates/deno_task_shell/src/shell/types.rs b/crates/deno_task_shell/src/shell/types.rs index e1094f5..36c9e28 100644 --- a/crates/deno_task_shell/src/shell/types.rs +++ b/crates/deno_task_shell/src/shell/types.rs @@ -33,9 +33,9 @@ pub struct ShellState { token: CancellationToken, /// Git repository handling. git_repository: bool, // Is `cwd` inside a git repository? - git_root: PathBuf, // Path to the root (`$git_root/.git/HEAD` exists) - git_branch: String, // Contents of `$git_root/.git/HEAD` - last_command_cd: bool,// Was last command a `cd` (thus git_branch is current)? + git_root: PathBuf, // Path to the root (`$git_root/.git/HEAD` exists) + git_branch: String, // Contents of `$git_root/.git/HEAD` + last_command_cd: bool, // Was last command a `cd` (thus git_branch is current)? } impl ShellState { @@ -83,7 +83,7 @@ impl ShellState { } pub fn last_command_cd(&self) -> bool { - self.last_command_cd + self.last_command_cd } pub fn env_vars(&self) -> &HashMap { @@ -109,7 +109,7 @@ impl ShellState { Ok(contents) => { // The git root can still be read, update the git branch self.git_branch = contents.trim().to_string(); - }, + } Err(_) => { // The git root can no longer be read // (the `.git/HEAD` was removed in the meantime) @@ -131,36 +131,41 @@ impl ShellState { // Handle a git repository // First read the current directory's `.git/HEAD` match fs::read_to_string(cwd.join(".git/HEAD")) { - Ok(contents) => { - // We are in a git repository in the git root dir - self.git_repository = true; - self.git_branch = contents.trim().to_string(); - self.git_root = cwd.to_path_buf(); - }, - Err(_) => { - if self.git_repository && cwd.display().to_string().starts_with(&self.git_root.display().to_string()) { - // We moved inside the same git repository, but we are not - // in the git root dir - self.update_git_branch(); - } else { - // We didn't move within the same git repository, - // and there is no `.git` present. - // Consequently, we: - // * Either moved into a subdirectory of a git repository from - // oustide - // * Or moved into a directory that is not inside git repository - // In the first case we need to recursively search to find the - // root. This might be slow, so we want to be smart and use the - // old directory to eliminate search in case we are moving up or - // down from the same root. For now we will set no git - // repository, which is incorrect for the first case, but will - // be fast for the most common use of not being inside a git - // repository. - self.git_repository = false; - self.git_branch = "".to_string(); - self.git_root = "".to_string().into(); - } + Ok(contents) => { + // We are in a git repository in the git root dir + self.git_repository = true; + self.git_branch = contents.trim().to_string(); + self.git_root = cwd.to_path_buf(); + } + Err(_) => { + if self.git_repository + && cwd + .display() + .to_string() + .starts_with(&self.git_root.display().to_string()) + { + // We moved inside the same git repository, but we are not + // in the git root dir + self.update_git_branch(); + } else { + // We didn't move within the same git repository, + // and there is no `.git` present. + // Consequently, we: + // * Either moved into a subdirectory of a git repository from + // oustide + // * Or moved into a directory that is not inside git repository + // In the first case we need to recursively search to find the + // root. This might be slow, so we want to be smart and use the + // old directory to eliminate search in case we are moving up or + // down from the same root. For now we will set no git + // repository, which is incorrect for the first case, but will + // be fast for the most common use of not being inside a git + // repository. + self.git_repository = false; + self.git_branch = "".to_string(); + self.git_root = "".to_string().into(); } + } }; } diff --git a/crates/shell/src/main.rs b/crates/shell/src/main.rs index ef754e1..37a482f 100644 --- a/crates/shell/src/main.rs +++ b/crates/shell/src/main.rs @@ -100,8 +100,7 @@ async fn interactive() -> anyhow::Result<()> { } let mut git_branch: String = "".to_string(); if state.git_repository() { - git_branch = match state.git_branch() - .strip_prefix("ref: refs/heads/") { + git_branch = match state.git_branch().strip_prefix("ref: refs/heads/") { Some(stripped) => stripped.to_string(), None => { let mut hash = state.git_branch().to_string();