Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git: Add push() builder #11

Merged
merged 1 commit into from
Feb 6, 2024
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
3 changes: 3 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ mod branch;
mod cherry_pick;
mod fetch;
mod log;
mod push;
mod rev_list;
mod switch;

pub use branch::{branch, StartingPoint};
pub use cherry_pick::cherry_pick;
pub use fetch::fetch;
pub use log::log;
pub use push::push;
pub use rev_list::rev_list;
pub use switch::switch;

Expand Down Expand Up @@ -49,6 +51,7 @@ impl Format {

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

pub trait GitCmd: Sized {
// FIXME: Spawn needs to check the exit code and encode that in its return type - non-zero should be Err
Expand Down
40 changes: 40 additions & 0 deletions src/git/push.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::process::Command;

use super::{Branch, GitCmd, Remote};

#[derive(Default)]
pub struct Push {
upstream: Option<String>,
refspecs: Vec<String>,
}

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

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

pub fn branch<T: Into<String>>(self, Branch(branch): Branch<T>) -> Push {
let mut refspecs = self.refspecs;
refspecs.push(branch.into());

Push { refspecs, ..self }
}
}

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

self.upstream.map(|remote| cmd.arg("-u").arg(remote));
self.refspecs.iter().for_each(|r| {
cmd.arg(r);
});
}
}
9 changes: 5 additions & 4 deletions src/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,11 @@ pub async fn prepare_commits(
})?;

info!("pushing branch...");
std::process::Command::new("git")
.args(["push", "-u", "origin", "HEAD"])
.spawn()?
.wait_with_output()?;
git::push()
.upstream(git::Remote("origin"))
// TODO: Rename? This should be .refspec()?
.branch(git::Branch("HEAD"))
.spawn()?;

if let Some(token) = token {
info!("creating pull-request...");
Expand Down