Skip to content

Commit

Permalink
refactor: remove --set-upstream hack
Browse files Browse the repository at this point in the history
  • Loading branch information
altsem committed Oct 16, 2024
1 parent 6c88fcb commit 3821c6f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 42 deletions.
31 changes: 9 additions & 22 deletions src/ops/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl OpTrait for PullFromPushRemote {
}
Some(push_remote) => {
let refspec = git::get_head(&state.repo)?;
pull(state, term, &push_remote, Some(&refspec))
pull(state, term, &[&push_remote, &refspec])
}
},
))
Expand All @@ -53,7 +53,7 @@ fn set_push_remote_and_pull(state: &mut State, term: &mut Term, push_remote_name
.map_err(|_| "Could not set pushRemote config")?;

let refspec = git::get_head(&repo)?;
pull(state, term, push_remote_name, Some(&refspec))
pull(state, term, &[push_remote_name, &refspec])
}

pub(crate) struct PullFromUpstream;
Expand All @@ -63,12 +63,12 @@ impl OpTrait for PullFromUpstream {
|state: &mut State, term: &mut Term| match get_upstream_components(&state.repo)? {
None => {
let mut prompt =
create_prompt("Set upstream then pull", set_upstream_and_pull, true);
create_prompt("Set upstream then pull", pull_and_set_upstream, true);
Rc::get_mut(&mut prompt).unwrap()(state, term)
}
Some((remote, branch)) => {
let refspec = format!("refs/heads/{}", branch);
pull(state, term, &remote, Some(&refspec))
pull(state, term, &[&remote, &refspec])
}
},
))
Expand All @@ -83,18 +83,9 @@ impl OpTrait for PullFromUpstream {
}
}

fn set_upstream_and_pull(state: &mut State, term: &mut Term, upstream_name: &str) -> Res<()> {
state
.pending_menu
.as_mut()
.unwrap()
.args
.get_mut("--set-upstream")
.ok_or("Internal error")?
.set("")?;

fn pull_and_set_upstream(state: &mut State, term: &mut Term, upstream_name: &str) -> Res<()> {
let refspec = git::get_head(&state.repo)?;
pull(state, term, upstream_name, Some(&refspec))
pull(state, term, &["--set-upstream", upstream_name, &refspec])
}

pub(crate) struct PullFromElsewhere;
Expand All @@ -109,18 +100,14 @@ impl OpTrait for PullFromElsewhere {
}

fn pull_elsewhere(state: &mut State, term: &mut Term, remote: &str) -> Res<()> {
pull(state, term, remote, None)
pull(state, term, &[remote])
}

fn pull(state: &mut State, term: &mut Term, remote: &str, refspec: Option<&str>) -> Res<()> {
fn pull(state: &mut State, term: &mut Term, extra_args: &[&str]) -> Res<()> {
let mut cmd = Command::new("git");
cmd.args(["pull"]);
cmd.args(state.pending_menu.as_ref().unwrap().args());
cmd.arg(remote);

if let Some(refspec) = refspec {
cmd.arg(refspec);
}
cmd.args(extra_args);

state.close_menu();
state.run_cmd_async(term, &[], cmd)?;
Expand Down
27 changes: 7 additions & 20 deletions src/ops/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl OpTrait for PushToPushRemote {
Some(push_remote) => {
let head_ref = git::get_head(&state.repo)?;
let refspec = format!("{0}:{0}", head_ref);
push(state, term, &push_remote, Some(&refspec))
push(state, term, &[&push_remote, &refspec])
}
},
))
Expand All @@ -54,7 +54,7 @@ fn set_push_remote_and_push(state: &mut State, term: &mut Term, push_remote_name

let head_ref = git::get_head(&state.repo)?;
let refspec = format!("{0}:{0}", head_ref);
push(state, term, push_remote_name, Some(&refspec))
push(state, term, &[push_remote_name, &refspec])
}

pub(crate) struct PushToUpstream;
Expand All @@ -70,7 +70,7 @@ impl OpTrait for PushToUpstream {
Some((remote, branch)) => {
let head_ref = git::get_head(&state.repo)?;
let refspec = format!("{}:refs/heads/{}", head_ref, branch);
push(state, term, &remote, Some(&refspec))
push(state, term, &[&remote, &refspec])
}
},
))
Expand All @@ -86,18 +86,9 @@ impl OpTrait for PushToUpstream {
}

fn set_upstream_and_push(state: &mut State, term: &mut Term, upstream_name: &str) -> Res<()> {
state
.pending_menu
.as_mut()
.unwrap()
.args
.get_mut("--set-upstream")
.ok_or("Internal error")?
.set("")?;

let head_ref = git::get_head(&state.repo)?;
let refspec = format!("{0}:{0}", head_ref);
push(state, term, upstream_name, Some(&refspec))
push(state, term, &["--set-upstream", upstream_name, &refspec])
}

pub(crate) struct PushToElsewhere;
Expand All @@ -112,18 +103,14 @@ impl OpTrait for PushToElsewhere {
}

fn push_elsewhere(state: &mut State, term: &mut Term, remote: &str) -> Res<()> {
push(state, term, remote, None)
push(state, term, &[remote])
}

fn push(state: &mut State, term: &mut Term, remote: &str, refspec: Option<&str>) -> Res<()> {
fn push(state: &mut State, term: &mut Term, extra_args: &[&str]) -> Res<()> {
let mut cmd = Command::new("git");
cmd.args(["push"]);
cmd.args(state.pending_menu.as_ref().unwrap().args());
cmd.arg(remote);

if let Some(refspec) = refspec {
cmd.arg(refspec);
}
cmd.args(extra_args);

state.close_menu();
state.run_cmd_async(term, &[], cmd)?;
Expand Down

0 comments on commit 3821c6f

Please sign in to comment.