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
36 changes: 32 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ keywords = ["git"]
exclude = [".gitignore", ".github", "target"]

[dependencies]
anyhow = "1.0.48"
const_format = "0.2.22"
serde = { version = "1.0.130", features = ["derive"] }
csv = "1.1.6"
thiserror = "1.0.30"

[dependencies.clap]
version = "3.0.0-beta.5"
Expand Down
106 changes: 47 additions & 59 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use ::anyhow::Result;
use ::const_format::concatcp;
use ::csv::ReaderBuilder;
use ::serde::Deserialize;
use ::std::{
self,
borrow::{Cow, ToOwned},
clone::Clone,
collections::{BTreeSet, HashMap},
convert::AsRef,
default::Default,
error::Error,
convert::{AsRef, Into},
format,
iter::{IntoIterator, Iterator},
option::Option::{self, None},
result::Result::{self, Err, Ok},
option::Option::{self, Some},
result::Result::{Err, Ok},
string::{String, ToString},
vec::Vec,
write,
};
use ::thiserror::Error;

pub const NON_EXISTANT_OBJECT: ObjectName<'static> =
ObjectName::new("0000000000000000000000000000000000000000");
Expand All @@ -27,87 +28,78 @@ pub const STC_REMOTE_REF_PREFIX: &str = concatcp!(STC_REF_PREFIX, "remote/");

pub const BRANCH_REF_PREFIX: &str = "refs/heads/";

#[derive(Debug)]
pub struct Status {
#[derive(Error, Debug)]
#[error("exitcode={exitcode:?}, stdout={stdout:?}, stderr={stderr:?}")]
pub struct ExecStatus {
pub exitcode: i32,
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
pub stdout: String,
pub stderr: String,
}

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

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

impl ::std::fmt::Display for Status {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "SuperError is here!")
}
}

impl Error for Status {
fn source(&self) -> Option<&(dyn Error + 'static)> {
// XXX
None //Some(&self.side)
pub fn result(self) -> Result<()> {
match self.exitcode {
0 => Ok(()),
_ => Err(self.into()),
}
}
}

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

fn snapshot(&self) -> Result<Repository, Status> {
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_slice()).map_err(|_err| Status::with(1))?;
let refs = parse_ref(status.stdout.as_bytes())?;
let head = refs
.values()
.find(|r| r.head)
.map(|r| r.name.branchname().owning_clone());
Ok(Repository { refs, head })
}

fn check_branchname<'a>(&self, name: &'a str) -> Result<BranchName<'a>, Status> {
fn check_branchname<'a>(&self, name: &'a str) -> Result<BranchName<'a>> {
self.exec(&["check-ref-format", "--branch", name])?;
Ok(BranchName(Cow::Owned(name.to_string())))
}

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

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

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

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

fn create_ref(&self, name: &RefName, commit: &ObjectName) -> Result<(), Status> {
fn create_ref(&self, name: &RefName, commit: &ObjectName) -> Result<()> {
self.exec(&[
"update-ref",
"--no-deref",
Expand All @@ -124,7 +116,7 @@ pub trait Git {
name: &RefName,
new_commit: &ObjectName,
cur_commit: &ObjectName,
) -> Result<(), Status> {
) -> Result<()> {
self.exec(&[
"update-ref",
"--no-deref",
Expand All @@ -136,7 +128,7 @@ pub trait Git {
.map(|_| {})
}

fn delete_ref(&self, name: &RefName, cur_commit: &ObjectName) -> Result<(), Status> {
fn delete_ref(&self, name: &RefName, cur_commit: &ObjectName) -> Result<()> {
self.exec(&[
"update-ref",
"--no-deref",
Expand All @@ -147,7 +139,7 @@ pub trait Git {
.map(|_| {})
}

fn rebase_onto(&self, name: &BranchName) -> Result<(), Status> {
fn rebase_onto(&self, name: &BranchName) -> Result<()> {
self.exec(&[
"rebase",
"--committer-date-is-author-date",
Expand All @@ -159,12 +151,7 @@ pub trait Git {
.map(|_| {})
}

fn push(
&self,
name: &BranchName,
remote: &RemoteName,
expect: &ObjectName,
) -> Result<(), Status> {
fn push(&self, name: &BranchName, remote: &RemoteName, expect: &ObjectName) -> Result<()> {
self.exec(&[
"push",
"--set-upstream",
Expand All @@ -175,16 +162,16 @@ pub trait Git {
.map(|_| {})
}

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

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

fn config_unset_pattern(&self, key: &str, pattern: &str) -> Result<(), Status> {
fn config_unset_pattern(&self, key: &str, pattern: &str) -> Result<()> {
match self.exec(&[
"config",
"--local",
Expand All @@ -194,22 +181,23 @@ pub trait Git {
pattern,
]) {
// 5 means the nothing matched.
Err(status) if status.exitcode != 5 => Err(status),
Err(err) => match err.downcast_ref::<ExecStatus>() {
Some(status) if status.exitcode != 5 => Err(err),
_ => Ok(()),
},
_ => Ok(()),
}
}

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

fn forkpoint(&self, base: &RefName, branch: &RefName) -> Result<ObjectName, Status> {
fn forkpoint(&self, base: &RefName, branch: &RefName) -> Result<ObjectName> {
self.exec(&["merge-base", "--fork-point", base.as_str(), branch.as_str()])
.map(move |status| {
// TODO: handle not found
ObjectName(Cow::Owned(
String::from_utf8_lossy(&status.stdout).to_string(),
))
ObjectName(Cow::Owned(status.stdout))
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#![allow(missing_docs)] // TODO: change to warn/deny
#![allow(dead_code)] // TODO: remove

use ::anyhow::Result;
use ::clap::{self, Parser, Subcommand};
use ::std::{
option::Option::{self, None, Some},
result::Result,
string::String,
};

Expand Down Expand Up @@ -81,9 +81,9 @@ enum Command {
Sync,
}

fn main() -> Result<(), git::Status> {
fn main() -> Result<()> {
let root = Root::parse();
let runner = runner::Runner::new("git");
let runner = runner::Runner::new("git")?;
let stc = stc::Stc::new(runner);
match root.subcommand {
Command::Clean => stc.clean(),
Expand Down
Loading