githelper/
lib.rs

1#[macro_use]
2extern crate log;
3
4use std::io::Result;
5use std::io::Error;
6use std::io::ErrorKind;
7use std::path::Path;
8use std::process::Command;
9
10pub fn get_current_commitid_for_repo(folder: &Path) -> Result<String> {
11    let output = Command::new("git").arg("rev-parse")
12        .arg("HEAD")
13        .current_dir(folder)
14        .output()?;
15
16    trace!("output of current commit command: {:#?}", output);
17
18    if output.status.success() {
19        Ok(format!("{}", String::from_utf8_lossy(&output.stdout)))
20    } else {
21        Err(Error::new(ErrorKind::Other,
22                       format!("{}", String::from_utf8_lossy(&output.stderr))))
23    }
24}
25
26pub fn clone(folder: &Path, remote: &str) -> Result<String> {
27    let output = Command::new("git").arg("clone")
28        .arg(remote)
29        .arg(folder)
30        .output()?;
31
32    trace!("output of clone command: {:#?}", output);
33
34    if output.status.success() {
35        Ok(format!("{}", String::from_utf8_lossy(&output.stdout)))
36    } else {
37        Err(Error::new(ErrorKind::Other,
38                       format!("{}", String::from_utf8_lossy(&output.stderr))))
39    }
40}
41
42#[test]
43fn test_git_clone() {
44    use std::path::PathBuf;
45    let out = assert!(clone(PathBuf::from("/tmp/git_test_clone").as_path(), ".").is_ok());
46    std::fs::remove_dir_all("/tmp/git_test_clone").unwrap();
47
48    out
49}
50
51pub fn add(folder: &Path, file: &Path) -> Result<String> {
52    let output = Command::new("git").arg("add")
53        .arg(file)
54        .current_dir(folder)
55        .output()?;
56
57    trace!("output of add command: {:#?}", output);
58
59    if output.status.success() {
60        Ok(format!("{}", String::from_utf8_lossy(&output.stdout)))
61    } else {
62        Err(Error::new(ErrorKind::Other,
63                       format!("{}", String::from_utf8_lossy(&output.stderr))))
64    }
65}
66
67pub fn commit(folder: &Path, message: &str) -> Result<String> {
68    let output = Command::new("git").arg("commit")
69        .arg("-m")
70        .arg(message)
71        .current_dir(folder)
72        .output()?;
73
74    trace!("output of commit command: {:#?}", output);
75
76    if output.status.success() {
77        Ok(format!("{}", String::from_utf8_lossy(&output.stdout)))
78    } else {
79        Err(Error::new(ErrorKind::Other,
80                       format!("{}", String::from_utf8_lossy(&output.stderr))))
81    }
82}
83
84pub fn status(folder: &Path) -> Result<String> {
85    let output = Command::new("git").arg("status")
86        .current_dir(folder)
87        .output()?;
88
89    trace!("output of init command: {:#?}", output);
90
91    if output.status.success() {
92        Ok(format!("{}", String::from_utf8_lossy(&output.stdout)))
93    } else {
94        Err(Error::new(ErrorKind::Other,
95                       format!("{}", String::from_utf8_lossy(&output.stderr))))
96    }
97}
98
99pub fn init(folder: &Path) -> Result<String> {
100    let output = Command::new("git").arg("init")
101        .current_dir(folder)
102        .output()?;
103
104    trace!("output of init command: {:#?}", output);
105
106    if output.status.success() {
107        Ok(format!("{}", String::from_utf8_lossy(&output.stdout)))
108    } else {
109        Err(Error::new(ErrorKind::Other,
110                       format!("{}", String::from_utf8_lossy(&output.stderr))))
111    }
112}
113
114pub fn pull(folder: &Path) -> Result<String> {
115    debug!("pulling git repo in folder {:#?}", folder);
116
117    let output = Command::new("git").arg("pull")
118        .current_dir(folder)
119        .output()?;
120
121    trace!("output of pull command: {:#?}", output);
122
123    if output.status.success() {
124        Ok(format!("{}", String::from_utf8_lossy(&output.stdout)))
125    } else {
126        Err(Error::new(ErrorKind::Other,
127                       format!("{}", String::from_utf8_lossy(&output.stderr))))
128    }
129}
130
131pub fn push(folder: &Path) -> Result<String> {
132    debug!("pushing git repo in folder {:#?}", folder);
133
134    let output = Command::new("git").arg("push")
135        .current_dir(folder)
136        .output()?;
137
138    trace!("output of push command: {:#?}", output);
139
140    if output.status.success() {
141        Ok(format!("{}", String::from_utf8_lossy(&output.stdout)))
142    } else {
143        Err(Error::new(ErrorKind::Other,
144                       format!("{}", String::from_utf8_lossy(&output.stderr))))
145    }
146}
147
148pub fn sync(folder: &Path) -> Result<String> {
149    pull(folder)?;
150    push(folder)
151}