module Autoshell::Git

Git repo stuff @!attribute branch

@return [String] (master) branch of the current repo

Attributes

branch[W]
commit_hash_for_checkout[W]

Public Instance Methods

archive(branch_or_tag = nil) click to toggle source

Get a tar archive of the repo as a string

@param [String] (HEAD) Branch or tag @return [String] Zip-formatted binary string

# File lib/autoshell/git.rb, line 94
def archive(branch_or_tag = nil)
  git 'archive', branch_or_tag || 'HEAD', binmode: true
end
branch() click to toggle source
# File lib/autoshell/git.rb, line 8
def branch
  @branch ||= 'master'
end
branch_from_repo_url(repo_url) click to toggle source

Set the branch from a repo url

@param [String] Git repository URL @return [string] Branch name

# File lib/autoshell/git.rb, line 17
def branch_from_repo_url(repo_url)
  if repo_url =~ /#\S+[^\/]/
    @branch = repo_url.split('#')[1]
  else
    @branch = 'master'
  end
end
checkout_version(version_hash) click to toggle source

Checkout a specific hash on the repo

@param [String] Git version hash @return [String] Output of git commands

# File lib/autoshell/git.rb, line 77
def checkout_version(version_hash)
  git 'checkout', version_hash || 'HEAD'
end
clone(repo_url) click to toggle source

Clone a repo to disk from the url

@param [String] Git repository URL @return [String] Output of git commands

# File lib/autoshell/git.rb, line 66
def clone(repo_url)
  mkpdir working_dir
  run 'git', 'clone', '--recursive', repo_url.split('#')[0], working_dir
  branch_from_repo_url(repo_url)
  update
end
commit_hash(branch_or_tag = nil) click to toggle source

Get the current commit hash

@param [String] (HEAD) Branch or tag @return [String] Git commit hash

# File lib/autoshell/git.rb, line 85
def commit_hash(branch_or_tag = nil)
  @version = git 'rev-parse', branch_or_tag || 'HEAD'
end
Also aliased as: version
commit_hash_for_checkout() click to toggle source
# File lib/autoshell/git.rb, line 25
def commit_hash_for_checkout
  @commit_hash_for_checkout ||= 'HEAD'
end
git(*args) click to toggle source

Run a git command

@param [*Array<String>] Command @return [String] Command output

# File lib/autoshell/git.rb, line 102
def git(*args)
  cd { run(*['git'] + args) }
end
git?() click to toggle source

Has this repo been cloned to disk?

@return [Boolean] True if this dir is a cloned repo

# File lib/autoshell/git.rb, line 33
def git?
  dir? '.git'
end
switch(new_branch) click to toggle source

Checkout a different branch

@param [String] Branch name @return [String] Output of git commands

# File lib/autoshell/git.rb, line 57
def switch(new_branch)
  self.branch = new_branch
  update
end
update() click to toggle source

Update the repo on disk

@return [String] Output of git commands

# File lib/autoshell/git.rb, line 40
def update
  [
    git('checkout', '.'),
    git('clean', '-ffd'),
    git('fetch', 'origin'),
    git('checkout', branch),
    git('pull', '--recurse-submodules=yes'),
    git('checkout', commit_hash_for_checkout),
    git('submodule', 'update', '--init'),
    git('clean', '-ffd')
  ].join("\n")
end
version(branch_or_tag = nil)
Alias for: commit_hash