# Support functions used by the Pallet Jack tools

require 'rugged'

# Return a string stating the Git provenance of a warehouse directory,
# suitable for inclusion at the top of a generated configuration file,
# with each line prefixed by +comment_char+.
#
# If +warehouse_path+ points within a Git repository, return a string
# stating its absolute path, active branch and commit ID.
#
# If Git information cannot be found for +warehouse_path+, return a
# string stating its path and print an error message on stderr.

def git_header(tool_name, warehouse_path, comment_char = '#')
  repo = Rugged::Repository.discover(warehouse_path)
  workdir = repo.workdir
  branch = repo.head.name
  commit = repo.head.target_id
  return "#{comment_char}#{comment_char}
#{comment_char}#{comment_char} Automatically generated by #{tool_name} from
#{comment_char}#{comment_char} Repository: #{workdir}
#{comment_char}#{comment_char} Branch: #{branch}
#{comment_char}#{comment_char} Commit ID: #{commit}
#{comment_char}#{comment_char}

"
rescue
  STDERR.puts "Error finding Git sourcing information: #{$!}"
  return "#{comment_char}#{comment_char}
#{comment_char}#{comment_char} Automatically generated by #{tool_name} from #{warehouse_path}
#{comment_char}#{comment_char}

"
end

Move function for writing Git provenance header into a support module
Move function for writing Git provenance header into a support module

All tools will want to use this function, so move it out of the tool itself to
a common file.
