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