Example of a git push using either rugged or grit
Asked Answered
E

2

7

I'm looking for some code examples, for either rugged or grit, showing how to do a git push.

Background

I have rake tasks deploy:staging and deploy:production that I use to deploy my app.

I'm deploying to heroku, so these tasks essentially do the following:

  1. Get the most recent tag (eg. git describe --abbrev=0)
  2. Push the version represented by that tag to the specified remote (eg. git push staging v1.00)
  3. Store the version in a heroku config var (eg. heroku config:add APP_VERSION=v1.00)

(There's also some checks in there to make sure I haven't forgotten to create a new tag before pushing etc.)

Initially I was using system calls from my Rakefile for these CLI commands; then I moved to using the git and heroku-api gems.

The git gem appears to be abandoned however (no commits in the past year); it seems that Grit and rugged are now the standard gems for working with Git.

Unfortunately, given the lack of documentation, I can't figure out how to do a git push with either of these libraries.

(In the following examples, assume that the remote/branch I'm pushing to is origin/master, and is already setup as a remote in the local repo)

Starting with rugged:

$ irb
2.0.0-p0 :001 > require 'rugged'
 => true 
2.0.0-p0 :002 > repo = Rugged::Repository.new('/path/to/repo')
 => #<Rugged::Repository:0x007fe8b48821c0 @encoding=#<Encoding:UTF-8>> 
2.0.0-p0 :003 > remote = Rugged::Remote.lookup(repo, 'origin')
 NoMethodError: undefined method `lookup' for Rugged::Remote:Class

Now for grit:

$ irb
2.0.0-p0 :001 > require 'grit'
 => true 
2.0.0-p0 :002 > repo = Grit::Repo.new('/path/to/repo')
 => #<Grit::Repo "/path/to/repo/.git"> 
2.0.0-p0 :004 > remote = repo.remotes.last
 => #<Grit::Remote "origin/master"> 
2.0.0-p0 :005 > repo.git.push(remote)
NoMethodError: undefined method `delete' for #<Grit::Remote "origin/master">

Any help would be greatly appreciated.

Erudition answered 1/3, 2013 at 5:2 Comment(0)
A
1

Ok, I think I figured it out, BUT now It's asking me for my gitHub credentials and I can't type my credentials because I receive a 'Timeout' error.

This is what I did:

Add the remote repo to the project:

repo.git.remote({},'add','RemoteRepoName','https://github.com//.git')

Push to github

pusher = repo.git.push({:process_info => true, :progress => true}, 'RemoteRepoName', 'master')

Annmaria answered 10/4, 2013 at 20:49 Comment(1)
Thanks mcKain...that seems to work. I did get an error when trying to push from a local repo to another repo on the same machine, but that was only because the remote was not a bare repository; so in the case of pushing to github or heroku I don't think this would be an issue. In the case of pushing to github, you probably need to add the remote using the SSH url (eg. [email protected]:username/repo) rather than the https URL. Anyway, that's good enough for me to mark this as the answer. Thanks.Erudition
G
1

With grit, repo.git.push actually calls out to Git#native via method_missing. Its signature is this:

def native(cmd, options = {}, *args, &block)

so you'd want to do the following instead:

repo.git.push({}, remote)

Yeah, it's silly to put OPTIONAL options at the beginning, but that's just how it's written.

Gard answered 9/4, 2013 at 21:13 Comment(1)
Thanks Cody. Not sure if I'm doing something wrong, but it didn't seem to work.. I can call repo.git.push({}, remote) as you suggest, and although it no longer throws an error, it doesn't actually appear to push anything to the remote.Erudition
A
1

Ok, I think I figured it out, BUT now It's asking me for my gitHub credentials and I can't type my credentials because I receive a 'Timeout' error.

This is what I did:

Add the remote repo to the project:

repo.git.remote({},'add','RemoteRepoName','https://github.com//.git')

Push to github

pusher = repo.git.push({:process_info => true, :progress => true}, 'RemoteRepoName', 'master')

Annmaria answered 10/4, 2013 at 20:49 Comment(1)
Thanks mcKain...that seems to work. I did get an error when trying to push from a local repo to another repo on the same machine, but that was only because the remote was not a bare repository; so in the case of pushing to github or heroku I don't think this would be an issue. In the case of pushing to github, you probably need to add the remote using the SSH url (eg. [email protected]:username/repo) rather than the https URL. Anyway, that's good enough for me to mark this as the answer. Thanks.Erudition

© 2022 - 2024 — McMap. All rights reserved.