aboutsummaryrefslogtreecommitdiff
path: root/src/git_tools.rs
blob: 1c361cad47e985ea5425d55faf64f2d10e2acd3e (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::io::{Result, Error, ErrorKind};
use std::process::Command;
use std::path::Path;

pub fn git_save_previous_branch(path: &Path) -> Result<String> {
    let output = Command::new("git")
        .arg("rev-parse")
        .arg("--abbrev-ref")
        .arg("HEAD")
        .current_dir(path)
        .output()?;
    if !output.status.success() {
        Err(Error::new(ErrorKind::Other, "Failed to get current branch"))
    } else {
        Ok(String::from_utf8(output.stdout).unwrap().trim().to_string())
    }
}

pub fn git_checkout(path: &Path, branch: &str) -> Result<()> {
    let output = Command::new("git")
        .arg("checkout")
        .arg(branch)
        .current_dir(path)
        .output()?;
    if !output.status.success() {
        Err(Error::new(ErrorKind::Other, format!("Failed to checkout {}", branch)))
    } else {
        Ok(())
    }
}

pub fn git_fetch(path: &Path) -> Result<()> {
    let output = Command::new("git")
        .arg("fetch")
        .current_dir(path)
        .output()?;
    if !output.status.success() {
        return Err(Error::new(ErrorKind::Other, "Failed to fetch"));
    }
    Ok(())
}

pub fn git_check_for_conflicts(path: &Path) -> Result<bool> {
    let merge_output = Command::new("git")
        .arg("merge")
        .arg("--no-commit")
        .arg("--no-ff")
        .arg("origin/main")
        .current_dir(path)
        .output()?;
    if !merge_output.status.success() {
        return Err(Error::new(ErrorKind::Other, "Failed to merge"));
    }
    let conflicts_output = Command::new("git")
        .arg("ls-files")
        .arg("--unmerged")
        .current_dir(path)
        .output()?;
    Command::new("git")
        .arg("merge")
        .arg("--abort")
        .current_dir(path)
        .output()?;
    Ok(!conflicts_output.stdout.is_empty())
}

pub fn git_merge(path: &Path) -> Result<()> {
    let output = Command::new("git")
        .arg("merge")
        .arg("origin/main")
        .current_dir(path)
        .output()?;
    if !output.status.success() {
        Err(Error::new(ErrorKind::Other, "Failed to merge"))
    } else {
        Ok(())
    }
}

pub fn git_push(path: &Path) -> Result<()> {
    let output = Command::new("git")
        .arg("push")
        .arg("origin")
        .arg("main")
        .current_dir(path)
        .output()?;
    if !output.status.success() {
        Err(Error::new(ErrorKind::Other, "Failed to push to origin"))
    } else {
        Ok(())
    }
}

pub fn get_commit(path: &Path, revision: &str) -> Result<String> {
    let output = Command::new("git")
        .arg("rev-parse")
        .arg("--short")
        .arg(revision)
        .current_dir(path)
        .output()?;
    if !output.status.success() {
        Err(Error::new(ErrorKind::Other, "Failed to get current commit"))
    } else {
        Ok(String::from_utf8(output.stdout).unwrap().trim().to_string())
    }
}