Class: Railbow::Status::GitData

Inherits:
Object
  • Object
show all
Defined in:
lib/railbow/status/git_data.rb

Overview

Every git lookup db:migrate:status makes about a migrations directory: who wrote each migration, when it landed on the mainline, which branch introduced it, and what is still uncommitted.

One instance per migrations directory. Multi-database runs reuse the instance across databases that share a directory, which is what keeps git log from running once per database.

Constant Summary collapse

EMPTY_AUTHORS =
{names: {}, emails: {}}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migrate_dir:, author_enabled:, diff_enabled:, base_override: "", branch_mask: "") ⇒ GitData

migrate_dir is nil when no migration file could be located, in which case every file-scoped lookup is skipped and the accessors stay empty.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/railbow/status/git_data.rb', line 26

def initialize(migrate_dir:, author_enabled:, diff_enabled:, base_override: "", branch_mask: "")
  @migrate_dir = migrate_dir
  @author_names = {}
  @author_emails = {}
  @landed_dates = {}
  @branch_origins = {}
  @uncommitted_files = Set.new
  @incoming_merge_files = Set.new
  @merge_source_label = nil

  load_authors(author_enabled)
  load_diff(branch_mask, base_override) if diff_enabled && migrate_dir
end

Instance Attribute Details

#author_emailsObject (readonly)

Returns the value of attribute author_emails.



20
21
22
# File 'lib/railbow/status/git_data.rb', line 20

def author_emails
  @author_emails
end

#author_namesObject (readonly)

Returns the value of attribute author_names.



20
21
22
# File 'lib/railbow/status/git_data.rb', line 20

def author_names
  @author_names
end

#branch_originsObject (readonly)

Returns the value of attribute branch_origins.



20
21
22
# File 'lib/railbow/status/git_data.rb', line 20

def branch_origins
  @branch_origins
end

#git_emailObject (readonly)

Returns the value of attribute git_email.



20
21
22
# File 'lib/railbow/status/git_data.rb', line 20

def git_email
  @git_email
end

#git_nameObject (readonly)

Returns the value of attribute git_name.



20
21
22
# File 'lib/railbow/status/git_data.rb', line 20

def git_name
  @git_name
end

#incoming_merge_filesObject (readonly)

Returns the value of attribute incoming_merge_files.



20
21
22
# File 'lib/railbow/status/git_data.rb', line 20

def incoming_merge_files
  @incoming_merge_files
end

#landed_datesObject (readonly)

Returns the value of attribute landed_dates.



20
21
22
# File 'lib/railbow/status/git_data.rb', line 20

def landed_dates
  @landed_dates
end

#merge_source_labelObject (readonly)

Returns the value of attribute merge_source_label.



20
21
22
# File 'lib/railbow/status/git_data.rb', line 20

def merge_source_label
  @merge_source_label
end

#migrate_dirObject (readonly)

Returns the value of attribute migrate_dir.



20
21
22
# File 'lib/railbow/status/git_data.rb', line 20

def migrate_dir
  @migrate_dir
end

#uncommitted_filesObject (readonly)

Returns the value of attribute uncommitted_files.



20
21
22
# File 'lib/railbow/status/git_data.rb', line 20

def uncommitted_files
  @uncommitted_files
end

Class Method Details

.apply_branch_mask(branch, branch_mask) ⇒ Object

Extracts a display label from a branch name. "auto" uses the built-in ticket pattern; anything else is a regex whose first capture wins.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/railbow/status/git_data.rb', line 53

def self.apply_branch_mask(branch, branch_mask)
  return branch if branch_mask.empty?
  return Railbow::Params.extract_branch_ticket(branch) if branch_mask == "auto"

  re = begin
    Regexp.new(branch_mask, Regexp::IGNORECASE)
  rescue RegexpError
    return branch
  end
  m = branch.match(re)
  (m && m[1]) ? m[1] : branch
end

.for(migrate_dir:, **opts) ⇒ Object

Shares one instance per migrations directory across a multi-database run. Databases that share a directory (shards) or a run that renders several databases would otherwise repeat the same full-history git log once per database.



44
45
46
47
48
49
# File 'lib/railbow/status/git_data.rb', line 44

def self.for(migrate_dir:, **opts)
  cache = Railbow::MultiDb.current&.git_cache
  return new(migrate_dir: migrate_dir, **opts) unless cache

  cache[[migrate_dir, opts]] ||= new(migrate_dir: migrate_dir, **opts)
end