class GithubPages::GitManager

Constants

CREDENTIALS_FILE

Attributes

branch[R]
remote[R]
repo[R]

Public Class Methods

new(remote, preserved_dir, repo = nil, branch = nil) click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 9
def initialize(remote, preserved_dir, repo = nil, branch = nil)
  @preserved = preserved_dir
  @remote = remote
  @repo = repo || git("config remote.#{remote}.url").chomp.gsub(/^git:/, 'https:')
  @branch = branch || default_branch

  @git = Git.open(Dir.pwd)

  setup
end
open(*args) { |git| ... } click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 28
def self.open(*args, &block)
  git = new(*args, &block)
  yield git
  git.cleanup
end

Public Instance Methods

cleanup() click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 34
def cleanup
  cleanup_credentials
end
commit_and_push(msg) click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 50
def commit_and_push(msg)
  @git.commit(msg)
  @git.push(remote, "ghpages_deployment:#{branch}")
end
default_branch() click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 20
def default_branch
  if @repo =~ /\.github\.(?:com|io)\.git$/
    'master'
  else
    'gh-pages'
  end
end
ls_files(dir) click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 46
def ls_files(dir)
  cached_files('ls-files', dir)
end
remove(*files) click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 55
def remove(*files)
  @git.remove(files) unless files.empty?
end
stage(files) click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 38
def stage(files)
  @git.add files unless files.empty?
end
staged_modifications(dir) click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 42
def staged_modifications(dir)
  cached_files('diff --name-only', dir)
end

Private Instance Methods

cached_files(cmd, dir) click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 63
def cached_files(cmd, dir)
  git("#{cmd} --cached -z #{dir}").split("\0")
end
cleanup_credentials() click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 122
def cleanup_credentials
  # TODO
end
git(command) click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 69
def git(command)
  `git #{command}`
end
remote_branch?() click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 97
def remote_branch?
  @git.branches.remote.any? do |br|
    br.name == branch && br.remote.name == remote
  end
end
remove_staged() click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 91
def remove_staged
  remove(*staged_modifications('.'))

  git "clean -d -x -f -e #{@preserved}"
end
setup() click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 73
def setup
  setup_repo
  setup_user
  setup_credentials
  setup_branch
end
setup_branch() click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 103
def setup_branch
  if remote_branch?
    git "checkout -b ghpages_deployment #{remote}/#{branch}"
    remove_staged
    @git.pull(remote, branch)
  else
    git "checkout --orphan ghpages_deployment"
    remove_staged
  end
end
setup_credentials() click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 114
def setup_credentials
  @git.config('credential.helper', "store --file=#{CREDENTIALS_FILE}")
  File.open(CREDENTIALS_FILE, 'w') do |file|
    token = ENV['GITHUB_OAUTH_TOKEN']
    file.write "https://#{token}:x-oauth-basic@github.com"
  end
end
setup_repo() click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 80
def setup_repo
  @git.remove_remote(remote) if @git.remotes.map(&:name).include?(remote)
  @git.add_remote(remote, repo)
  @git.remote(remote).fetch
end
setup_user() click to toggle source
# File lib/ghpages_deploy/git_manager.rb, line 86
def setup_user
  @git.config('user.name', ENV['GIT_NAME']) if ENV['GIT_NAME']
  @git.config('user.email', ENV['GIT_EMAIL']) if ENV['GIT_EMAIL']
end