Class: GitSVNMirror

Inherits:
Object
  • Object
show all
Defined in:
lib/git-svn-mirror.rb

Constant Summary collapse

VERSION =
"0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#authors_fileObject

Returns the value of attribute authors_file.



6
7
8
# File 'lib/git-svn-mirror.rb', line 6

def authors_file
  @authors_file
end

#fromObject

Returns the value of attribute from.



6
7
8
# File 'lib/git-svn-mirror.rb', line 6

def from
  @from
end

#silentObject

Returns the value of attribute silent.



6
7
8
# File 'lib/git-svn-mirror.rb', line 6

def silent
  @silent
end

#toObject

Returns the value of attribute to.



6
7
8
# File 'lib/git-svn-mirror.rb', line 6

def to
  @to
end

#workbenchObject

Returns the value of attribute workbench.



6
7
8
# File 'lib/git-svn-mirror.rb', line 6

def workbench
  @workbench
end

Class Method Details

.init(mirror, argv) ⇒ Object



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
# File 'lib/git-svn-mirror.rb', line 30

def self.init(mirror, argv)
  opts = option_parser(mirror, argv) do |o|
    o.banner = "Usage: git-svn-mirror init [mandatory options] [options]"
    o.separator "\n  Mandatory options are --from and --to.\n\n"
    o.on('--from URI',          'The location of the SVN repository that is to be mirrored.')                  { |uri| mirror.from = uri }
    o.on('--to URI',            'The location of the GIT repository that is the mirror.')                      { |uri| mirror.to = uri }
    o.on('--workbench PATH',    'The location of the workbench repository. Defaults to the current work dir.') { |wb|  mirror.workbench = wb }
    o.on('--authors-file PATH', 'An optional authors file used to migrate SVN usernames to GIT\'s format.')    { |af|  mirror.authors_file = af }
  end

  if mirror.from && mirror.to
    if !File.exist?(mirror.workbench)
      puts "[!] Given workbench path does not exist."
      false
    else
      if mirror.authors_file && !File.exist?(mirror.authors_file)
        puts "[!] Given authors file does not exist."
        false
      else
        mirror.init
        true
      end
    end
  else
    puts opts
    false
  end
end

.option_parser(mirror, argv) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/git-svn-mirror.rb', line 20

def self.option_parser(mirror, argv)
  opts = OptionParser.new do |o|
    yield(o)
    o.on('-s', '--silent',  'Silent mode.')       { mirror.silent = true }
    o.on('-v', '--version', 'Print the version.') { puts VERSION; exit }
  end
  opts.parse!(argv)
  opts
end

.run(argv) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/git-svn-mirror.rb', line 8

def self.run(argv)
  mirror = new
  status = case argv.shift
           when 'init'   then init(mirror, argv)
           when 'update' then update(mirror, argv)
           else
             puts "Usage: git-svn-mirror [init|update]"
             false
           end
  [mirror, status]
end

.update(mirror, argv) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/git-svn-mirror.rb', line 59

def self.update(mirror, argv)
  opts = option_parser(mirror, argv) do |o|
    o.banner = "Usage: git-svn-mirror update [options] [workbench1] [workbench2] ..."
    o.separator "\n  Defaults to the current work dir if none is given.\n\n"
  end

  if argv.empty?
    mirror.update
  else
    argv.each do |workbench|
      mirror.workbench = workbench
      mirror.update
    end
  end
  true
end

Instance Method Details

#config(key) ⇒ Object



135
136
137
138
# File 'lib/git-svn-mirror.rb', line 135

def config(key)
  value = sh("git config --get #{key}", true)
  value unless value.empty?
end

#fetchObject



101
102
103
104
# File 'lib/git-svn-mirror.rb', line 101

def fetch
  log "* Fetching from SVN repo at `#{from}'"
  sh "git svn fetch"
end

#initObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/git-svn-mirror.rb', line 76

def init
  log "* Configuring mirror workbench at `#{workbench}'"
  sh "git init --bare"

  sh "git svn init --stdlayout --prefix=svn/ #{from}"
  sh "git config --add svn.authorsfile '#{authors_file}'" if authors_file

  sh "git remote add origin #{to}"
  sh "git config --add remote.origin.push 'refs/remotes/svn/*:refs/heads/*'"

  fetch
  log "* Running garbage collection"
  sh "git gc"

  log "The mirror workbench has been configured. To push to the remote GIT repo,",
      "and possibly as a cron job, run the following command:",
      "",
      "  $ git-svn-mirror update '#{workbench}'"
end

#log(*str) ⇒ Object



131
132
133
# File 'lib/git-svn-mirror.rb', line 131

def log(*str)
  puts("\n#{str.join("\n")}\n\n") unless @silent
end

#pushObject



106
107
108
109
# File 'lib/git-svn-mirror.rb', line 106

def push
  log "* Pushing to GIT repo at `#{to}'"
  sh "git push origin"
end

#sh(command, capture = false) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/git-svn-mirror.rb', line 140

def sh(command, capture = false)
  Dir.chdir(workbench) do
    command = "env GIT_DIR='#{workbench}' #{command}"
    command += (@silent ? " > /dev/null 2>&1" : " 1>&2") unless capture
    `#{command}`.strip
  end
end

#updateObject



96
97
98
99
# File 'lib/git-svn-mirror.rb', line 96

def update
  fetch
  push
end