Skip to content

HTTPS clone URL

Subversion checkout URL

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