rubyprince
rubyprince

Reputation: 17793

When do bundler update the gem incase of gems pointed to git repo?

I am trying to move the common functionality of 3 Rails application into a gem. I have created the gem, tested it locally and is going to move to a private repository.

So, now I am concerned about how to deal with case if I have change in the code inside the gem. Do I need to change the version of the gem, if I want to update the gem while bundle update mygem, or will Bundler detect the change from the commit hash of the git repo while doing bundle update mygem?

Upvotes: 5

Views: 2200

Answers (4)

rubyprince
rubyprince

Reputation: 17793

I did not realize that since I own the gem, I could just update the gem and check it out for myself. I just tried it out and here are my findings:

The gem updates only when we run bundle update gemname in case of any gems whether it is from a git repo or from rubygems.org. If there is no details of the gem given in the Gemfile.lock (or if the Gemfile.lock does not exist), it will get the latest of the gem and writes the details of the gem inside the Gemfile.lock.

For the case of gems from git repo, when we run bundle update gemname, it updates to the latest commit hash of the relevant branch of the gem (in my case, it was master), and it writes the commit hash to the Gemfile.lock. In my case, it looks something like this (Note the revision block):

GIT
  remote: [email protected]:mygem.git
  revision: 5311ed765d5724cd20dfbcb87aa66b6f6fcbee7d
  specs:
    mygem (0.0.1)

For the case of gems from rubygems when we run bundle update gemname, it updates if a new version is available and the Gemfile.lock looks like this:

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (3.2.13)

So, to answer my own question, when I do bundle update mygem, for a gem from a git repo, it will get updated to the latest commit hash in the Git repo (even if the version of the gem is not changed). So, I do not need to change the version of the gem, I just need to push to the git repo and do bundle update mygem in the real project that uses mygem.

Upvotes: 3

Tim Moore
Tim Moore

Reputation: 9482

You do not need to change the version inside the gem every time you make a change to it. When using git gems, Gemfile.lock locks to a commit hash rather than the version number. You don't need to specify a version at all.

When you run bundle update mygem and mygem is a git gem, it will update the locked commit hash to the latest available on the branch you have specified (or on master if you have not specified a branch).

Upvotes: 5

p.matsinopoulos
p.matsinopoulos

Reputation: 7810

Since your Gemfile will be referencing your gem in the private git repository, then in order for your application to pick up any new changes in your gem, you need to do bundle update gemname. Pure and simple. If you do not do that, bundle will not pick up the changes.

Bundler uses the version of your gem that is locked inside your Gemfile.lock file in order to start/use it in your application. The version info inside your Gemfile.lock is updated only if you do bundle update. Otherwise it is locked (a.k.a. Gemfile.lock) and whatever version is locked is being used.

BTW, referencing a gem in a private git repository can have many options (:branch, :tag e.t.c.) but this is irrelevant. Lock is going to take place not matter what.

I hope that this one explains how bundler works.

Upvotes: 5

damoiser
damoiser

Reputation: 6238

Good question, I think when you run bundle update my_gem the task check only your version installed vs. version on the gem's branch, without checking the hash of the master gem branch.

I think so, because all the time I have run the bundle update the "update process" for a gem runs only when a new version (greater than my local version) is detected.

Hereafter some references:

EDIT

I see that you can check the remote hash declaring explicitly in the Gemfile:

# Specify that a git repository should use a particular ref, branch, or tag
:git => 'git://github.com/rails/rails.git', :ref => '4aded' 
:git => 'git://github.com/rails/rails.git', :branch => '2-3-stable' 
:git => 'git://github.com/rails/rails.git', :tag => 'v2.3.5'

But for "autochecking" if the gem version is a newer than your local, I think that the better way to do this is to specify a version.

EDIT 2

Maybe specifying a :branch of the git gem repo is the solution you are looking for.

:git => 'git://github.com/rails/rails.git', :branch => 'my_current_master_branch' 

Upvotes: 1

Related Questions