Class: StructureSqlMergeDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/git-merge-structure-sql.rb

Overview

git-merge-structure-sql - git merge driver for db/structure.sql in a Rails project

How to use:

$ git-merge-structure-sql --install

Copyright © 2018-2021 Akinori MUSHA

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Defined Under Namespace

Modules: Default, MySQL

Constant Summary collapse

VERSION =
'1.1.2'
VARIANTS =
[]

Instance Method Summary collapse

Instance Method Details

#main(*argv) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/git-merge-structure-sql.rb', line 135

def main(*argv)
  require 'optparse'
  require 'shellwords'

  myname = File.basename($0)
  driver_name = 'merge-structure-sql'

  banner = "    \#{myname} - git merge driver for db/structure.sql in a Rails project \#{VERSION}\n\n    usage: \#{myname} <current-file> <base-file> <other-file>\n           \#{myname} --install\n\n  EOF\n\n  install = false\n\n  opts = OptionParser.new(banner) { |opts|\n    opts.version = VERSION\n    opts.summary_indent = ''\n    opts.summary_width = 24\n\n    opts.on('--install[={global|local}]',\n      \"Enable this merge driver in Git (default: global)\") { |val|\n      case install = val&.to_sym || :global\n      when :global, :local\n        # ok\n      else\n        raise OptionParser::InvalidArgument, \"--install=\#{val}: unknown argument\"\n      end\n    }\n  }\n\n  files = opts.order(argv)\n\n  if install\n    global = install == :global\n\n    git_config = [\"git\", \"config\", *(\"--global\" if global)]\n\n    config_file = `GIT_EDITOR=echo \#{git_config.shelljoin} -e 2>/dev/null`.chomp\n\n    puts \"\#{config_file}: Adding the \\\"\#{driver_name}\\\" driver definition\"\n\n    system!(\n      *git_config,\n      \"merge.\#{driver_name}.name\",\n      \"Rails structure.sql merge driver\"\n    )\n    system!(\n      *git_config,\n      \"merge.\#{driver_name}.driver\",\n      \"\#{myname.shellescape} %A %O %B\"\n    )\n\n    attributes_file =\n      if global\n        filename = `git config --global core.attributesfile`.chomp\n\n        if $?.success?\n          File.expand_path(filename)\n        else\n          [\n            [File.join(ENV['XDG_CONFIG_HOME'] || '~/.config', 'git'), 'attributes'],\n            ['~', '.gitattributes']\n          ].find { |dir, file|\n            if File.directory?(File.expand_path(dir))\n              system!(*%W[\n                git config --global core.attributesfile \#{File.join(dir, file)}\n              ])\n              break File.expand_path(file, dir)\n            end\n          } or raise \"don't you have home?\"\n        end\n      else\n        git_dir = `git rev-parse --git-dir`.chomp\n\n        if $?.success?\n          File.expand_path(File.join('info', 'attributes'), git_dir)\n        else\n          raise \"not in a git directory\"\n        end\n      end\n\n    File.open(attributes_file, 'a+') { |f|\n      pattern = /^\\s*structure.sql\\s+(?:\\S+\\s+)*merge=\#{Regexp.quote(driver_name)}(?:\\s|$)/\n      break if f.any? { |line| pattern === line }\n\n      puts \"\#{attributes_file}: Registering the \\\"\#{driver_name}\\\" driver for structure.sql\"\n      f.puts if f.pos > 0\n      f.puts \"structure.sql merge=\#{driver_name}\"\n    }\n\n    exit\n  end\n\n  unless files.size == 3\n    STDERR.print opts.help\n    exit 64\n  end\n\n  contents = files.map { |file| File.read(file) }\n\n  sample = contents[0]\n  if variant = VARIANTS.find { |v| v.match?(sample) }\n    variant.merge!(*contents)\n\n    files.zip(contents) do |file, content|\n      File.write(file, content)\n    end\n  else\n    STDERR.puts \"\#{myname}: Unsupported format; falling back to git-merge-file(1)\"\n  end\n\n  exec(*%W[git merge-file -q], *files)\nrescue => e\n  STDERR.puts \"\#{myname}: \#{e.message}\"\n  exit 1\nend\n"

#system!(*args) ⇒ Object



131
132
133
# File 'lib/git-merge-structure-sql.rb', line 131

def system!(*args)
  system(*args) or exit $?.exitstatus
end