#!/usr/bin/env ruby require 'optparse' options = {:all => false, :sha => ARGV.shift} optparser = OptionParser.new do |parser| parser.banner = "Usage: git-meta [sha|ref] [ --get | --get-all ]" parser.on('-a', '--get-all', "Retrieves all the keys as YAML data.") do |all| options[:all] = all end parser.on('-g', '--get KEY', "Retrives a certain KEY from the git commit object") do |key| options[:keys] = key.split(".") end parser.on('-h', '--help', "Returns Usage Notes") do puts parser exit end end if !options[:sha] puts optparser exit end optparser.parse(ARGV) git_commit_info, data, output = nil, nil, nil IO.popen("git-cat-file -p #{options[:sha]}", 'r') do |git_cat_file| git_commit_info = git_cat_file.read end if git_commit_info =~ /^(---git-meta---\s*\n)(.*\n)(^---git-meta---.*\n)/m if options[:keys] data = YAML.load($2) options[:keys].each do |key| data = data.fetch(key, Hash.new("")) end puts data.to_yaml.gsub(/^--- \n?/, '') elsif options[:all] puts $2 end end