Skip to content

Commit

Permalink
Updated to be more git-friendly
Browse files Browse the repository at this point in the history
This update provides the following capabilities:

* Users can now set the config boolean GIT_DESTRUCTIVE to have git
  repositories be force cleaned.
* Target git repositories are treated as git repos and not just blindly
  overwritten
* The branch name in the Puppetfile is preserved on git checkout
* The VERBOSE flag in the config file will now output the module that is
  being processed. This provides a sane verbosity between debug and
  nothing.
  • Loading branch information
trevor-vaughan committed Dec 16, 2015
1 parent df85a34 commit 5483226
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 6 deletions.
5 changes: 5 additions & 0 deletions lib/librarian/puppet/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def init
option "local", :type => :boolean, :default => false
option "use-v1-api", :type => :boolean, :default => true
option "use-forge", :type => :boolean
option "git-destructive", :type => :boolean
def install

ensure!
Expand All @@ -67,6 +68,9 @@ def install
unless options["use-forge"].nil?
environment.config_db.local['use-forge'] = options['use-forge'].to_s
end
unless options["git-destructive"].nil?
environment.config_db.local['git-destructive'] = options['git-destructive'].to_s
end
environment.config_db.local['mode'] = options['local'] ? 'local' : nil

resolve!
Expand All @@ -89,6 +93,7 @@ def update(*names)
option "path", :type => :string
option "destructive", :type => :boolean, :default => false
option "use-forge", :type => :boolean
option "git-destructive", :type => :boolean
def package
environment.vendor!
install
Expand Down
8 changes: 8 additions & 0 deletions lib/librarian/puppet/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def use_v1_api
def use_forge
config_db['use-forge'].to_s == 'false' ? false : true
end

def git_destructive
config_db['git-destructive'].to_s == 'false' ? false : true
end

def verbose?
config_db['verbose'].to_s == 'false' ? false : true
end
end
end
end
52 changes: 52 additions & 0 deletions lib/librarian/puppet/source/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Source
class Git
class Repository
def hash_from(remote, reference)

branch_names = remote_branch_names[remote]
if branch_names.include?(reference)
reference = "#{remote}/#{reference}"
Expand All @@ -14,6 +15,57 @@ def hash_from(remote, reference)
command = %W(rev-parse #{reference}^{commit} --quiet)
run!(command, :chdir => true).strip
end

# Return true if the repository has local modifications, false otherwise.
def dirty?
# Ignore anything that's not a git repository
# This is relevant for testing scenarios
return false unless self.git?

status = false
_path = relative_path_to(path).to_s
begin
Librarian::Posix.run!(%W{git update-index -q --ignore-submodules --refresh}, :chdir => _path)
rescue Librarian::Posix::CommandFailure => e
status = "Could not update git index for '#{path}'"
end

unless status
begin
Librarian::Posix.run!(%W{git diff-files --quiet --ignore-submodules --}, :chdir => _path)
rescue Librarian::Posix::CommandFailure => e
status = "'#{_path}' has unstaged changes"
end
end

unless status
begin
Librarian::Posix.run!(%W{git diff-index --cached --quiet HEAD --ignore-submodules --}, :chdir => _path)
rescue Librarian::Posix::CommandFailure => e
status = "'#{_path}' has uncommitted changes"
end
end

unless status
begin
untracked_files = Librarian::Posix.run!(%W{git ls-files -o -d --exclude-standard}, :chdir => _path)

unless untracked_files.empty?
untracked_files.strip!

if untracked_files.lines.count > 0
status = "'#{_path}' has untracked files"
end
end

rescue Librarian::Posix::CommandFailure => e
# We should never get here
raise Error, "Failure running 'git ls-files -o -d --exclude-standard' at '#{_path}'"
end
end

status
end
end
end
end
Expand Down
74 changes: 69 additions & 5 deletions lib/librarian/puppet/source/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module Local
def install!(manifest)
manifest.source == self or raise ArgumentError

if environment.verbose?
info { "Processing #{manifest.name}" }
end

debug { "Installing #{manifest}" }

name, version = manifest.name, manifest.version
Expand All @@ -30,12 +34,72 @@ def install!(manifest)
install_path = environment.project_path + path.to_s
end

if install_path.exist? && rsync? != true
debug { "Deleting #{relative_path_to(install_path)}" }
install_path.rmtree
end
install_repo = Git::Repository.new(environment,install_path)

if install_repo.git?
_install_path = relative_path_to(install_path)

if environment.git_destructive
debug { "Performing git hard reset of '#{_install_path}'" }

install_repo.reset_hard!
install_repo.clean!
end

if install_repo.dirty?
warn { "#{install_repo.dirty?}, skipping..." }
else
# Try to do nicer git operations when possible
_remote_repo = 'librarian_origin'

begin
Librarian::Posix.run!(%W{git remote add #{_remote_repo} #{repository_cache_path}}, :chdir => _install_path)
rescue Librarian::Posix::CommandFailure => e
unless e.to_s =~ /already exists/
raise Error, "Could not update git repository at #{_install_path}"
end
end

install_repo.fetch!(_remote_repo)

if environment.verbose?
warn "Checking out #{ref} in #{_install_path}"
end
install_repo.checkout!(ref)

install_perform_step_copy!(found_path, install_path)
begin
_target_ref = ref

# Handle branches vs absolute refs
if repository.remote_branch_names[repository.default_remote].include?(_target_ref)
_target_ref = "#{repository.default_remote}/#{_target_ref}"
end

ff_output = Librarian::Posix.run!(%W{git pull --ff-only #{_remote_repo} #{_target_ref}}, :chdir => _install_path)

if ff_output =~ /Updating\s+.*\.\.(.*)\s*$/
warn { "Updated '#{_install_path}' to #{$1}" }
end
rescue Librarian::Posix::CommandFailure => e
# We should not get here unless something went VERY wrong
fail Error, "Fast forward of git repo from #{_remote_repo} to '#{_install_path}' failed\n#{e}"
end

begin
Librarian::Posix.run!(%W{git remote rm #{_remote_repo}}, :chdir => _install_path)
rescue Librarian::Posix::CommandFailure => e
# We don't really care if this fails.
debug { "Removal of the '#{_remote_repo}' git remote failed" }
end
end
else
if install_path.exist? && rsync? != true
debug { "Deleting #{relative_path_to(install_path)}" }
install_path.rmtree
end

install_perform_step_copy!(found_path, install_path)
end
end

def fetch_version(name, extra)
Expand Down
2 changes: 1 addition & 1 deletion lib/librarian/puppet/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Librarian
module Puppet
VERSION = "2.2.1"
VERSION = "2.2.3"
end
end

0 comments on commit 5483226

Please sign in to comment.