Skip to content

Commit

Permalink
implement sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Bendzae committed May 5, 2024
1 parent 7ef80a1 commit 84b9234
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.idea
2 changes: 2 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub enum Commands {
List {},
#[clap(alias = "c")]
Change {},
#[clap(alias = "ss")]
Sync {},
Base {},
Up {},
Down {},
Expand Down
16 changes: 16 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn main() -> Result<()> {
Some(Commands::Add { name }) => ctx.add_to_stack(name)?,
Some(Commands::List {}) => ctx.list()?,
Some(Commands::Change {}) => ctx.change()?,
Some(Commands::Sync {}) => ctx.sync()?,
Some(Commands::Up {}) => ctx.checkout_above()?,
Some(Commands::Down {}) => ctx.checkout_below()?,
Some(Commands::Base {}) => ctx.checkout_base()?,
Expand Down Expand Up @@ -233,6 +234,21 @@ impl GsContext {
Ok(())
}

fn sync(&self) -> Result<()> {
let branches = &self.current_stack().unwrap().branches;
for (i, branch) in branches.clone().iter().enumerate() {
let rebase_on = match i {
0 => &self.current_stack().unwrap().base_branch,
_ => &branches[i - 1],
};
self.repo.rebase(
BranchName::from_str(branch)?,
BranchName::from_str(rebase_on)?,
)?;
}
Ok(())
}

fn reset(&mut self) -> Result<()> {
let mut deleted = 0;
self.state
Expand Down
8 changes: 8 additions & 0 deletions src/repo_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ use rustygit::{types::BranchName, Repository};

pub trait RepoExtenstions {
fn current_branch(&self) -> Result<BranchName>;
fn rebase(&self, branch: BranchName, on: BranchName) -> Result<()>;
}

impl RepoExtenstions for Repository {
fn current_branch(&self) -> Result<BranchName> {
let branches = self.cmd_out(&["rev-parse", "--abbrev-ref", "HEAD"])?;
Ok(BranchName::from_str(branches.first().unwrap())?)
}

fn rebase(&self, branch: BranchName, on: BranchName) -> Result<()> {
self.switch_branch(&branch)?;
let output = self.cmd_out(&["rebase", "--update-refs", on.to_string().as_str()])?;
println!("{:?}", output);
Ok(())
}
}

0 comments on commit 84b9234

Please sign in to comment.