]>
Commit | Line | Data |
---|---|---|
7263eddf RBR |
1 | use std::io::{Result, Error, ErrorKind}; |
2 | use std::process::Command; | |
3 | use std::path::Path; | |
4 | ||
5 | pub fn git_save_previous_branch(path: &Path) -> Result<String> { | |
6 | let output = Command::new("git") | |
7 | .arg("rev-parse") | |
8 | .arg("--abbrev-ref") | |
9 | .arg("HEAD") | |
10 | .current_dir(path) | |
11 | .output()?; | |
12 | if !output.status.success() { | |
13 | Err(Error::new(ErrorKind::Other, "Failed to get current branch")) | |
14 | } else { | |
15 | Ok(String::from_utf8(output.stdout).unwrap().trim().to_string()) | |
16 | } | |
17 | } | |
18 | ||
19 | pub fn git_checkout(path: &Path, branch: &str) -> Result<()> { | |
20 | let output = Command::new("git") | |
21 | .arg("checkout") | |
22 | .arg(branch) | |
23 | .current_dir(path) | |
24 | .output()?; | |
25 | if !output.status.success() { | |
26 | Err(Error::new(ErrorKind::Other, format!("Failed to checkout {}", branch))) | |
27 | } else { | |
28 | Ok(()) | |
29 | } | |
30 | } | |
31 | ||
32 | pub fn git_fetch(path: &Path) -> Result<()> { | |
33 | let output = Command::new("git") | |
34 | .arg("fetch") | |
35 | .current_dir(path) | |
36 | .output()?; | |
37 | if !output.status.success() { | |
38 | return Err(Error::new(ErrorKind::Other, "Failed to fetch")); | |
39 | } | |
40 | Ok(()) | |
41 | } | |
42 | ||
43 | pub fn git_check_for_conflicts(path: &Path) -> Result<bool> { | |
44 | let merge_output = Command::new("git") | |
45 | .arg("merge") | |
46 | .arg("--no-commit") | |
47 | .arg("--no-ff") | |
48 | .arg("origin/main") | |
49 | .current_dir(path) | |
50 | .output()?; | |
51 | if !merge_output.status.success() { | |
52 | return Err(Error::new(ErrorKind::Other, "Failed to merge")); | |
53 | } | |
54 | let conflicts_output = Command::new("git") | |
55 | .arg("ls-files") | |
56 | .arg("--unmerged") | |
57 | .current_dir(path) | |
58 | .output()?; | |
59 | Command::new("git") | |
60 | .arg("merge") | |
61 | .arg("--abort") | |
62 | .current_dir(path) | |
63 | .output()?; | |
64 | Ok(!conflicts_output.stdout.is_empty()) | |
65 | } | |
66 | ||
67 | pub fn git_merge(path: &Path) -> Result<()> { | |
68 | let output = Command::new("git") | |
69 | .arg("merge") | |
70 | .arg("origin/main") | |
71 | .current_dir(path) | |
72 | .output()?; | |
73 | if !output.status.success() { | |
74 | Err(Error::new(ErrorKind::Other, "Failed to merge")) | |
75 | } else { | |
76 | Ok(()) | |
77 | } | |
78 | } | |
79 | ||
80 | pub fn git_push(path: &Path) -> Result<()> { | |
81 | let output = Command::new("git") | |
82 | .arg("push") | |
83 | .arg("origin") | |
84 | .arg("main") | |
85 | .current_dir(path) | |
86 | .output()?; | |
87 | if !output.status.success() { | |
88 | Err(Error::new(ErrorKind::Other, "Failed to push to origin")) | |
89 | } else { | |
90 | Ok(()) | |
91 | } | |
92 | } | |
93 | ||
94 | pub fn get_commit(path: &Path, revision: &str) -> Result<String> { | |
95 | let output = Command::new("git") | |
96 | .arg("rev-parse") | |
97 | .arg("--short") | |
98 | .arg(revision) | |
99 | .current_dir(path) | |
100 | .output()?; | |
101 | if !output.status.success() { | |
102 | Err(Error::new(ErrorKind::Other, "Failed to get current commit")) | |
103 | } else { | |
104 | Ok(String::from_utf8(output.stdout).unwrap().trim().to_string()) | |
105 | } | |
106 | } |