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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
#[macro_use] extern crate log; use std::io::Result; use std::io::Error; use std::io::ErrorKind; use std::path::Path; use std::process::Command; pub fn get_current_commitid_for_repo(folder: &Path) -> Result<String> { let output = Command::new("git").arg("rev-parse") .arg("HEAD") .current_dir(folder) .output()?; trace!("output of current commit command: {:#?}", output); if output.status.success() { Ok(format!("{}", String::from_utf8_lossy(&output.stdout))) } else { Err(Error::new(ErrorKind::Other, format!("{}", String::from_utf8_lossy(&output.stderr)))) } } pub fn clone(folder: &Path, remote: &str) -> Result<String> { let output = Command::new("git").arg("clone") .arg(remote) .arg(folder) .output()?; trace!("output of clone command: {:#?}", output); if output.status.success() { Ok(format!("{}", String::from_utf8_lossy(&output.stdout))) } else { Err(Error::new(ErrorKind::Other, format!("{}", String::from_utf8_lossy(&output.stderr)))) } } #[test] fn test_git_clone() { use std::path::PathBuf; let out = assert!(clone(PathBuf::from("/tmp/git_test_clone").as_path(), ".").is_ok()); std::fs::remove_dir_all("/tmp/git_test_clone").unwrap(); out } pub fn add(folder: &Path, file: &Path) -> Result<String> { let output = Command::new("git").arg("add") .arg(file) .current_dir(folder) .output()?; trace!("output of add command: {:#?}", output); if output.status.success() { Ok(format!("{}", String::from_utf8_lossy(&output.stdout))) } else { Err(Error::new(ErrorKind::Other, format!("{}", String::from_utf8_lossy(&output.stderr)))) } } pub fn commit(folder: &Path, message: &str) -> Result<String> { let output = Command::new("git").arg("commit") .arg("-m") .arg(message) .current_dir(folder) .output()?; trace!("output of commit command: {:#?}", output); if output.status.success() { Ok(format!("{}", String::from_utf8_lossy(&output.stdout))) } else { Err(Error::new(ErrorKind::Other, format!("{}", String::from_utf8_lossy(&output.stderr)))) } } pub fn status(folder: &Path) -> Result<String> { let output = Command::new("git").arg("status") .current_dir(folder) .output()?; trace!("output of init command: {:#?}", output); if output.status.success() { Ok(format!("{}", String::from_utf8_lossy(&output.stdout))) } else { Err(Error::new(ErrorKind::Other, format!("{}", String::from_utf8_lossy(&output.stderr)))) } } pub fn init(folder: &Path) -> Result<String> { let output = Command::new("git").arg("init") .current_dir(folder) .output()?; trace!("output of init command: {:#?}", output); if output.status.success() { Ok(format!("{}", String::from_utf8_lossy(&output.stdout))) } else { Err(Error::new(ErrorKind::Other, format!("{}", String::from_utf8_lossy(&output.stderr)))) } } pub fn pull(folder: &Path) -> Result<String> { debug!("pulling git repo in folder {:#?}", folder); let output = Command::new("git").arg("pull") .current_dir(folder) .output()?; trace!("output of pull command: {:#?}", output); if output.status.success() { Ok(format!("{}", String::from_utf8_lossy(&output.stdout))) } else { Err(Error::new(ErrorKind::Other, format!("{}", String::from_utf8_lossy(&output.stderr)))) } } pub fn push(folder: &Path) -> Result<String> { debug!("pushing git repo in folder {:#?}", folder); let output = Command::new("git").arg("push") .current_dir(folder) .output()?; trace!("output of push command: {:#?}", output); if output.status.success() { Ok(format!("{}", String::from_utf8_lossy(&output.stdout))) } else { Err(Error::new(ErrorKind::Other, format!("{}", String::from_utf8_lossy(&output.stderr)))) } } pub fn sync(folder: &Path) -> Result<String> { pull(folder)?; push(folder) }