-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit.rs
More file actions
35 lines (29 loc) · 819 Bytes
/
Copy pathgit.rs
File metadata and controls
35 lines (29 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use anyhow::Result;
use std::path::Path;
use std::process::Command;
/// Check if directory is a valid git repository.
pub fn is_git_repo() -> bool {
Command::new("git")
.args(["rev-parse", "--is-inside-work-tree"])
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
/// Check if .git directory exists.
fn git_dir_exists() -> bool {
Path::new(".git").exists()
}
/// Initialize git repository if not already a git repo.
pub fn init_if_needed() -> Result<bool> {
if git_dir_exists() {
return Ok(false);
}
Command::new("git")
.arg("init")
.output()
.map_err(|e| anyhow::anyhow!("Failed to run 'git init': {}", e))?;
if !is_git_repo() {
anyhow::bail!("git init succeeded but .git not found");
}
Ok(true)
}