Skip to content

Commit

Permalink
Apply cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
certik committed Sep 7, 2024
1 parent d3a4b00 commit ba21dc9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
73 changes: 39 additions & 34 deletions crates/deno_task_shell/src/shell/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String, String> {
Expand All @@ -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)
Expand All @@ -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();
}
}
};
}

Expand Down
3 changes: 1 addition & 2 deletions crates/shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit ba21dc9

Please sign in to comment.