public
Description: A bidirectional Git - Bazaar gateway
Homepage:
Clone URL: git://github.com/pieter/git-bzr.git
git-bzr / git-bzr
100755 130 lines (107 sloc) 3.987 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env ruby
require 'fileutils'
EXPORT_LOCATION = "~/.bazaar/plugins/fastimport/exporters/bzr-fast-export.py"
PYTHON = ENV["PYTHON"] || "/opt/local/bin/python2.5"
 
command = ARGV.shift
commands = [:add, :push, :fetch, :pull]
 
if !command || !commands.include?(command.to_sym)
  puts "Usage: git bzr [COMMAND] [OPTIONS]"
  puts "Commands: add, push, fetch, pull"
  exit
end
 
class BzrCommands
  
  def add(*args)
    name = args.shift
    location = args.shift
    unless name && location && args.empty?
      puts "Usage: git bzr add name location"
      exit
    end
    if `git remote show`.split("\n").include?(name)
      puts "There is already a remote with that name"
      exit
    end
    
    if `git config git-bzr.#{name}.url` != ""
      puts "There is alread a bazaar branch with that name"
      exit
    end
    
    if !File.exists?(File.join(location, ".bzr"))
      puts "Remote is not a bazaar repository"
      exit
    end
 
    `git config git-bzr.#{name}.location #{location}`
    puts "Bazaar branch #{name} added. You can fetch it with `git bzr fetch #{name}`"
    
  end
 
  def get_location(remote)
    l = `git config git-bzr.#{remote}.location`.strip
    if l == ""
      puts "Cannot find bazaar remote with name `#{remote}`."
      exit
    end
    l
  end
  
  def fetch(*args)
    remote = args.shift
    unless remote && args.empty?
      puts "Usage: git bzr fetch branchname"
      exit
    end
    location = get_location(remote)
 
    git_map = File.expand_path(File.join(git_dir, "bzr-git", "#{remote}-git-map"))
    bzr_map = File.expand_path(File.join(git_dir, "bzr-git", "#{remote}-bzr-map"))
 
    if !File.exists?(git_map) && !File.exists?(bzr_map)
      print "There doesn't seem to be an existing refmap. "
      puts "Doing an initial import"
      FileUtils.makedirs(File.dirname(git_map))
      `(#{PYTHON} #{EXPORT_LOCATION} --export-marks=#{bzr_map} --git-branch=bzr/#{remote} #{location}) | (git fast-import --export-marks=#{git_map})`
    elsif File.exists?(git_map) && File.exists?(bzr_map)
      puts "Updating remote #{remote}"
      old_rev = `git rev-parse bzr/#{remote}`
      `(#{PYTHON} #{EXPORT_LOCATION} --import-marks=#{bzr_map} --export-marks=#{bzr_map} --git-branch=bzr/#{remote} #{location}) | (git fast-import --quiet --export-marks=#{git_map} --import-marks=#{git_map})`
      new_rev = `git rev-parse bzr/#{remote}`
      puts "Changes since last update:"
      puts `git shortlog #{old_rev.strip}..#{new_rev.strip}`
    else
      puts "One of the mapfiles is missing! Something went wrong!"
    end
  end
  
  def push(*args)
    remote = args.shift
    unless remote && args.empty?
      puts "Usage: git bzr push branchname"
      exit
    end
    location = get_location(remote)
    
    if `git rev-list --left-right HEAD...bzr/#{remote} | sed -n '/^>/ p'`.strip != ""
      puts "HEAD is not a strict child of #{remote}, cannot push. Merge first"
      exit
    end
    
    if `git rev-list --left-right HEAD...bzr/#{remote} | sed -n '/^</ p'`.strip == ""
      puts "Nothing to push. Commit something first"
      exit
    end
    
    git_map = File.expand_path(File.join(git_dir, "bzr-git", "#{remote}-git-map"))
    bzr_map = File.expand_path(File.join(git_dir, "bzr-git", "#{remote}-bzr-map"))
 
    if !File.exists?(git_map) || !File.exists?(bzr_map)
      puts "We do not have refmapping yet. Then how can I push?"
      exit
    end
    
    `(git fast-export --import-marks=#{git_map} --export-marks=#{git_map} HEAD) | (cd #{location} && bzr fast-import --import-marks=#{bzr_map} --export-marks=#{bzr_map} -)`
  end
 
  def git_dir
    `git rev-parse --git-dir`.strip
  end
 
  def run(cmd, *args)
    `git rev-parse 2> /dev/null`
    if $? != 0
      puts "Must be inside a git repository to work"
      exit
    end
    up = `git rev-parse --show-cdup`.strip
    up = "." if up == ""
    Dir.chdir(up) do
      __send__(cmd.to_s, *args)
    end
  end
end
 
 
BzrCommands.new.run(command, *ARGV)