module SyncWrap::GitHelp

Utility methods for sync file/sources kept in a git repository.

Public Class Methods

clean_hash() click to toggle source

Return a lambda that will require_clean! for callers path before providing git_hash. Use this for cases where you only want to use a git hash when it is an accurate reflection of the local file state. Since the test is deferred, it will only be required for actions (i.e. image creation, etc.) that actually use it.

# File lib/syncwrap/git_help.rb, line 48
def self.clean_hash
  cpath = caller_path( caller )
  lambda do
    require_clean!( cpath )
    git_hash( cpath )
git_hash( path = nil ) click to toggle source

Return the abbreviated SHA-1 hash for the last git commit at path

# File lib/syncwrap/git_help.rb, line 38
def self.git_hash( path = nil )
  path ||= caller_path( caller )
  %x`cd #{path} && git log -n 1 --format='format:%h'`
require_clean!( path = nil ) click to toggle source

Raises RuntimeError if the git tree at path (default to caller's path) is not clean

# File lib/syncwrap/git_help.rb, line 27
def self.require_clean!( path = nil )
  path ||= caller_path( caller )
  delta = %x`cd #{path} && git status --porcelain -- . 2>&1`
  if delta.split( /^/ ).length > 0
    warn( "Commit or move these first:\n" + delta )
    raise "Git repo at #{path} not clean"