Skip to main content

create_lamdera_app_rs/
git.rs

1use crate::error::Result;
2use colored::*;
3#[cfg(unix)]
4use std::fs;
5#[cfg(unix)]
6use std::os::unix::fs::PermissionsExt;
7use std::path::Path;
8use std::process::Command;
9
10pub fn init_repository(project_path: &Path) -> Result<()> {
11    let status = Command::new("git")
12        .arg("init")
13        .current_dir(project_path)
14        .status()?;
15
16    if !status.success() {
17        return Err(anyhow::anyhow!("Failed to initialize git repository"));
18    }
19
20    println!("{}", "✓ Initialized git repository".green());
21    Ok(())
22}
23
24pub fn add_remote(project_path: &Path, remote_url: &str) -> Result<()> {
25    let status = Command::new("git")
26        .args(["remote", "add", "origin", remote_url])
27        .current_dir(project_path)
28        .status()?;
29
30    if !status.success() {
31        return Err(anyhow::anyhow!("Failed to add git remote"));
32    }
33
34    Ok(())
35}
36
37pub fn initial_commit(project_path: &Path) -> Result<()> {
38    // Add all files
39    Command::new("git")
40        .args(["add", "."])
41        .current_dir(project_path)
42        .status()?;
43
44    // Create initial commit
45    let status = Command::new("git")
46        .args(["commit", "-m", "Initial commit"])
47        .current_dir(project_path)
48        .status()?;
49
50    if !status.success() {
51        return Err(anyhow::anyhow!("Failed to create initial commit"));
52    }
53
54    println!("{}", "✓ Created initial commit".green());
55    Ok(())
56}
57
58pub fn push_to_remote(project_path: &Path) -> Result<()> {
59    let status = Command::new("git")
60        .args(["push", "-u", "origin", "main"])
61        .current_dir(project_path)
62        .status()?;
63
64    if !status.success() {
65        return Err(anyhow::anyhow!("Failed to push to remote"));
66    }
67
68    println!("{}", "✓ Pushed to remote repository".green());
69    Ok(())
70}
71
72pub fn make_hooks_executable(project_path: &Path) -> Result<()> {
73    #[cfg(unix)]
74    {
75        let hook_path = project_path.join(".githooks/pre-commit");
76        if hook_path.exists() {
77            let mut permissions = fs::metadata(&hook_path)?.permissions();
78            permissions.set_mode(permissions.mode() | 0o111);
79            fs::set_permissions(&hook_path, permissions)?;
80        }
81    }
82
83    Ok(())
84}
85
86pub fn configure_hooks(project_path: &Path) -> Result<()> {
87    make_hooks_executable(project_path)?;
88
89    let status = Command::new("git")
90        .args(["config", "core.hooksPath", ".githooks"])
91        .current_dir(project_path)
92        .status()?;
93
94    if status.success() {
95        println!("{}", "✓ Configured git hooks".green());
96    }
97
98    Ok(())
99}