Optionally use git to keep brew etc versioned.

Still in alpha state.
Handles defaults and merging changes with new versions.
Enable by setting the HOMEBREW_GIT_ETC environment variable.

Closes Homebrew/homebrew#15751.
Closes Homebrew/homebrew#17713.
This commit is contained in:
Mike McQuaid 2013-09-01 16:03:02 +01:00
parent 3e7da6b16e
commit ab20ed0bc2
5 changed files with 62 additions and 2 deletions

View File

@ -142,7 +142,7 @@ class Formula
def kext_prefix; prefix+'Library/Extensions' end
# configuration needs to be preserved past upgrades
def etc; HOMEBREW_PREFIX+'etc' end
def etc; HOMEBREW_GIT_ETC ? prefix+'etc' : HOMEBREW_PREFIX+'etc' end
# generally we don't want var stuff inside the keg
def var; HOMEBREW_PREFIX+'var' end

View File

@ -71,6 +71,58 @@ class FormulaInstaller
raise
end
def git_etc_preinstall
return unless quiet_system 'git', '--version'
etc = HOMEBREW_PREFIX+'etc'
etc.cd do
quiet_system 'git', 'init' unless (etc+'.git').directory?
quiet_system 'git', 'checkout', '-B', "#{f.name}-last"
system 'git', 'add', '.'
system 'git', 'commit', '-m', "#{f.name}-#{f.version}: preinstall"
end
end
def git_etc_postinstall
return unless quiet_system 'git', '--version'
etc = HOMEBREW_PREFIX+'etc'
keg_etc_files = Dir[f.etc+'*']
last_branch = "#{f.name}-last"
default_branch = "#{f.name}-default"
merged = false
etc.cd do
FileUtils.cp_r keg_etc_files, etc
system 'git', 'add', '.'
if quiet_system 'git', 'diff', '--exit-code', default_branch
quiet_system 'git', 'reset', '--hard'
else
if quiet_system 'git', 'rev-parse', 'master'
quiet_system 'git', 'checkout', '-f', 'master'
FileUtils.cp_r keg_etc_files, etc
quiet_system 'git', 'add', '.'
else
quiet_system 'git', 'checkout', '-b' 'master'
end
system 'git', 'commit', '-m', "#{f.name}-#{f.version}: default"
quiet_system 'git', 'branch', '-f', default_branch
merged = true unless quiet_system 'git' 'merge-base', '--is-ancestor',
last_branch, 'master'
system 'git', 'merge', '--no-ff', '--no-edit',
'-X', 'theirs', last_branch
end
if merged
ohai "Configuration Files"
puts "Your configuration files for #{f.name} in etc were merged:"
puts "To reverse this merge: git reset --hard #{last_branch}"
puts "To restore defaults: git reset --hard #{default_branch}"
end
end
end
def install
# not in initialize so upgrade can unlink the active keg before calling this
# function but after instantiating this class so that it can avoid having to
@ -110,7 +162,10 @@ class FormulaInstaller
@@attempted << f
git_etc_preinstall if HOMEBREW_GIT_ETC
@poured_bottle = false
begin
if pour_bottle? true
pour
@ -136,6 +191,8 @@ class FormulaInstaller
opoo "#{f.name} post_install failed. Rerun with `brew postinstall #{f.name}`."
end
git_etc_postinstall if HOMEBREW_GIT_ETC
opoo "Nothing was installed to #{f.prefix}" unless f.installed?
end

View File

@ -84,6 +84,8 @@ HOMEBREW_USER_AGENT = "Homebrew #{HOMEBREW_VERSION} (Ruby #{RUBY_VERSION}-#{RUBY
HOMEBREW_CURL_ARGS = '-f#LA'
HOMEBREW_GIT_ETC = !ENV['HOMEBREW_GIT_ETC'].nil?
module Homebrew extend self
include FileUtils

View File

@ -114,7 +114,7 @@ class Keg < Pathname
# yeah indeed, you have to force anything you need in the main tree into
# these dirs REMEMBER that *NOT* everything needs to be in the main tree
link_dir('etc', mode) {:mkpath}
link_dir('etc', mode) {:mkpath} unless HOMEBREW_GIT_ETC
link_dir('bin', mode) {:skip_dir}
link_dir('sbin', mode) {:skip_dir}
link_dir('include', mode) {:link}

View File

@ -25,6 +25,7 @@ HOMEBREW_USER_AGENT = 'Homebrew'
HOMEBREW_WWW = 'http://example.com'
HOMEBREW_CURL_ARGS = '-fsLA'
HOMEBREW_VERSION = '0.9-test'
HOMEBREW_GIT_ETC = false
RUBY_BIN = Pathname.new(RbConfig::CONFIG['bindir'])
RUBY_PATH = RUBY_BIN + RbConfig::CONFIG['ruby_install_name'] + RbConfig::CONFIG['EXEEXT']