-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement command hashing (#206)
* Implement hash builtin * Update simple command resolution to go through hashed paths (when appropriate) * Simple optimization for executable lookup in completion code path
- Loading branch information
Showing
15 changed files
with
363 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ mod factory; | |
mod false_; | ||
mod fg; | ||
mod getopts; | ||
mod hash; | ||
mod help; | ||
mod jobs; | ||
#[cfg(unix)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use clap::Parser; | ||
use std::{io::Write, path::PathBuf}; | ||
|
||
use crate::{builtins, commands}; | ||
|
||
#[derive(Parser)] | ||
pub(crate) struct HashCommand { | ||
/// Remove entries associated with the given names. | ||
#[arg(short = 'd')] | ||
remove: bool, | ||
|
||
/// Display paths in a format usable for input. | ||
#[arg(short = 'l')] | ||
display_as_usable_input: bool, | ||
|
||
/// The path to associate with the names. | ||
#[arg(short = 'p')] | ||
path_to_use: Option<PathBuf>, | ||
|
||
/// Remove all entries. | ||
#[arg(short = 'r')] | ||
remove_all: bool, | ||
|
||
/// Display the paths associated with the names. | ||
#[arg(short = 't')] | ||
display_paths: bool, | ||
|
||
/// Names to process. | ||
names: Vec<String>, | ||
} | ||
|
||
impl builtins::Command for HashCommand { | ||
async fn execute( | ||
&self, | ||
context: commands::ExecutionContext<'_>, | ||
) -> Result<crate::builtins::ExitCode, crate::error::Error> { | ||
let mut result = builtins::ExitCode::Success; | ||
|
||
if self.remove_all { | ||
context.shell.program_location_cache.reset(); | ||
} else if self.remove { | ||
for name in &self.names { | ||
if !context.shell.program_location_cache.unset(name) { | ||
writeln!(context.stderr(), "{name}: not found")?; | ||
result = builtins::ExitCode::Custom(1); | ||
} | ||
} | ||
} else if self.display_paths { | ||
for name in &self.names { | ||
if let Some(path) = context.shell.program_location_cache.get(name) { | ||
if self.display_as_usable_input { | ||
writeln!( | ||
context.stdout(), | ||
"builtin hash -p {} {name}", | ||
path.to_string_lossy() | ||
)?; | ||
} else { | ||
let mut prefix = String::new(); | ||
|
||
if self.names.len() > 1 { | ||
prefix.push_str(name.as_str()); | ||
prefix.push('\t'); | ||
} | ||
|
||
writeln!( | ||
context.stdout(), | ||
"{prefix}{}", | ||
path.to_string_lossy().as_ref() | ||
)?; | ||
} | ||
} else { | ||
writeln!(context.stderr(), "{name}: not found")?; | ||
result = builtins::ExitCode::Custom(1); | ||
} | ||
} | ||
} else if let Some(path) = &self.path_to_use { | ||
for name in &self.names { | ||
context.shell.program_location_cache.set(name, path.clone()); | ||
} | ||
} else { | ||
for name in &self.names { | ||
// Remove from the cache if already hashed. | ||
let _ = context.shell.program_location_cache.unset(name); | ||
|
||
// Hash the path. | ||
if context | ||
.shell | ||
.find_first_executable_in_path_using_cache(name) | ||
.is_none() | ||
{ | ||
writeln!(context.stderr(), "{name}: not found")?; | ||
result = builtins::ExitCode::Custom(1); | ||
} | ||
} | ||
} | ||
|
||
Ok(result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.