Skip to content

Commit 49d5174

Browse files
committed
feat: auto-initialize git repository in gitkit init
1 parent 8ed5086 commit 49d5174

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/git.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use anyhow::Result;
2+
use std::path::Path;
3+
use std::process::Command;
4+
5+
/// Check if directory is a valid git repository.
6+
pub fn is_git_repo() -> bool {
7+
Command::new("git")
8+
.args(&["rev-parse", "--is-inside-work-tree"])
9+
.output()
10+
.map(|o| o.status.success())
11+
.unwrap_or(false)
12+
}
13+
14+
/// Check if .git directory exists.
15+
fn git_dir_exists() -> bool {
16+
Path::new(".git").exists()
17+
}
18+
19+
/// Initialize git repository if not already a git repo.
20+
pub fn init_if_needed() -> Result<bool> {
21+
if git_dir_exists() {
22+
return Ok(false);
23+
}
24+
25+
Command::new("git")
26+
.arg("init")
27+
.output()
28+
.map_err(|e| anyhow::anyhow!("Failed to run 'git init': {}", e))?;
29+
30+
if !is_git_repo() {
31+
anyhow::bail!("git init succeeded but .git not found");
32+
}
33+
34+
Ok(true)
35+
}

src/init.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::Result;
22
use inquire::{MultiSelect, Text};
33

4-
use crate::{attributes, config, hooks, ignore};
4+
use crate::{attributes, config, git, hooks, ignore};
55

66
const BANNER: &str = r#"
77
███ █████ █████ ███ █████
@@ -18,6 +18,12 @@ const BANNER: &str = r#"
1818
"#;
1919

2020
pub fn run() -> Result<()> {
21+
// Initialize git repository if not already one
22+
let git_initialized = git::init_if_needed()?;
23+
if git_initialized {
24+
println!(" ◇ git repository initialized ✓");
25+
}
26+
2127
println!("{BANNER}");
2228
println!(" Configure your git repo\n");
2329

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clap::{Parser, Subcommand};
33

44
mod attributes;
55
mod config;
6+
mod git;
67
mod hooks;
78
mod ignore;
89
mod init;

0 commit comments

Comments
 (0)