Class: Au::Staging

Inherits:
Object show all
Defined in:
lib/au/models/staging.rb

Class Method Summary collapse

Class Method Details

.abs_path(relative_path) ⇒ Object



87
88
89
90
# File 'lib/au/models/staging.rb', line 87

def self.abs_path(relative_path)
  paths = [Repository.root, relative_path].compact
  File.join(*paths)
end

.clear_staged_file_pathsObject



65
66
67
# File 'lib/au/models/staging.rb', line 65

def self.clear_staged_file_paths
  File.delete stage_db.path
end

.filter_stageable(last_commit, relative_paths) ⇒ Object

private



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/au/models/staging.rb', line 46

def self.filter_stageable(last_commit, relative_paths)
  tracked_docs_md5 = last_commit&.tracked_docs_md5 || {}
  relative_paths.reject do |relative_path|
    known_md5 = tracked_docs_md5[relative_path]
    abs_path = File.join(Repository.root, relative_path)
    if File.exists?(abs_path)
      known_md5 && Digest::MD5.hexdigest(File.read(abs_path)) == known_md5
    else
      known_md5.nil?
    end
  end
end

.project_file_pathsObject



92
93
94
95
96
# File 'lib/au/models/staging.rb', line 92

def self.project_file_paths
  Dir.glob('**/*', base: Repository.root).reject do |file_path|
    File.directory? file_path
  end
end

.stage(relative_paths, last_commit = Repository.head) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/au/models/staging.rb', line 25

def self.stage(relative_paths, last_commit = Repository.head)
  relative_paths = relative_paths.each_with_object([]) do |staged_path, accum|
    if File.directory?(File.join(Repository.root, staged_path))
      Dir.glob('**/*', base: File.join(Repository.root, staged_path)) do |sub_path|
        accum << (File.join(staged_path, sub_path)) unless File.directory?(File.join(
            Repository.root, staged_path, sub_path))
      end
    else
      accum << staged_path
    end
  end

  stage_db.transaction do
    filter_stageable(last_commit, relative_paths).each do |relative_path|
      stage_db[relative_path] = 1
    end
  end
  relative_paths
end

.stage_dbObject

private



99
100
101
# File 'lib/au/models/staging.rb', line 99

def self.stage_db
  @stage_db ||= PStore.new(File.join(*[Repository.path, 'stage.pstore'].compact))
end

.staged_file_pathsObject



59
60
61
62
63
# File 'lib/au/models/staging.rb', line 59

def self.staged_file_paths
  stage_db.transaction(true) do
    stage_db.roots
  end
end

.status(last_commit = Repository.head) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/au/models/staging.rb', line 7

def self.status(last_commit = Repository.head)
  return project_file_paths unless last_commit
  changed_doc_paths = []
  tracked_docs_md5 = last_commit.tracked_docs_md5
  project_file_paths.each do |relative_path|
    known_md5 = tracked_docs_md5[relative_path]
    # file not tracked or tracked but hasn't changed
    if known_md5.nil? || Digest::MD5.hexdigest(File.read(abs_path(relative_path))) != known_md5
      changed_doc_paths << relative_path
    end
    tracked_docs_md5.delete(relative_path) if known_md5
  end

  # remaining files in tracked_docs_md5 are no longer in the project
  changed_doc_paths += tracked_docs_md5.keys unless tracked_docs_md5.empty?
  changed_doc_paths
end

.unstage(file_paths) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/au/models/staging.rb', line 69

def self.unstage(file_paths)
  file_paths = file_paths.each_with_object([]) do |staged_path, accum|
    if File.directory?(staged_path)
      Dir.glob('**/*', base: staged_path) do |sub_path|
        accum << (File.join(staged_path, sub_path)) unless File.directory?(sub_path)
      end
    else
      accum << staged_path
    end
  end

  stage_db.transaction do
    file_paths.each do |file_path|
      stage_db.delete(file_path)
    end
  end
end