rust-lang / git2-rs

libgit2 bindings for Rust
https://docs.rs/git2
Apache License 2.0
1.69k stars 388 forks source link

How do you perform a rebase? #932

Open technologicalMayhem opened 1 year ago

technologicalMayhem commented 1 year ago

So I am struggling with how you would perform a rebase. I want to handle a case where a remote has changes pushed to it and I now want to rebase the commits that I already made, so I can push them.

I am rather unsure how the rebase function works and how to integrate incoming changes.

Can someone perhaps provide an example?

extrawurst commented 1 year ago

this is just a wrapper around libgit2. for usage help I would suggest reaching out to upstream. everything applies to git2-rs then too

NYBACHOK commented 1 year ago

@technologicalMayhem I'm a little bit late. May you show how to push changes?

 self.repository.remote( UPSTREAM_NAME, self.options.upstream_url.0.as_str() )
    .map_err( GitErrors::Git2Error )?
    .fetch( &[ &self.options.default_branch ], Some( &mut fetch_options ), None)
    .map_err( GitErrors::Git2Error )?;
    dbg!( "Add remote and fetch");

    let upstream_master_oid = self.repository.refname_to_id(&format!("refs/remotes/{UPSTREAM_NAME}/{}", self.options.default_branch))
    .map_err( GitErrors::Git2Error )?;
    dbg!(&upstream_master_oid);

    let upstream_master_commit = self.repository.find_annotated_commit(upstream_master_oid)
    .map_err( GitErrors::Git2Error )?;

    let self_master_oid = self.repository.refname_to_id(&format!("refs/remotes/origin/{}", self.options.default_branch))
    .map_err( GitErrors::Git2Error )?;
    dbg!(&self_master_oid);

    let self_master_commit = self.repository.find_annotated_commit(upstream_master_oid)
    .map_err( GitErrors::Git2Error )?;

    let mut rebase_options = git2::RebaseOptions::default();
    rebase_options.inmemory( false );

    let mut rebase = self.repository.rebase(
      None,
      Some( &upstream_master_commit ),
      Some(&self_master_commit),
      Some(&mut rebase_options)
    )
    .map_err( GitErrors::Git2Error )?;

    for reb in rebase.by_ref()
    {
      reb.map_err( GitErrors::Git2Error )?;
    }

    rebase.finish( None)
    .map_err( GitErrors::Git2Error )?;
dvaerum commented 2 days ago

@NYBACHOK are you sure that this example works? Because regardless of what I do, I cannot get rebase to work with git2-rs 😭