Class: Bookwatch::Ingest::GitAccessor

Inherits:
Object
  • Object
show all
Includes:
DirectoryHelperMethods
Defined in:
lib/bookwatch/ingest/git_accessor.rb

Constant Summary collapse

TagExists =
Class.new(RuntimeError)
InvalidTagRef =
Class.new(RuntimeError)

Instance Method Summary collapse

Instance Method Details

#author_date(path, exclusion_flag: '[exclude]', dita: false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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') ⇒ Object



14
15
16
17
18
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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', '[email protected]')
    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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# 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