Class: Git::Fancy::BranchHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/git/fancy/branch_history.rb

Overview

SQLite3 database to keep track of recent branches

Defined Under Namespace

Classes: Branch

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/git/fancy/branch_history.rb', line 12

def logger
  @logger
end

Instance Method Details

#add_branch(branch_name) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/git/fancy/branch_history.rb', line 42

def add_branch(branch_name)
  sql = <<~SQL
    INSERT INTO recent_branches (name, last_accessed) VALUES (?, ?)
      ON CONFLICT DO UPDATE SET last_accessed=?
  SQL
  time = Time.now.to_i
  db.execute(sql, [branch_name, time, time])
end

#add_note(note, branch_name = nil) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/git/fancy/branch_history.rb', line 66

def add_note(note, branch_name = nil)
  branch_name ||= current_branch

  sql = <<~SQL
    UPDATE recent_branches SET note = ? WHERE name = ?
  SQL
  db.execute(sql, [note, branch_name])
end

#branches_matching_notes(search_term) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/git/fancy/branch_history.rb', line 75

def branches_matching_notes(search_term)
  sql = <<~SQL
    SELECT name, last_accessed, note
    FROM recent_branches
    WHERE note IS NOT NULL
      AND LOWER(note) LIKE ?
    ORDER BY last_accessed DESC
  SQL
  to_branches db.execute(sql, ["%#{search_term.downcase}%"])
end

#clean_missing_branchesObject

Removes branches in the db that no longer exist locally



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/git/fancy/branch_history.rb', line 52

def clean_missing_branches
  sql = <<~SQL
    select name from recent_branches where name NOT IN (#{local_branch_names.map { |b| "'#{b}'" }.join(',')})
  SQL
  missing_branch_names = db.execute(sql).flatten

  del_sql = "DELETE FROM recent_branches WHERE name IN (#{missing_branch_names.map { |b| "'#{b}'" }.join(',')})"

  return if missing_branch_names.empty?

  db.execute(del_sql)
  logger.info "Removed missing local branches: #{missing_branch_names.join(', ')} branches from database"
end

#current_branchObject



94
95
96
# File 'lib/git/fancy/branch_history.rb', line 94

def current_branch
  `git branch --show-current`.chomp
end

#dbObject



18
# File 'lib/git/fancy/branch_history.rb', line 18

def db = Db.instance.db

#inspect_historyObject



90
91
92
# File 'lib/git/fancy/branch_history.rb', line 90

def inspect_history
  db.execute('SELECT * FROM recent_branches ORDER BY last_accessed DESC')
end

#local_branch_namesObject



20
21
22
# File 'lib/git/fancy/branch_history.rb', line 20

def local_branch_names
  `git branch --sort='-*authordate' --format='%(refname:short)'`.split("\n")
end

#local_branchesObject



24
25
26
# File 'lib/git/fancy/branch_history.rb', line 24

def local_branches
  to_branches(local_branch_names)
end

#purge_history!Object



86
87
88
# File 'lib/git/fancy/branch_history.rb', line 86

def purge_history!
  db.execute('DROP TABLE recent_branches')
end

#recent_branches(limit: 10, include_current: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/git/fancy/branch_history.rb', line 28

def recent_branches(limit: 10, include_current: false)
  if include_current
    sql = <<~SQL
      SELECT name, last_accessed, note FROM recent_branches ORDER BY last_accessed DESC LIMIT ?
    SQL
    return to_branches db.execute(sql, [limit])
  end

  sql = <<~SQL
    SELECT name, last_accessed, note FROM recent_branches WHERE name != ? ORDER BY last_accessed DESC LIMIT ?
  SQL
  to_branches db.execute(sql, [current_branch, limit])
end