From c46bbdb77dfea8b0b7e0268218e4a0e9bc9501f3 Mon Sep 17 00:00:00 2001 From: Andy Borsz Date: Tue, 2 Oct 2012 13:21:17 -0400 Subject: [PATCH] Check Git version before copying local config --- .gitconfig | 9 ++----- .gitconfig.local | 5 ++++ bin/git_configuration_handler.rb | 44 ++++++++++++++++++++++++++++++++ bin/hlink | 7 ++++- 4 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 .gitconfig.local create mode 100644 bin/git_configuration_handler.rb diff --git a/.gitconfig b/.gitconfig index 38e7033a..f4b1255e 100644 --- a/.gitconfig +++ b/.gitconfig @@ -24,10 +24,10 @@ excludesfile = ~/.cvsignore editor = vim whitespace = warn -[github] - user = hashrocketeer [help] autocorrect = 10 +[include] + path = ~/.gitconfig.local [interactive] singlekey = true [merge] @@ -52,8 +52,3 @@ default = tracking [rebase] autosquash = true -[user] - email = dev@hashrocket.com - name = "Hashrocket Workstation" -[include] - path = .gitconfig.local diff --git a/.gitconfig.local b/.gitconfig.local new file mode 100644 index 00000000..5aa15bb3 --- /dev/null +++ b/.gitconfig.local @@ -0,0 +1,5 @@ +[github] + user = hashrocketeer +[user] + email = dev@hashrocket.com + name = "Hashrocket Workstation" diff --git a/bin/git_configuration_handler.rb b/bin/git_configuration_handler.rb new file mode 100644 index 00000000..6f71aa15 --- /dev/null +++ b/bin/git_configuration_handler.rb @@ -0,0 +1,44 @@ +class GitConfigurationHandler + + LocalGitConfigCopiedMessage = "\nGeneric Hashrocket Git credentials \ +have been configured in ~/.gitconfig.local.\nReplace this with your \ +own info if this is a personal machine.\n\n" + + UnsupportedGitVersionMessage = "\nYour Git version does not support \ +local configuration files. We highly recommend updating to the latest \ +version.\n\n" + + def initialize(home) + @home = home + if system_git_version_supports_local_config? + apply_local_gitconfig unless local_git_config_present? + else + puts UnsupportedGitVersionMessage + end + end + + def target_git_version + Gem::Version.new('1.7.10') + end + + def system_git_version + Gem::Version.new(extracted_version_number) + end + + def extracted_version_number + `git --version`.scan(/\d+/).join('.') + end + + def system_git_version_supports_local_config? + system_git_version >= target_git_version + end + + def local_git_config_present? + File.exist?(@home + '/.gitconfig.local') + end + + def apply_local_gitconfig + FileUtils.cp '.gitconfig.local', @home + '/.gitconfig.local' + puts LocalGitConfigCopiedMessage + end +end diff --git a/bin/hlink b/bin/hlink index b1a49b87..525355ba 100755 --- a/bin/hlink +++ b/bin/hlink @@ -1,7 +1,8 @@ #!/usr/bin/env ruby require 'fileutils' +require_relative 'git_configuration_handler' -EXCLUDES = %w(bin custom . .. .git README.md) +EXCLUDES = %w(bin custom . .. .git README.md .gitconfig.local) def cwd @cwd ||= File.expand_path(File.dirname(__FILE__)) @@ -80,3 +81,7 @@ directories_at(File.join(root,'custom')).each do |custom| install(file,dest_dir) end end + +# install a .gitconfig.local if Git version +# is compatible and file not already present +GitConfigurationHandler.new(@home)