You are currently browsing the tag archive for the ‘Git’ tag.

We use this script to automatically merge all features into staging and re-merge those features with deploy-ed code. It will stop when there is a merge conflict -> resolve and start it again.

Automatic –>
often -> smaller conflicts.
no thinking/typing -> less mistakes/headaches.

Usage
e.g. store it in scripts folder or turn it into a rake task

scripts/push_and_merge_all
scripts/push_and_merge_all stage # to also deploy everything to staging

Code

#!/usr/bin/env ruby
require 'rubygems'
require 'rake'

sh "git status | grep 'nothing to commit' && echo 'you are clean''" # ensure we are not dirty
current_branch = `git branch | grep '*'`.split.last
sh "git fetch origin" # get up-to-date info about what needs pulling

def merge(branch, options)
  sh "git checkout #{branch}"
  push_and_pull
  options[:with].each do |merged_branch|
    sh "git checkout #{merged_branch}"
    push_and_pull
    sh "git checkout #{branch} && git merge #{merged_branch}"
  end
  push_and_pull
end

def push_and_pull
  status= `git status 2> /dev/null`
  remote_pattern = /# Your branch is (.*?) /
  diverge_pattern = /# Your branch and (.*) have diverged/
  if status =~ remote_pattern
    if $1 == 'ahead'
      sh "git push"
    else
      sh "git pull"
    end
  elsif status =~ diverge_pattern
    sh "git pull && git push"
  end
end

to_stage = ['master', 'feature_a', 'feature_b']
to_stage.each{|branch| merge(branch, :with => ['deploy']) }
merge('staging', :with => to_stage)

sh "cap deploy" if ARGV[0] == 'stage'
sh "git checkout #{current_branch}"

git ls-files --others --exclude-standard | xargs rm

To use it as git rmuntracked, add to ~/.gitconfig

[alias]
  rmuntracked = "!git ls-files --others --exclude-standard | xargs rm"

(as you can see in my dotfiles)

Also available as Gist

Ever wondered how much who adds/removes, its time to find out :D
(those are real stats, I just obfuscated the names ;) )

Results

Git scores (in LOC):
mr-add              :  +482273       -9466
justu               :  +286250       -159905
grosser             :  +152384       -323344
another             :  +121257       -82116
naames              :  +104577       -13591
justfor             :  +68716        -72446
example             :  +7795         -4987
andeven             :  +5100         -1730
morenow             :  +4225         -2764
finish              :  +17           -19

Update: After working 2.5 years on this project my final stats where: +861345 -1115685 = -254340 LOC :D

Install
Copy init git_stats.rb and ruby git_stats.rb
(you can add the names of people who commit with different users into the ‘same’ array)

# please add enhancements to http://gist.github.com/234560
#!/usr/bin/env ruby
t = Time.now

same = [['name-a','name-b'],['mr fred','fred']]
pipe = open("|git log --shortstat")
author = "unknown"
stats = {}

loop do
  line = pipe.readline rescue break
  author = $1 if line =~ /Author\: ([a-z]+) </
  found = same.detect{|a| a.include?(author)}
  author = found.first if found

  if line =~  /files changed, (\d+) insertions\(\+\), (\d+) deletions/
    stats[author] ||= Hash.new(0)
    stats[author]['+']+=$1.to_i
    stats[author]['-']+=$2.to_i
    print '.'
  end
end

puts "\nGit scores (in LOC):"
puts stats.sort_by{|a,d| -d['+'] }.map{|author, data| "#{author.ljust(20)}:  +#{data['+'].to_s.ljust(10)}   -#{data['-'].to_s.ljust(10)} " } * "\n"
Follow

Get every new post delivered to your Inbox.

Join 63 other followers