Skip to content

Commit

Permalink
Check if the current directory is in a git repo already, and only ini…
Browse files Browse the repository at this point in the history
…tialize if not
  • Loading branch information
jessebraham committed Oct 30, 2024
1 parent ff981be commit 9e51a14
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{env, path::PathBuf, process};
use std::{
env,
path::{Path, PathBuf},
process,
};

use clap::Parser;
use esp_metadata::Chip;
Expand Down Expand Up @@ -255,16 +259,20 @@ fn main() {
.arg("group_imports=StdExternalCrate")
.arg("--config")
.arg("imports_granularity=Module")
.current_dir(path.join(&args.name))
.current_dir(&dir)
.output()
.unwrap();

// Run git init
process::Command::new("git")
.arg("init")
.current_dir(path.join(&args.name))
.output()
.unwrap();
if should_initialize_git_repo(&dir) {
// Run git init
process::Command::new("git")
.arg("init")
.current_dir(&dir)
.output()
.unwrap();
} else {
eprintln!("Current directory is already in a git repository, skipping git initialization");
}
}

fn process_file(
Expand Down Expand Up @@ -411,6 +419,23 @@ fn process_options(args: &Args) {
}
}

fn should_initialize_git_repo(mut path: &Path) -> bool {
loop {
let dotgit_path = path.join(".git");
if dotgit_path.exists() && dotgit_path.is_dir() {
return false;
}

if let Some(parent) = path.parent() {
path = parent;
} else {
break;
}
}

true
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 9e51a14

Please sign in to comment.