Class: GitNoted::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/git_noted/repository.rb

Defined Under Namespace

Classes: Note

Instance Method Summary collapse

Constructor Details

#initialize(remote_url, local_path, username: nil, password: nil, logger: Logger.new(STDERR)) ⇒ Repository

Returns a new instance of Repository.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/git_noted/repository.rb', line 17

def initialize(remote_url, local_path, username: nil, password: nil, logger: Logger.new(STDERR))
  @local_path = Pathname.new(local_path).cleanpath
  @remote_url = remote_url
  if username
    @credentials = Rugged::Credentials::UserPassword.new(username: username, password: password)
  else
    @credentials = nil
  end
  @logger = logger

  # initial update
  update! || index!
end

Instance Method Details

#read(note) ⇒ Object



45
46
47
# File 'lib/git_noted/repository.rb', line 45

def read(note)
  File.read(note.path).sub(/^:label:(.*)$\n/, '')
end

#schedule_update!(interval) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/git_noted/repository.rb', line 49

def schedule_update!(interval)
  Concurrent::ScheduledTask.execute(interval) do
    begin
      update!
    rescue => e
      @logger.error "Failed to update repository: #{e}"
      e.backtrace.each do |bt|
        @logger.error "  #{bt}"
      end
    ensure
      schedule_update!(interval)
    end
  end
end

#search_labels(prefix: nil, used_with: nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/git_noted/repository.rb', line 74

def search_labels(prefix: nil, used_with: nil)
  match = {}
  @notes.each do |note|
    if used_with.nil? || used_with.all? {|t| note.labels.include?(t) }
      if prefix.nil?
        matching = note.labels
      else
        matching = note.labels.select {|label| label.start_with?(prefix) }
      end
      matching.each {|label| match[label] = true }
    end
  end
  match.keys.sort
end

#search_notes(labels: nil, exclude_labels: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/git_noted/repository.rb', line 64

def search_notes(labels: nil, exclude_labels: nil)
  labels ||= []
  exclude_labels ||= []

  @notes.select do |note|
    labels.all? {|t| note.labels.include?(t) } &&
      !exclude_labels.any? {|t| note.labels.include?(t) }
  end
end

#update!Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/git_noted/repository.rb', line 31

def update!
  begin
    @repo ||= Rugged::Repository.init_at(@local_path.to_s)
    updated = fetch(@repo)
    return false unless updated
  rescue => e
    # fall back to clone.
    @logger.warn "Failed to update repository incrementally. Falling back to clone: #{e}"
  end
  @repo = clone
  index!
  return true
end