Skip to content
Open
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
1,308 changes: 867 additions & 441 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ env_logger = "0.10.0"
git2 = "0.18.1"
log = "0.4.20"
octocrab = "0.33"
serde = { version = "1.0.224", features = ["derive"] }
thiserror = "1.0.40"
tokio = { version = "1.33.0", features = ["full"] }
toml = "0.9.6"
49 changes: 46 additions & 3 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
extern crate log as ext_log;
use ext_log::info;

use std::process::{self, Command, Stdio};
use std::{fmt, io};

Expand All @@ -8,14 +11,26 @@ mod branch;
mod cherry_pick;
mod fetch;
mod log;
mod merge;
mod mergebase;
mod push;
mod rebase;
mod rev_list;
mod revparse;
mod show;
mod switch;

pub use branch::{branch, StartingPoint};
pub use cherry_pick::cherry_pick;
pub use fetch::fetch;
pub use log::log;
pub use merge::merge;
pub use mergebase::merge_base;
pub use push::push;
pub use rebase::rebase;
pub use rev_list::rev_list;
pub use revparse::revparse;
pub use show::show;
pub use switch::switch;

#[derive(Debug, Error)]
Expand All @@ -35,18 +50,21 @@ impl fmt::Display for Error {
#[derive(Clone, Copy)]
pub enum Format {
Hash,
ShortHash,
Title,
}

impl Format {
fn as_str(&self) -> &str {
match self {
Format::Hash => "%h",
Format::Hash => "%H",
Format::ShortHash => "%h",
Format::Title => "%s",
}
}
}

pub struct Revision<T: Into<String>>(pub T);
pub struct Branch<T: Into<String>>(pub T);
pub struct Commit<T: Into<String>>(pub T);

Expand All @@ -57,7 +75,6 @@ pub trait GitCmd: Sized {
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());

self.setup(&mut cmd);

let output = cmd.spawn()?.wait_with_output()?;

if output.status.success() {
Expand All @@ -66,6 +83,32 @@ pub trait GitCmd: Sized {
Err(Error::Status(output))
}
}

fn setup(self, cmd: &mut Command);
}

pub fn split_remote_branch(rev: &str) -> Option<(&str, &str)> {
let split: Vec<&str> = rev.splitn(2, '/').collect();

if split.len() < 2 {
return None;
}

Some((split[0], split[1]))
}

pub fn maybe_fetch_from_branch(rev: &str) -> Result<(), Error> {
let (remote, _) = if let Some((rem, br)) = split_remote_branch(rev) {
info!("Remote: {rem}, branch: {br}");
(Some(rem), br)
} else {
info!("branch spec has no remote: {rev}");
(None, rev)
};

if let Some(rem) = remote {
info!("Fetching from {rem}");
fetch().remote(rem).spawn()?;
}

Ok(())
}
14 changes: 14 additions & 0 deletions src/git/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Log {
branch: Option<String>,
grep: Option<String>,
format: Option<Format>,
merges: Option<bool>,
}

pub fn log() -> Log {
Expand Down Expand Up @@ -43,6 +44,12 @@ impl Log {
..self
}
}
pub fn merges<T: Into<bool>>(self, merges: T) -> Log {
Log {
merges: Some(merges.into()),
..self
}
}
}

impl GitCmd for Log {
Expand All @@ -54,5 +61,12 @@ impl GitCmd for Log {
self.format
.map(|f| cmd.arg(format!("--format={}", f.as_str())));
self.branch.map(|b| cmd.arg(b));
self.merges.map(|b| {
if b {
cmd.arg("--merges")
} else {
cmd.arg("--no-merges")
}
});
}
}
59 changes: 59 additions & 0 deletions src/git/merge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use super::GitCmd;

use std::process::Command;

// FIXME: Add a derive(Builder)
pub struct Merge {
other_branch: String,
strategy: Option<String>,
message: Option<String>,
no_edit: bool,
}

pub fn merge<T: Into<String>>(other_branch: T) -> Merge {
Merge {
other_branch: other_branch.into(),
strategy: None,
message: None,
no_edit: false,
}
}

impl Merge {
pub fn message<T: Into<String>>(self, message: T) -> Merge {
Merge {
message: Some(message.into()),
..self
}
}

pub fn no_edit(self) -> Merge {
Merge {
no_edit: true,
..self
}
}

// FIXME add better type for strategy
pub fn strategy<T: Into<String>>(self, strategy: T) -> Merge {
Merge {
strategy: Some(strategy.into()),
..self
}
}
}

impl GitCmd for Merge {
fn setup(self, cmd: &mut Command) {
cmd.arg("merge");

self.strategy.map(|s| cmd.arg("--strategy").arg(s));

if self.no_edit {
cmd.arg("--no-edit");
}

self.message.map(|m| cmd.arg("-m").arg(m.as_str()));
cmd.arg(self.other_branch);
}
}
23 changes: 23 additions & 0 deletions src/git/mergebase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::GitCmd;
use std::process::Command;

// FIXME: Add a derive(Builder)
pub struct Mergebase {
commit1: String,
commit2: String,
}

pub fn merge_base<T1: Into<String>, T2: Into<String>>(commit1: T1, commit2: T2) -> Mergebase {
Mergebase {
commit1: commit1.into(),
commit2: commit2.into(),
}
}

impl GitCmd for Mergebase {
fn setup(self, cmd: &mut Command) {
cmd.arg("merge-base");
cmd.arg(self.commit1);
cmd.arg(self.commit2);
}
}
63 changes: 63 additions & 0 deletions src/git/push.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use super::GitCmd;

use std::process::Command;

// FIXME: Add a derive(Builder)
#[derive(Default)]
pub struct Push {
set_upstream: bool,
force: bool,
remote: Option<String>,
refspec: Option<String>,
}

pub fn push() -> Push {
Push::default()
}

impl Push {
pub fn set_upstream(self) -> Push {
Push {
set_upstream: true,
..self
}
}

pub fn force(self) -> Push {
Push {
force: true,
..self
}
}

pub fn remote<T: Into<String>>(self, remote: T) -> Push {
Push {
remote: Some(remote.into()),
..self
}
}

pub fn refspec<T: Into<String>>(self, refspec: T) -> Push {
Push {
refspec: Some(refspec.into()),
..self
}
}
}

impl GitCmd for Push {
fn setup(self, cmd: &mut Command) {
cmd.arg("push");
if self.set_upstream {
cmd.arg("--set-upstream");
}

self.remote.map(|x| cmd.arg(x));

if self.force {
cmd.arg("--force");
}

self.refspec.map(|r| cmd.arg(r));
}
}
52 changes: 52 additions & 0 deletions src/git/rebase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::GitCmd;
use std::process::Command;

// FIXME: Add a derive(Builder)
pub struct Rebase {
onto: String,
interactive: bool,
autosquash: bool,
}

pub fn rebase<T: Into<String>>(onto: T) -> Rebase {
Rebase {
onto: onto.into(),
interactive: false,
autosquash: false,
}
}

impl Rebase {
pub fn interactive(self) -> Rebase {
Rebase {
interactive: true,
..self
}
}

pub fn autosquash(self) -> Rebase {
Rebase {
autosquash: true,
..self
}
}
}

impl GitCmd for Rebase {
fn setup(self, cmd: &mut Command) {
if self.interactive {
cmd.arg("-c").arg("sequence.editor=true");
}

cmd.arg("rebase");

if self.interactive {
cmd.arg("--interactive");
}

if self.autosquash {
cmd.arg("--autosquash");
}
cmd.arg(self.onto);
}
}
27 changes: 27 additions & 0 deletions src/git/revparse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use super::{GitCmd, Revision};

use std::process::Command;

// FIXME: Add a derive(Builder)
#[derive(Default)]
pub struct RevParse {
revision: String,
}

pub fn revparse() -> RevParse {
RevParse::default()
}

impl RevParse {
pub fn revision<T: Into<String>>(self, Revision(revision): Revision<T>) -> RevParse {
RevParse {
revision: revision.into(),
}
}
}

impl GitCmd for RevParse {
fn setup(self, cmd: &mut Command) {
cmd.arg("rev-parse").arg(self.revision);
}
}
Loading