Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if the current directory is in a git repo already, and only initialize if not #29

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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