Permalink
Browse files

added some ruby scripts for git stuff

  • Loading branch information...
kyriacos committed Mar 30, 2012
1 parent 9350ec2 commit 5b09982cbb52eb03136ea7d10f9a421cb263637f
Showing with 82 additions and 0 deletions.
  1. +42 −0 gbrt.rb
  2. +40 −0 git_cwd_info.rb
View
42 gbrt.rb
@@ -0,0 +1,42 @@
+#!/usr/bin/env ruby
+
+def format_commit_info timestamp, time_desc, commit_id, message, ref_name
+ [
+ "#{timestamp.strftime("%y %b %d")}, #{timestamp.strftime("%l:%M%p").downcase}",
+ "(#{time_desc})",
+ commit_id,
+ message,
+ ref_name
+ ]
+end
+
+def render_commit_info timestamp, time_desc, commit_id, message, ref_name, merged
+ [
+ timestamp,
+ time_desc,
+ "\e[32m#{ref_name}\e[0m",
+ (merged ? " #{commit_id}" : "+ \e[33m#{commit_id}\e[0m"),
+ "\e[#{message[/^Temp/] ? 31 : 90}m#{message.strip}\e[0m"
+ ].join(' ')
+end
+
+commit_info = `git branch #{ARGV.join(' ')} | cut -c 3-`.strip.split("\n").reject {|ref_name|
+ ref_name[' -> ']
+}.map {|ref_name|
+ `git log --no-walk --pretty=format:"%ct\n%cr\n%h\n%s" '#{ref_name}' --`.strip.split("\n").push(ref_name)
+}.map {|commit_info|
+ [Time.at(commit_info.shift.to_i)].concat(commit_info)
+}.sort_by {|commit_info|
+ commit_info.first # unix timestamp
+}.reverse.map {|commit_info|
+ format_commit_info(*commit_info)
+}.transpose.map {|column|
+ max_col_length = column.sort_by {|i| i.length }.last.length
+ column.map {|i| i.ljust(max_col_length) }
+}.transpose.map {|commit_info|
+ commit_info.push(
+ `git merge-base HEAD #{commit_info[2]}`.chomp[0...7] == commit_info[2]
+ )
+}.each {|commit_info|
+ puts render_commit_info(*commit_info)
+}
View
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby
+# -*- coding: utf-8 -*-
+
+# The methods that get called more than once are memoized.
+
+def git_repo_path
+ @git_repo_path ||= `git rev-parse --git-dir 2>/dev/null`.strip
+end
+
+def in_git_repo
+ !git_repo_path.empty? &&
+ git_repo_path != '~' &&
+ git_repo_path != "#{ENV['HOME']}/.git"
+end
+
+def git_parse_branch
+ @git_parse_branch ||= File.read("#{git_repo_path}/HEAD").strip.scan(/(?<=refs\/heads\/).*$/).first
+end
+
+def git_head_commit_id
+ `git rev-parse --short HEAD 2>/dev/null`.strip
+end
+
+def git_cwd_dirty
+ " %{\e[90m%}✗%{\e[0m%}" unless git_repo_path == '.' || `git ls-files -m`.strip.empty?
+end
+
+def rebasing_etc
+ if File.exists?(File.join(git_repo_path, 'BISECT_LOG'))
+ "+bisect"
+ elsif File.exists?(File.join(git_repo_path, 'MERGE_HEAD'))
+ "+merge"
+ elsif %w[rebase rebase-apply rebase-merge ../.dotest].any? {|d| File.exists?(File.join(git_repo_path, d)) }
+ "+rebase"
+ end
+end
+
+if in_git_repo
+ print " %{\e[34m%}#{git_parse_branch}%{\e[90m%}@%{\e[33m%}#{git_head_commit_id}%{\e[0m%}#{rebasing_etc}#{git_cwd_dirty}"
+end

0 comments on commit 5b09982

Please sign in to comment.