#!/usr/bin/env ruby # git.rb - a P4 shim that pretends to be Git so that it can work with Xcode # entirely experimental: here be dragons require 'fileutils' begin p4 = '/usr/local/bin/p4' # require a P4 URI because it's my tool # URIs are of the form [username@]p4:// case ARGV[0] when 'clone' matches = /(.+?)@p4:\/\/(.+?)\/(.*)|p4:\/\/(.+?)\/(.*)/.match(ARGV[1]) user = matches.captures[0] port = matches.captures[1] || matches.captures[3] path = matches.captures[2] || matches.captures[4] # let's be civilized and not require a port if port !~ /:\d*/ port = port + ":1666" end cmd = "#{p4} -d #{ARGV[2]}" cmd += " -u #{user}" if user `#{cmd} clone -p #{port} -f //#{path}` FileUtils.mkdir_p "ARGV[2]/.git/info" when 'init' puts "#{p4} init" FileUtils.mkdir_p '.git/info' when 'add' `#{p4} rec` when 'status' results = `#{p4} -ztag -F '%change% %action% %localFile%' status` results.each_line do |r| if r.start_with?(' add ') puts "?? #{r.split(' ')[1]}" elsif r.start_with?('default add ') puts "A #{r.split(' ')[2]}" elsif r.start_with?(' edit ') puts " M #{r.split(' ')[1]}" elsif r.start_with?('default edit ') puts "M #{r.split(' ')[2]}" elsif r.start_with?(' delete ') puts " D #{r.split(' ')[1]}" elsif r.start_with?('default delete ') puts "D #{r.split(' ')[2]}" end end when 'branch' results = `#{p4} switch` puts '* ' + results when 'mv' puts "#{p4} edit #{ARGV[1]}" puts "#{p4} move #{ARGV[1]} #{ARGV[2]}" when 'rm' if ARGV[1] == '-f' && ARGV[2] == '-r' && ARGV[3] == '--' ARGV[4..-1].each do |f| "#{p4} delete #{f}" "#{p4} delete #{f}/..." end end when 'commit' puts 'p4 submit' when 'checkout' puts 'p4 switch' when 'blame' puts 'p4 annotate' when 'archive' puts 'lots of stuff' when 'log' puts 'p4 changes' when 'config' && ARGV[1] == '--get' if ARGV[2] == 'branch.default.remote' puts 'default' elsif ARGV[2] == 'branch.default.merge' puts 'refs/heads/default' end when 'remote' puts "default\x09http://xxxxx (fetch)" puts "default\x09http://xxxxx (push)" when 'remote-ls' puts '8854794d9d2d44170abe943b22ff7f8d647a0b4e refs/heads/default' end end