git2-rs icon indicating copy to clipboard operation
git2-rs copied to clipboard

How do you perform a rebase?

Open technologicalMayhem opened this issue 2 years ago • 2 comments

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?

technologicalMayhem avatar Feb 23 '23 07:02 technologicalMayhem

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

extrawurst avatar Feb 23 '23 07:02 extrawurst

@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 )?;
    ```

NYBACHOK avatar Oct 05 '23 15:10 NYBACHOK