Search code examples
rubygitrugged

Rugged - fetch, pull rebase possible?


Using rugged how do you perform the following operations: fetch, pull and rebase?

I am using the development branch and after reviewing its documentation found here as a guide to the Remote class.

EDIT: Since git pull is just a shorthand for git fetch and git merge FETCH_HEAD the better question is how to perform git fetch, git merge and git rebase.


Solution

  • git fetch:

    remote = Rugged::Remote.lookup(repo, "origin")
    remote.connect(:fetch) do |r|
      r.download
      r.update_tips!
    end
    

    git merge:

    merge_index = repo.merge_commits(
      Rugged::Branches.lookup(repo, "master").tip,
      Rugged::Branches.lookup(repo, "origin/master").tip
    )
    
    raise "Conflict detected!" if merge_index.conflicts?
    
    merge_commit = Rugged::Commit.create(repo, {
      parents: [
        Rugged::Branches.lookup(repo, "master").tip,
        Rugged::Branches.lookup(repo, "origin/master").tip
      ],
      tree: merge_index.write_tree(repo),
      message: 'Merged `origin/master` into `master`',
      author:    { name: "User", email: "[email protected]" },
      committer: { name: "User", email: "[email protected]" },
      update_ref: 'master'
    })
    

    git rebase:

    Rebasing was not implemented yet in libgit2, and thus is not available in Rugged.


    In general, your use case sounds very high level, while the rugged API is currently a bit more focused on low-level git repository access and modification. Eventually, we'll also have many higher-level helpers (like a more simple/correct pull) in the future, but we're not there yet.