railbow 0.6.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,19 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "date"
4
+ require "time"
5
+ require_relative "../clock"
4
6
  require_relative "../git_utils"
5
7
  require_relative "../multi_db"
6
8
  require_relative "../params"
7
9
 
8
10
  module Railbow
9
11
  module Status
10
- # Every git lookup db:migrate:status makes about a migrations directory:
11
- # who wrote each migration, when it landed on the mainline, which branch
12
- # introduced it, and what is still uncommitted.
13
- #
14
- # One instance per migrations directory. Multi-database runs reuse the
15
- # instance across databases that share a directory, which is what keeps
16
- # `git log` from running once per database.
12
+ # Every git lookup db:migrate:status makes about one migrations directory:
13
+ # authors, landing dates, originating branches and uncommitted files.
17
14
  class GitData
18
15
  EMPTY_AUTHORS = {names: {}, emails: {}}.freeze
19
16
 
@@ -37,10 +34,8 @@ module Railbow
37
34
  load_diff(branch_mask, base_override) if diff_enabled && migrate_dir
38
35
  end
39
36
 
40
- # Shares one instance per migrations directory across a multi-database
41
- # run. Databases that share a directory (shards) or a run that renders
42
- # several databases would otherwise repeat the same full-history
43
- # `git log` once per database.
37
+ # One instance per migrations directory across a multi-database run, so
38
+ # the full-history `git log` does not repeat once per database.
44
39
  def self.for(migrate_dir:, **opts)
45
40
  cache = Railbow::MultiDb.current&.git_cache
46
41
  return new(migrate_dir: migrate_dir, **opts) unless cache
@@ -48,6 +43,120 @@ module Railbow
48
43
  cache[[migrate_dir, opts]] ||= new(migrate_dir: migrate_dir, **opts)
49
44
  end
50
45
 
46
+ # Basenames of the migrations the checked-out branch adds over its base:
47
+ # committed since the merge-base, or added and not committed yet. Much
48
+ # cheaper than a full instance (no history walk, no origin lookup), so a
49
+ # database that is otherwise only summarized can afford to ask.
50
+ def self.branch_files(paths, base_override: "")
51
+ run_cached([:branch_files, paths, base_override]) do
52
+ files = Set.new(committed_branch_paths(paths, base_override).map { |f| File.basename(f) })
53
+ files.merge(uncommitted_files(paths, additions_only: true))
54
+ end
55
+ end
56
+
57
+ # Paths of the migrations committed on this branch since it left its
58
+ # base. Shared with the origin badge, so the badge and the branch rows
59
+ # agree on which migrations are the branch's.
60
+ def self.committed_branch_paths(paths, base_override)
61
+ merge_base = run_cached([:merge_base, base_override]) do
62
+ out, status = Railbow::GitUtils.capture2("merge-base", "HEAD", default_branch(base_override))
63
+ status.success? ? out.strip : nil
64
+ end
65
+ return [] unless merge_base
66
+
67
+ diff_out, diff_status = Railbow::GitUtils.capture2(
68
+ "diff", "--name-status", "--diff-filter=AR", merge_base, "HEAD", "--", *paths
69
+ )
70
+ return [] unless diff_status.success?
71
+
72
+ diff_out.each_line.filter_map { |l| name_status_path(l.strip) }
73
+ end
74
+
75
+ # Memoized for the rest of a multi-database run, so per-database calls
76
+ # share what does not depend on the database. Outside a run, no cache.
77
+ def self.run_cached(key)
78
+ cache = Railbow::MultiDb.current&.git_cache
79
+ return yield unless cache
80
+ return cache[key] if cache.key?(key)
81
+
82
+ cache[key] = yield
83
+ end
84
+
85
+ def self.default_branch(override)
86
+ return override if override && !override.empty?
87
+
88
+ run_cached([:default_branch]) { detect_default_branch }
89
+ end
90
+
91
+ def self.detect_default_branch
92
+ output, status = Railbow::GitUtils.capture2("symbolic-ref", "refs/remotes/origin/HEAD")
93
+ if status.success?
94
+ branch = output.strip.sub(%r{^refs/remotes/origin/}, "")
95
+ return branch unless branch.empty?
96
+ end
97
+
98
+ %w[main master].each do |candidate|
99
+ _, st = Railbow::GitUtils.capture2("rev-parse", "--verify", "refs/heads/#{candidate}")
100
+ return candidate if st.success?
101
+ end
102
+
103
+ "main"
104
+ end
105
+
106
+ # --name-status lines are "A\tfilepath" or "Rnnn\told\tnew"; a rename
107
+ # attributes to its destination.
108
+ def self.name_status_path(line)
109
+ cols = line.split("\t")
110
+ filepath = cols[0]&.start_with?("R") ? cols[2] : cols[1]
111
+ (filepath.nil? || filepath.empty?) ? nil : filepath
112
+ end
113
+
114
+ def self.name_status_basename(line)
115
+ filepath = name_status_path(line)
116
+ filepath && File.basename(filepath)
117
+ end
118
+
119
+ # A staged edit ("M ") belongs to the current branch for the badge, but
120
+ # with additions_only it is not a migration the branch adds: an edit to
121
+ # an old migration would otherwise count until it is committed.
122
+ def self.uncommitted_files(paths, additions_only: false)
123
+ output, status = Railbow::GitUtils.capture2("status", "--porcelain", "--", *paths)
124
+ return Set.new unless status.success?
125
+
126
+ result = Set.new
127
+ output.each_line do |line|
128
+ code = line[0..1]
129
+
130
+ if code[0] == "R"
131
+ # Rename: "R old -> new" or "R100 old -> new". Take the destination.
132
+ parts = line[3..].split(" -> ", 2)
133
+ filepath = (parts[1] || parts[0]).strip
134
+ elsif ["??", "A ", "AM"].include?(code) || (code == "M " && !additions_only)
135
+ filepath = line[3..].strip
136
+ else
137
+ next
138
+ end
139
+
140
+ result << File.basename(filepath) unless filepath.empty?
141
+ end
142
+
143
+ # During an in-progress merge, files from MERGE_HEAD (e.g. main) show up
144
+ # as staged additions. Exclude them so they are not tagged as ours.
145
+ result - incoming_merge_files(paths)
146
+ end
147
+
148
+ def self.incoming_merge_files(paths)
149
+ merging = run_cached([:merging]) { Railbow::GitUtils.capture3("rev-parse", "MERGE_HEAD")[2].success? }
150
+ return Set.new unless merging
151
+
152
+ output, _, status = Railbow::GitUtils.capture3(
153
+ "diff", "--name-only", "--diff-filter=AR", "HEAD", "MERGE_HEAD", "--", *paths
154
+ )
155
+ return Set.new unless status.success?
156
+
157
+ Set.new(output.each_line.map { |l| File.basename(l.strip) }.reject(&:empty?))
158
+ end
159
+
51
160
  # Extracts a display label from a branch name. "auto" uses the built-in
52
161
  # ticket pattern; anything else is a regex whose first capture wins.
53
162
  def self.apply_branch_mask(branch, branch_mask)
@@ -137,8 +246,8 @@ module Railbow
137
246
  line = line.strip
138
247
  if line.start_with?("COMMIT:")
139
248
  current_date = begin
140
- Date.parse(line.sub("COMMIT:", ""))
141
- rescue Date::Error
249
+ Railbow::Clock.in_zone(Time.iso8601(line.sub("COMMIT:", ""))).to_date
250
+ rescue ArgumentError
142
251
  nil
143
252
  end
144
253
  elsif !line.empty? && current_date
@@ -150,12 +259,8 @@ module Railbow
150
259
  dates
151
260
  end
152
261
 
153
- # --name-status lines are "A\tfilepath" or "Rnnn\told\tnew"; a rename
154
- # attributes to its destination.
155
262
  def name_status_basename(line)
156
- cols = line.split("\t")
157
- filepath = cols[0]&.start_with?("R") ? cols[2] : cols[1]
158
- filepath ? File.basename(filepath) : nil
263
+ self.class.name_status_basename(line)
159
264
  end
160
265
 
161
266
  def current_git_email
@@ -176,36 +281,11 @@ module Railbow
176
281
  end
177
282
 
178
283
  def detect_default_branch(override)
179
- return override if override && !override.empty?
180
-
181
- output, status = Railbow::GitUtils.capture2("symbolic-ref", "refs/remotes/origin/HEAD")
182
- if status.success?
183
- branch = output.strip.sub(%r{^refs/remotes/origin/}, "")
184
- return branch unless branch.empty?
185
- end
186
-
187
- %w[main master].each do |candidate|
188
- _, st = Railbow::GitUtils.capture2("rev-parse", "--verify", "refs/heads/#{candidate}")
189
- return candidate if st.success?
190
- end
191
-
192
- "main"
284
+ self.class.default_branch(override)
193
285
  end
194
286
 
195
287
  def branch_migration_origins(base_branch, branch_mask)
196
- merge_base_out, mb_status = Railbow::GitUtils.capture2("merge-base", "HEAD", base_branch)
197
- return {} unless mb_status.success?
198
-
199
- merge_base = merge_base_out.strip
200
- diff_out, diff_status = Railbow::GitUtils.capture2(
201
- "diff", "--name-status", "--diff-filter=AR", merge_base, "HEAD", "--", migrate_dir
202
- )
203
- return {} unless diff_status.success?
204
-
205
- files = diff_out.each_line.map { |l|
206
- cols = l.strip.split("\t")
207
- cols[0]&.start_with?("R") ? cols[2] : cols[1]
208
- }.compact.reject(&:empty?)
288
+ files = self.class.committed_branch_paths([migrate_dir], base_branch)
209
289
  origins = {}
210
290
 
211
291
  files.each do |filepath|
@@ -223,14 +303,27 @@ module Railbow
223
303
  origins
224
304
  end
225
305
 
306
+ # The commit that first produced this filename. A plain log shows no diff
307
+ # for a merge commit, so a file named inside one (a migration
308
+ # re-timestamped while merging the base branch in) needs the -m form,
309
+ # oldest match first. -m stays a fallback: a later "merge main into
310
+ # feature" commit lists every main migration as added against one parent,
311
+ # and pick_origin_branch would then label those with the base branch.
226
312
  def adding_commit(filepath)
227
- commit_out, cs = Railbow::GitUtils.capture2(
228
- "log", "--diff-filter=AR", "--format=%H", "-1", "--", filepath
313
+ commits = commits_touching(filepath, "-1")
314
+ return nil if commits.nil?
315
+ return commits.first if commits.any?
316
+
317
+ commits_touching(filepath, "-m")&.last
318
+ end
319
+
320
+ def commits_touching(filepath, *flags)
321
+ out, status = Railbow::GitUtils.capture2(
322
+ "log", "--diff-filter=AR", "--format=%H", *flags, "--", filepath
229
323
  )
230
- return nil unless cs.success?
324
+ return nil unless status.success?
231
325
 
232
- commit = commit_out.strip
233
- commit.empty? ? nil : commit
326
+ out.each_line.map(&:strip).reject(&:empty?)
234
327
  end
235
328
 
236
329
  def branches_containing(commit)
@@ -242,11 +335,9 @@ module Railbow
242
335
  branches_out.each_line.map(&:strip).reject(&:empty?)
243
336
  end
244
337
 
245
- # Picks the branch that originally introduced the commit.
246
- # 1. Drop child branches: if A is an ancestor of B, the commit came from A.
247
- # 2. Among the rest, prefer the branch with the most commits after the
248
- # adding commit - it has been active longest since, so it is the
249
- # original rather than a newer fork.
338
+ # Picks the branch that introduced the commit: child branches drop out
339
+ # (if A is an ancestor of B, the commit came from A), then the branch
340
+ # with the most commits since wins, as the original rather than a fork.
250
341
  def pick_origin_branch(branches, commit)
251
342
  return branches.first if branches.size == 1
252
343
 
@@ -267,29 +358,7 @@ module Railbow
267
358
  end
268
359
 
269
360
  def uncommitted_migration_files
270
- output, status = Railbow::GitUtils.capture2("status", "--porcelain", "--", migrate_dir)
271
- return Set.new unless status.success?
272
-
273
- result = Set.new
274
- output.each_line do |line|
275
- code = line[0..1]
276
-
277
- if code[0] == "R"
278
- # Rename: "R old -> new" or "R100 old -> new". Take the destination.
279
- parts = line[3..].split(" -> ", 2)
280
- filepath = (parts[1] || parts[0]).strip
281
- elsif ["??", "A ", "AM", "M "].include?(code)
282
- filepath = line[3..].strip
283
- else
284
- next
285
- end
286
-
287
- result << File.basename(filepath) unless filepath.empty?
288
- end
289
-
290
- # During an in-progress merge, files from MERGE_HEAD (e.g. main) show up
291
- # as staged additions. Exclude them so they are not tagged as ours.
292
- result - incoming_merge_files_from_git
361
+ self.class.uncommitted_files([migrate_dir])
293
362
  end
294
363
 
295
364
  def detect_merge_source_label(branch_mask)
@@ -312,15 +381,7 @@ module Railbow
312
381
  end
313
382
 
314
383
  def incoming_merge_files_from_git
315
- _, _, mh_status = Railbow::GitUtils.capture3("rev-parse", "MERGE_HEAD")
316
- return Set.new unless mh_status.success?
317
-
318
- output, _, status = Railbow::GitUtils.capture3(
319
- "diff", "--name-only", "--diff-filter=AR", "HEAD", "MERGE_HEAD", "--", migrate_dir
320
- )
321
- return Set.new unless status.success?
322
-
323
- Set.new(output.each_line.map { |l| File.basename(l.strip) }.reject(&:empty?))
384
+ self.class.incoming_merge_files([migrate_dir])
324
385
  end
325
386
  end
326
387
  end
@@ -33,6 +33,12 @@ module Railbow
33
33
  short - Jan 30 (column: Date)
34
34
  custom(…) - user strftime, e.g. custom(%b %d, %Y)
35
35
 
36
+ RBW_TZ=<zone> Timezone dates are shown in (default: local):
37
+ local - this machine's timezone
38
+ utc - UTC, the digits of the Migration ID
39
+ <name> - any zone Rails knows, e.g.
40
+ America/New_York
41
+
36
42
  RBW_VIEW=<options> Display options (comma-separated):
37
43
  calendar - show month/year separator lines + week ticks
38
44
  tables - parse migration files, show Tables column
@@ -63,7 +69,10 @@ module Railbow
63
69
  with column widths shared so the tables
64
70
  line up. Databases sharing a migrations
65
71
  path (shards) merge into one table with a
66
- status per database.
72
+ status per database. Migrations your branch
73
+ adds always show, even outside the window,
74
+ and a summarized database lists just those
75
+ (needs RBW_GIT=diff).
67
76
  focus - expand only the first
68
77
  database, summarizing the
69
78
  rest in a line each. A
@@ -9,12 +9,9 @@ require_relative "../table"
9
9
 
10
10
  module Railbow
11
11
  module Status
12
- # Turns collected sections into output. The only place db:migrate:status
13
- # writes to stdout, which is what lets a multi-database run share column
14
- # widths and print a single header.
15
- #
16
- # A run holding one database renders exactly as it did before multi-database
17
- # support existed: no overview line, no section rule, nothing new.
12
+ # The only place db:migrate:status writes to stdout, which is what lets a
13
+ # multi-database run share column widths and print a single header. A
14
+ # single-database run gets no overview line and no section rule.
18
15
  class Printer
19
16
  RULE = "─"
20
17
  COLLAPSED = "⋯"
@@ -32,6 +29,7 @@ module Railbow
32
29
  def print
33
30
  return print_solo(sections.first) if solo?
34
31
 
32
+ apply_branch_rows
35
33
  print_overview
36
34
  Railbow::Params.db_inline? ? print_inline : print_sections
37
35
  print_skipped
@@ -88,9 +86,8 @@ module Railbow
88
86
  puts render_table(section)
89
87
  end
90
88
 
91
- # Says what the window hid, and admits when the floor overrode it: the
92
- # table then holds rows older than the window it names, and a bare
93
- # "hidden - SINCE=70d" would read as a contradiction next to them.
89
+ # Admits when the floor overrode the window: a bare "hidden - SINCE=90d"
90
+ # would contradict the older rows shown below it.
94
91
  def hidden_note(section)
95
92
  return nil unless section.hidden_count > 0
96
93
 
@@ -110,8 +107,7 @@ module Railbow
110
107
  puts
111
108
  puts section_header(section, renderer.fixed_width(widths))
112
109
 
113
- # Reachable with RBW_DB=full, which asks for every section to be drawn
114
- # in full, including the ones that would otherwise have collapsed.
110
+ # Reachable with RBW_DB=full, which draws even quiet sections in full.
115
111
  if section.state == :no_migrations
116
112
  puts formatter.yellow(" No migrations found")
117
113
  return
@@ -123,10 +119,9 @@ module Railbow
123
119
  puts render_table(section, renderer)
124
120
  end
125
121
 
126
- # RBW_DB=inline: every database in one time-ordered table, each row
127
- # carrying a Db badge. The calendar then spans the whole application
128
- # rather than restarting per database, which is the point of asking for
129
- # it. Quiet databases still collapse below the table.
122
+ # RBW_DB=inline: every database in one time-ordered table with a Db
123
+ # badge per row, so the calendar spans the whole application. Quiet
124
+ # databases still collapse below the table.
130
125
  def print_inline
131
126
  live = sections.reject { |s| collapse?(s) || s.rows.empty? }
132
127
  collapsed = sections - live
@@ -149,9 +144,8 @@ module Railbow
149
144
  collapsed.each { |section| puts collapsed_line(section) }
150
145
  end
151
146
 
152
- # Sorted by version, so the databases interleave by when each migration
153
- # was written. Ties fall back to config order, which keeps two databases
154
- # that share a timestamp in database.yml order rather than an arbitrary one.
147
+ # Sorted by version; ties fall back to database.yml order rather than an
148
+ # arbitrary one.
155
149
  def inline_entries(live)
156
150
  assigner = Railbow::ColorAssigner.new(live.map { |s| inline_label(s) })
157
151
 
@@ -209,27 +203,32 @@ module Railbow
209
203
  puts formatter.dim("#{COLLAPSED} #{count_label(skipped.size, "database")} hidden: #{skipped.join(", ")}")
210
204
  end
211
205
 
212
- # A quiet database says everything it has to say in one line. Nothing in
213
- # the window means there is no table to draw either, so collapsing loses
214
- # nothing - the counts below carry what the rows would have shown.
215
- #
216
- # RBW_DB=focus collapses the busy ones too, keeping the first database as
217
- # the one you actually read. Pending migrations veto that: a database
218
- # with work waiting is drawn in full however focused the run is.
206
+ # Your own migrations show up whichever database they target and however
207
+ # old they are: a drawn table gains the ones the time window left out, and
208
+ # a database that would collapse keeps a table of just those.
209
+ def apply_branch_rows
210
+ sections.each do |section|
211
+ next if section.branch_versions.empty?
212
+
213
+ collapse?(section) ? section.narrow_to_branch! : section.include_branch!
214
+ end
215
+ end
216
+
217
+ # A quiet database has no table to draw, so one line says it all.
218
+ # RBW_DB=focus collapses all but the first database too, unless one has
219
+ # migrations pending.
219
220
  def collapse?(section)
220
- return false if Railbow::Params.db_full?
221
+ return false if Railbow::Params.db_full? || section.branch_only?
221
222
  return true if section.quiet?
222
- # Focus shapes the sectioned view. Applying it to a merged table would
223
- # drop every database but the first out of the very thing that exists
224
- # to hold all of them.
223
+ # Focus would empty the inline table of every database but the first.
225
224
  return false if Railbow::Params.db_inline?
226
225
  return false unless Railbow::Params.db_focus?
227
226
 
228
227
  !focused?(section) && !section.pending_in_view?
229
228
  end
230
229
 
231
- # The first database in database.yml, which is conventionally primary and
232
- # is the one Rails migrates first.
230
+ # The first database in database.yml: conventionally primary, and the one
231
+ # Rails migrates first.
233
232
  def focused?(section)
234
233
  section.equal?(sections.first)
235
234
  end
@@ -254,13 +253,29 @@ module Railbow
254
253
  end
255
254
  end
256
255
 
256
+ # What a narrowed section's summary line would have warned about: pending
257
+ # or ghost migrations among the rows it does not draw.
258
+ def hidden_outside(section)
259
+ parts = []
260
+ parts << "#{section.hidden_pending_count} pending" if section.hidden_pending_count > 0
261
+ parts << count_label(section.hidden_ghost_count, "ghost") if section.hidden_ghost_count > 0
262
+ return nil if parts.empty?
263
+
264
+ "#{parts.join(", ")} outside the #{section.since_value} window - RBW_SINCE=all"
265
+ end
266
+
257
267
  # "──── primary · pawshop-dev ──── 12 of 240 ────", ruled out to where
258
268
  # the last column starts so it frames the table rather than floating.
259
269
  def section_header(section, rule_width)
260
270
  label = section.db_names.join(" + ")
261
271
  detail = section.sharded? ? shared_path(section) : section.database
262
272
  head = "#{RULE * 4} #{label} · #{detail} "
263
- head += "#{RULE * 4} #{shown_count(section)} of #{section.total_count} " if section.hidden_count > 0
273
+ if section.branch_only?
274
+ head += "#{RULE * 4} #{section.rows.size} on this branch of #{section.total_count} "
275
+ head += "#{RULE * 4} #{hidden_outside(section)} " if hidden_outside(section)
276
+ elsif section.hidden_count > 0
277
+ head += "#{RULE * 4} #{shown_count(section)} of #{section.total_count} "
278
+ end
264
279
 
265
280
  width = (rule_width > 0) ? rule_width : DEFAULT_RULE_WIDTH
266
281
  if (term = terminal_width)
@@ -279,9 +294,8 @@ module Railbow
279
294
  nil
280
295
  end
281
296
 
282
- # A bare "10 of 50" reads as "10 fell inside the window". When the floor
283
- # overrode the window the selection is a tail rather than a window, so it
284
- # says "last 10 of 50" - the same words the single-database note uses.
297
+ # "last 10 of 50" when the floor overrode the window, since a bare
298
+ # "10 of 50" reads as "10 fell inside the window".
285
299
  def shown_count(section)
286
300
  shown = section.total_count - section.hidden_count
287
301
  section.floor_applied? ? "last #{shown}" : shown.to_s