Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use ::std::{
clone::Clone,
collections::{BTreeSet, HashMap},
convert::{AsRef, Into},
format,
iter::{IntoIterator, Iterator},
option::Option::{self, Some},
result::Result::{Err, Ok},
Expand Down Expand Up @@ -37,34 +36,36 @@ pub struct ExecStatus {
}

impl ExecStatus {
pub fn new(exitcode: i32, stdout: String, stderr: String) -> Self {
ExecStatus {
exitcode,
stdout,
stderr,
}
}

pub fn from(exitcode: i32, stdout: Vec<u8>, stderr: Vec<u8>) -> Self {
pub fn new(exitcode: i32, stdout: Vec<u8>, stderr: Vec<u8>) -> Self {
// TODO: use OsString.
ExecStatus {
exitcode,
stdout: String::from_utf8_lossy(stdout.as_slice()).to_string(),
stderr: String::from_utf8_lossy(stderr.as_slice()).to_string(),
}
}

pub fn result(self) -> Result<()> {
match self.exitcode {
0 => Ok(()),
_ => Err(self.into()),
}
}
}

pub trait Git {
fn exec(&self, args: &[&str]) -> Result<ExecStatus>;

fn exec_log(&self, args: &[&str]) -> Result<()> {
match self.exec(args) {
Ok(_) => {
::std::eprintln!("[OK] git {:?}", args);
Ok(())
}
Err(err) => match err.downcast::<ExecStatus>() {
Ok(status) => {
::std::assert_ne!(status.exitcode, 0);
::std::eprintln!("[ERR {:?}] git {:?}", status.exitcode, args);
Err(status.into())
}
Err(err) => Err(err),
},
}
}

fn snapshot(&self) -> Result<Repository> {
let status = self.exec(&["for-each-ref", "--format", FIELD_FORMATS.join(",").as_str()])?;
let refs = parse_ref(status.stdout.as_bytes())?;
Expand All @@ -81,26 +82,27 @@ pub trait Git {
}

fn create_branch(&self, name: &BranchName, base: &BranchName) -> Result<()> {
self.exec(&["branch", "--create-reflog", name.as_str(), base.as_str()])
self.exec_log(&["branch", "--create-reflog", name.as_str(), base.as_str()])
.map(|_| {})
}

fn switch_branch(&self, b: &BranchName) -> Result<()> {
self.exec(&["switch", "--no-guess", b.as_str()]).map(|_| {})
self.exec_log(&["switch", "--no-guess", b.as_str()])
.map(|_| {})
}

fn create_symref(&self, name: &RefName, target: &RefName, reason: &'static str) -> Result<()> {
self.exec(&["symbolic-ref", "-m", reason, name.as_str(), target.as_str()])
self.exec_log(&["symbolic-ref", "-m", reason, name.as_str(), target.as_str()])
.map(|_| {})
}

fn delete_symref(&self, name: &RefName) -> Result<()> {
self.exec(&["symbolic-ref", "--delete", name.as_str()])
self.exec_log(&["symbolic-ref", "--delete", name.as_str()])
.map(|_| {})
}

fn create_ref(&self, name: &RefName, commit: &ObjectName) -> Result<()> {
self.exec(&[
self.exec_log(&[
"update-ref",
"--no-deref",
"--create-reflog",
Expand All @@ -117,7 +119,7 @@ pub trait Git {
new_commit: &ObjectName,
cur_commit: &ObjectName,
) -> Result<()> {
self.exec(&[
self.exec_log(&[
"update-ref",
"--no-deref",
"--create-reflog",
Expand All @@ -129,7 +131,7 @@ pub trait Git {
}

fn delete_ref(&self, name: &RefName, cur_commit: &ObjectName) -> Result<()> {
self.exec(&[
self.exec_log(&[
"update-ref",
"--no-deref",
"-d",
Expand All @@ -140,7 +142,7 @@ pub trait Git {
}

fn rebase_onto(&self, name: &BranchName) -> Result<()> {
self.exec(&[
self.exec_log(&[
"rebase",
"--committer-date-is-author-date",
"--onto",
Expand All @@ -152,27 +154,28 @@ pub trait Git {
}

fn push(&self, name: &BranchName, remote: &RemoteName, expect: &ObjectName) -> Result<()> {
self.exec(&[
self.exec_log(&[
"push",
"--set-upstream",
format!("--force-with-lease={}:{}", name.as_str(), expect.as_str()).as_str(),
::std::format!("--force-with-lease={}:{}", name.as_str(), expect.as_str()).as_str(),
remote.as_str(),
format!("{}:{}", name.as_str(), name.as_str()).as_str(),
::std::format!("{}:{}", name.as_str(), name.as_str()).as_str(),
])
.map(|_| {})
}

fn config_set(&self, key: &str, value: &str) -> Result<()> {
self.exec(&["config", "--local", key, value]).map(|_| {})
self.exec_log(&["config", "--local", key, value])
.map(|_| {})
}

fn config_add(&self, key: &str, value: &str) -> Result<()> {
self.exec(&["config", "--local", "--add", key, value])
self.exec_log(&["config", "--local", "--add", key, value])
.map(|_| {})
}

fn config_unset_pattern(&self, key: &str, pattern: &str) -> Result<()> {
match self.exec(&[
match self.exec_log(&[
"config",
"--local",
"--fixed-value",
Expand All @@ -190,7 +193,7 @@ pub trait Git {
}

fn fetch_all_prune(&self) -> Result<()> {
self.exec(&["fetch", "--all", "--prune"]).map(|_| {})
self.exec_log(&["fetch", "--all", "--prune"]).map(|_| {})
}

fn forkpoint(&self, base: &RefName, branch: &RefName) -> Result<ObjectName> {
Expand Down
17 changes: 6 additions & 11 deletions src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::git;
use ::anyhow::Result;
use ::std::{
assert_ne,
collections::HashMap,
convert::Into,
option::Option::Some,
Expand All @@ -28,26 +27,22 @@ impl<'a> Runner<'a> {

impl<'a> git::Git for Runner<'a> {
fn exec(&self, args: &[&str]) -> Result<git::ExecStatus> {
let cmd = Command::new(self.gitpath)
let output = Command::new(self.gitpath)
.args(args)
.current_dir(&self.workdir)
.envs(self.env.iter())
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
.output()?;

let output = cmd.wait_with_output()?;
if output.status.success() {
::std::eprintln!("[OK] git {:?}", args);
Ok(git::ExecStatus::from(0, output.stdout, output.stderr))
Ok(git::ExecStatus::new(0, output.stdout, output.stderr))
} else if let Some(code) = output.status.code() {
assert_ne!(code, 0);
::std::eprintln!("[ERR {:?}] git {:?}", code, args);
Err(git::ExecStatus::from(code, output.stdout, output.stderr).into())
::std::assert_ne!(code, 0);
Err(git::ExecStatus::new(code, output.stdout, output.stderr).into())
} else {
::std::eprintln!("[ERR] git {:?}", args);
Err(git::ExecStatus::from(1, output.stdout, output.stderr).into())
Err(git::ExecStatus::new(1, output.stdout, output.stderr).into())
}
}
}