Skip to content

Commit

Permalink
Add a Rake task to generate a standalone script
Browse files Browse the repository at this point in the history
Even though the code is easier to manage and test as something like a
Ruby project, it's harder to install and use.  The rake task added
`rake bundle` will output an executable, standalone script called
git-rank in the project root.

The README install instructions have also been updated to give this as
an options, and provide better documentation to the other installation
options.
  • Loading branch information
Matt Robinson committed Jan 16, 2012
1 parent 4834a8e commit 8020311
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 44 deletions.
22 changes: 17 additions & 5 deletions README.md
Expand Up @@ -7,13 +7,25 @@ files and filtering lines by regex.

# Installation

For now copy or symlink the git-rank file to somewhere in your path and add
git-rank/lib to your RUBYLIB to run from source.
The easiest way to install is to generate the standalone script and copy to
somewhere in your PATH like ~/bin.

Or you can build it as a gem yourself. This is probably not recommended unless you're happy with the current functionality or are uncomfortable with modifying your PATH and RUBYLIB. I'm not sure that shipping this as a gem is the right way to distribute it long term, as I'm guessing there may be a better way to add commands to git. If you want the gem:
rake bundle &> /dev/null && cp git-rank $HOME/bin

gem build git-rank.gemspec
gem install git-rank-0.0.1.gem
If you would like to run from source to make updates from git easier, symlink
the bin/git-rank file to somewhere in your path and add git-rank/lib to your
RUBYLIB to run from source. This way if you update this repo, no further
action is required to run the new code.

ln -s `pwd`/bin/git-rank $HOME/bin/git-rank && export RUBYLIB=`pwd`/lib:$RUBYLIB

Or you can build it as a gem yourself. This is probably not recommended unless
you're happy with the current functionality or are uncomfortable with modifying
your PATH and RUBYLIB. I'm not sure that shipping this as a gem is the right
way to distribute it long term, as I'm guessing there may be a better way to
add commands to git. If you want the gem:

gem build git-rank.gemspec && gem install git-rank-0.0.1.gem

# Usage

Expand Down
25 changes: 25 additions & 0 deletions Rakefile
@@ -0,0 +1,25 @@
require 'rake'

desc "Bundle all the code into a single script"

task :bundle do
require 'find'

git_rank = File.open('git-rank', 'w')
git_rank.puts "#!/usr/bin/env ruby"

Find.find('lib', 'bin') do |file|
next unless FileTest.file? file
File.read(file).each_line do |line|
next if line =~ /^require.*git-rank/
next if line =~ /^#!/
git_rank.puts line
end
end

File.chmod(0755, git_rank.path)
git_rank.close
puts "git-rank created"
puts "This file be run as a script"
puts "Add it somewhere in your PATH for easy execution"
end
19 changes: 7 additions & 12 deletions lib/git-rank.rb
@@ -1,9 +1,5 @@
#!/usr/bin/env ruby

require 'optparse'
require 'fileutils'
require 'yaml'
require 'digest/md5'
require 'git-rank/log'
require 'git-rank/blame'

Expand All @@ -23,14 +19,13 @@ def sum

module GitRank
class << self
def calculate(options = {})
authors = if options[:blame]
GitRank::Blame.calculate(options)
else
GitRank::Log.calculate(options)
def calculate(options = {})
authors = if options[:blame]
GitRank::Blame.calculate(options)
else
GitRank::Log.calculate(options)
end
authors
end
authors
end

end
end
2 changes: 2 additions & 0 deletions lib/git-rank/blame.rb
@@ -1,3 +1,5 @@
require 'digest/md5'

module GitRank
module Blame
class << self
Expand Down
3 changes: 3 additions & 0 deletions lib/git-rank/cache.rb
@@ -1,3 +1,6 @@
require 'yaml'
require 'fileutils'

module GitRank
module Cache
class << self
Expand Down
56 changes: 29 additions & 27 deletions lib/git-rank/log.rb
@@ -1,36 +1,38 @@
require 'digest/md5'

module GitRank
module Log
class << self
def calculate(options = {})
authors = Hash.new {|h, k| h[k] = h[k] = Hash.new(0)}
options_digest = Digest::MD5.hexdigest(options[:additions_only].to_s + options[:deletions_only].to_s)
module Log
class << self
def calculate(options = {})
authors = Hash.new {|h, k| h[k] = h[k] = Hash.new(0)}
options_digest = Digest::MD5.hexdigest(options[:additions_only].to_s + options[:deletions_only].to_s)

author = nil
file = nil
state = :pre_author
git_log.each do |line|
case
when (state == :pre_author || state == :post_author) && line =~ /Author: (.*)\s</
author = $1
state = :post_author
when line =~ /^(\d+)\s+(\d+)\s+(.*)/
additions = $1.to_i
deletions = $2.to_i
file = $3
authors[author][file] += (additions + deletions)
state = :in_diff
when state == :in_diff && line =~ /^commit /
state = :pre_author
author = nil
file = nil
state = :pre_author
git_log.each do |line|
case
when (state == :pre_author || state == :post_author) && line =~ /Author: (.*)\s</
author = $1
state = :post_author
when line =~ /^(\d+)\s+(\d+)\s+(.*)/
additions = $1.to_i
deletions = $2.to_i
file = $3
authors[author][file] += (additions + deletions)
state = :in_diff
when state == :in_diff && line =~ /^commit /
state = :pre_author
end
end
authors
end
authors
end

private
private

def git_log
`git log -M -C -C -w --no-color --numstat`
def git_log
`git log -M -C -C -w --no-color --numstat`
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/git-rank/options.rb
@@ -1,3 +1,5 @@
require 'optparse'

module GitRank::Options
def self.parse
options = {}
Expand Down

0 comments on commit 8020311

Please sign in to comment.