class Bookwatch::Ingest::GitAccessor

Constants

InvalidTagRef
TagExists

Public Instance Methods

author_date(path, exclusion_flag: '[exclude]', dita: false) click to toggle source
# File lib/bookwatch/ingest/git_accessor.rb, line 57
def author_date(path, exclusion_flag: '[exclude]', dita: false)
  fs = LocalFilesystemAccessor.new

  if dita
    source_dir = 'preprocessing'
    path_to_file = path.sub(/\.html(.md)?(.erb)?/, '.xml')
  else
    source_dir = source_dir_name
    path_to_file = path
  end


  Pathname(path).dirname.ascend do |current_dir|
    if (
        current_dir.to_s.include?(source_dir) &&
        current_dir.entries.include?(Pathname(".git")) &&
        fs.source_file_exists?(Pathname(path).dirname, path_to_file)
      )

      git = Git.open(current_dir)
      logs = git.gblob(path_to_file).log

      last_non_excluded_commit = logs.detect { |log| !log.message.include?(exclusion_flag) }

      return last_non_excluded_commit.author.date if last_non_excluded_commit
    end
  end
end
clone(url, name, path: nil, checkout: 'master') click to toggle source
# File lib/bookwatch/ingest/git_accessor.rb, line 14
def clone(url, name, path: nil, checkout: 'master')
  cached_clone(url, name, Pathname(path)).tap do |git|
    git.checkout(checkout)
  end
end
remote_tag(url, tagname, commit_or_object) click to toggle source
# File lib/bookwatch/ingest/git_accessor.rb, line 34
def remote_tag(url, tagname, commit_or_object)
  Dir.mktmpdir do |dir|
    path = Pathname(dir)
    git = cached_clone(url, temp_name("tag"), path)
    git.config('user.name', 'Bookwatch')
    git.config('user.email', 'bookwatch@cloudfoundry.org')
    begin
      git.add_tag(tagname, "origin/#{commit_or_object}",
        message: 'Tagged by Bookwatch')
    rescue Git::GitExecuteError => e
      case e.message
        when /already exists/
          raise TagExists
        when /as a valid ref/
          raise InvalidTagRef
        else
          raise
      end
    end
    git.push("origin", "master", tags: true)
  end
end
update(cloned_path) click to toggle source
# File lib/bookwatch/ingest/git_accessor.rb, line 20
def update(cloned_path)
  Git.open(cloned_path).pull
  Ingest::UpdateSuccess.new
rescue ArgumentError, Git::GitExecuteError => e
  case e.message
  when /overwritten by merge/
    Ingest::UpdateFailure.new('merge error')
  when /path does not exist/
    Ingest::UpdateFailure.new('not found')
  else
    raise
  end
end

Private Instance Methods

cached_clone(url, name, path) click to toggle source
# File lib/bookwatch/ingest/git_accessor.rb, line 92
def cached_clone(url, name, path)
  dest_dir = path.join(name)
  if dest_dir.exist?
    Git.open(dest_dir)
  else
    Git.clone(url, name, path: path, recursive: true)
  end
end
temp_name(purpose) click to toggle source
# File lib/bookwatch/ingest/git_accessor.rb, line 88
def temp_name(purpose)
  "bookwatch-git-accessor-#{purpose}"
end