Skip to content

Commit

Permalink
Restore previously checked out commit after restack (#57)
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
9999years authored Apr 7, 2024
1 parent 92ed735 commit 72c8d6f
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/restack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const CONTINUE_MESSAGE: &str = "Fix conflicts and then use `git-gr restack conti
/// TODO: Add versioning?
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
pub struct RestackTodo {
before: RepositoryState,
pub graph: DependencyGraph,
/// Restack steps left to perform.
steps: VecDeque<Step>,
Expand Down Expand Up @@ -112,6 +113,12 @@ impl RestackTodo {
}
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
pub struct RepositoryState {
change: Option<ChangeNumber>,
commit: CommitHash,
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
struct Step {
change: ChangeNumber,
Expand Down Expand Up @@ -285,6 +292,8 @@ pub fn restack(

fs::remove_file(todo_path(&git)?).into_diagnostic()?;

let restore = todo.before.clone();

let mut todo = PushTodo::from(todo);
if todo.is_empty() {
tracing::info!("Restack completed; no changes");
Expand All @@ -304,6 +313,17 @@ pub fn restack(
tracing::info!("Restack completed but changes have not been pushed; run `git-gr restack push` to sync changes with the remote.");
}

let restore_commit = match restore.change {
Some(restore_change) => todo
.refs
.get(&restore_change)
.map(|update| &update.new)
.unwrap_or(&restore.commit),
None => &restore.commit,
};

git.checkout(restore_commit)?;

Ok(())
}

Expand Down Expand Up @@ -374,11 +394,25 @@ pub fn create_todo(gerrit: &mut GerritGitRemote, branch: &str) -> miette::Result
return Err(miette!("Restack todo already exists at `{todo_path}`"));
}

let change_id = git
.change_id(branch)
.wrap_err("Failed to get Change-Id for HEAD")?;
let head = git.rev_parse("HEAD")?;
let head_change = match git
.change_id(&head)
.and_then(|change_id| gerrit.get_change(change_id))
{
Ok(change) => Some(change.number),
Err(error) => {
tracing::debug!("Failed to get HEAD change ID: {error}");
None
}
};

let change_id = git.change_id(branch)?;
let change = gerrit.get_change(change_id)?;
let mut todo = RestackTodo {
before: RepositoryState {
change: head_change,
commit: head,
},
graph: gerrit.dependency_graph(change.number)?,
steps: Default::default(),
refs: Default::default(),
Expand Down

0 comments on commit 72c8d6f

Please sign in to comment.