smorg-rb 7.1.22 → 8.0.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.
data/lib/smorg/rb.rb CHANGED
@@ -1,26 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'English'
4
- require 'bash/merge'
5
- require 'go-merge'
6
- require 'html/merge'
7
- require 'ast/merge'
8
- require 'ast-merge-git'
9
- require 'diff/lcs'
10
- require 'diff/lcs/hunk'
11
- require 'dotenv/merge'
12
- require 'json'
13
- require 'json-merge'
14
- require 'kettle/jem'
15
- require 'kettle/jem/tasks/install_task'
16
- require 'markdown/merge'
17
- require 'plain-merge'
18
- require 'rbs/merge'
19
- require 'toml/merge'
20
- require 'typescript/merge'
21
- require 'prism/merge'
22
- require 'psych/merge'
23
- require_relative 'rb/version'
3
+ require "English"
4
+ require "bash/merge"
5
+ require "go-merge"
6
+ require "html/merge"
7
+ require "ast/merge"
8
+ require "ast-merge-git"
9
+ require "diff/lcs"
10
+ require "diff/lcs/hunk"
11
+ require "dotenv/merge"
12
+ require "json"
13
+ require "json-merge"
14
+ require "kettle/jem"
15
+ require "kettle/jem/tasks/install_task"
16
+ require "markdown/merge"
17
+ require "plain-merge"
18
+ require "rbs/merge"
19
+ require "toml/merge"
20
+ require "typescript/merge"
21
+ require "prism/merge"
22
+ require "psych/merge"
23
+ require_relative "rb/version"
24
24
 
25
25
  module Smorg
26
26
  module RB # rubocop:disable Metrics/ModuleLength
@@ -35,17 +35,17 @@ module Smorg
35
35
  def run(args, stdout: $stdout, stderr: $stderr)
36
36
  command, *rest = args
37
37
  case command
38
- when 'merge-driver'
38
+ when "merge-driver"
39
39
  run_merge_driver(rest, stdout, stderr)
40
- when 'diff-driver'
40
+ when "diff-driver"
41
41
  run_diff_driver(rest, stdout, stderr)
42
- when 'conflicts'
42
+ when "conflicts"
43
43
  run_conflicts(rest, stdout, stderr)
44
- when 'languages'
44
+ when "languages"
45
45
  run_languages(rest, stdout, stderr)
46
- when 'git'
46
+ when "git"
47
47
  run_git(rest, stdout, stderr)
48
- when 'help', '-h', '--help'
48
+ when "help", "-h", "--help"
49
49
  print_usage(stdout)
50
50
  EXIT_SUCCESS
51
51
  else
@@ -56,18 +56,18 @@ module Smorg
56
56
  end
57
57
 
58
58
  def print_usage(out)
59
- out.puts('usage: smorg-rb merge-driver [--path-name PATH] [--output PATH] [--report PATH] [--strict] [--fallback=none|line|local|full-file] %O %A %B [%P]')
60
- out.puts(' smorg-rb merge-driver --ancestor %O --current %A --other %B --path-name %P')
61
- out.puts(' smorg-rb diff-driver [--path-name PATH] OLD NEW')
62
- out.puts(' smorg-rb diff-driver PATH OLD-FILE OLD-HEX OLD-MODE NEW-FILE NEW-HEX NEW-MODE [OLD-PREFIX NEW-PREFIX]')
63
- out.puts(' smorg-rb conflicts diff [--path-name PATH] [--exit-code] FILE')
64
- out.puts(' smorg-rb languages --gitattributes')
65
- out.puts(' smorg-rb git install [--scope local|global|include-file] [--profile semantic-diff|builtin-diff] [--check] [--undo] [--dry-run] [--json]')
59
+ out.puts("usage: smorg-rb merge-driver [--path-name PATH] [--output PATH] [--report PATH] [--strict] [--fallback=none|line|local|full-file] %O %A %B [%P]")
60
+ out.puts(" smorg-rb merge-driver --ancestor %O --current %A --other %B --path-name %P")
61
+ out.puts(" smorg-rb diff-driver [--path-name PATH] OLD NEW")
62
+ out.puts(" smorg-rb diff-driver PATH OLD-FILE OLD-HEX OLD-MODE NEW-FILE NEW-HEX NEW-MODE [OLD-PREFIX NEW-PREFIX]")
63
+ out.puts(" smorg-rb conflicts diff [--path-name PATH] [--exit-code] FILE")
64
+ out.puts(" smorg-rb languages --gitattributes")
65
+ out.puts(" smorg-rb git install [--scope local|global|include-file] [--profile semantic-diff|builtin-diff] [--check] [--undo] [--dry-run] [--json]")
66
66
  end
67
67
 
68
68
  def run_git(args, stdout, stderr)
69
69
  subcommand, *rest = args
70
- return git_usage(stderr) unless subcommand == 'install'
70
+ return git_usage(stderr) unless subcommand == "install"
71
71
 
72
72
  options = parse_git_install_options(rest, stderr)
73
73
  return EXIT_USER_ERROR unless options
@@ -83,9 +83,9 @@ module Smorg
83
83
  ).first
84
84
  report = {
85
85
  report_version: 1,
86
- ok: step.fetch(:status) != 'failed',
87
- profile: step.fetch(:profile, 'semantic-diff'),
88
- scope: step.fetch(:scope, run_options.fetch(:git_drivers, 'local')),
86
+ ok: step.fetch(:status) != "failed",
87
+ profile: step.fetch(:profile, "semantic-diff"),
88
+ scope: step.fetch(:scope, run_options.fetch(:git_drivers, "local")),
89
89
  install_steps: [step],
90
90
  missing: step.fetch(:missing, [])
91
91
  }
@@ -104,26 +104,26 @@ module Smorg
104
104
  end
105
105
 
106
106
  def git_usage(stderr)
107
- stderr.puts('usage: smorg-rb git install [--scope local|global|include-file] [--profile semantic-diff|builtin-diff] [--check] [--undo] [--dry-run] [--json]')
107
+ stderr.puts("usage: smorg-rb git install [--scope local|global|include-file] [--profile semantic-diff|builtin-diff] [--check] [--undo] [--dry-run] [--json]")
108
108
  EXIT_USER_ERROR
109
109
  end
110
110
 
111
111
  def parse_git_install_options(args, stderr)
112
- options = { scope: 'local', profile: 'semantic-diff', json: false, dry_run: false }
112
+ options = {scope: "local", profile: "semantic-diff", json: false, dry_run: false}
113
113
  until args.empty?
114
114
  value = args.shift
115
115
  case value
116
- when '--scope'
116
+ when "--scope"
117
117
  options[:scope] = args.shift.to_s
118
- when '--profile'
118
+ when "--profile"
119
119
  options[:profile] = args.shift.to_s
120
- when '--check'
120
+ when "--check"
121
121
  options[:check] = true
122
- when '--undo'
122
+ when "--undo"
123
123
  options[:undo] = true
124
- when '--dry-run'
124
+ when "--dry-run"
125
125
  options[:dry_run] = true
126
- when '--json'
126
+ when "--json"
127
127
  options[:json] = true
128
128
  else
129
129
  stderr.puts("unknown git install option #{value.inspect}")
@@ -135,19 +135,19 @@ module Smorg
135
135
 
136
136
  def git_install_run_options(options)
137
137
  git_drivers = if options[:check]
138
- 'check'
139
- elsif options[:undo]
140
- 'undo'
141
- elsif options.fetch(:scope) == 'global'
142
- 'global'
143
- elsif options.fetch(:scope) == 'include-file'
144
- 'include-file'
145
- elsif options.fetch(:profile) == 'builtin-diff'
146
- 'builtin-diff'
147
- else
148
- 'semantic-diff'
149
- end
150
- { git_drivers: git_drivers, dry_run: options[:dry_run] }.compact
138
+ "check"
139
+ elsif options[:undo]
140
+ "undo"
141
+ elsif options.fetch(:scope) == "global"
142
+ "global"
143
+ elsif options.fetch(:scope) == "include-file"
144
+ "include-file"
145
+ elsif options.fetch(:profile) == "builtin-diff"
146
+ "builtin-diff"
147
+ else
148
+ "semantic-diff"
149
+ end
150
+ {git_drivers: git_drivers, dry_run: options[:dry_run]}.compact
151
151
  end
152
152
 
153
153
  def run_merge_driver(args, stdout, stderr)
@@ -165,9 +165,9 @@ module Smorg
165
165
  profile_exit = report_and_enforce_profile(options, stdout, stderr)
166
166
  return profile_exit unless profile_exit == EXIT_SUCCESS
167
167
 
168
- fallback_policy = options[:strict] ? 'none' : options[:fallback]
168
+ fallback_policy = options[:strict] ? "none" : options[:fallback]
169
169
  result = merge_by_path(effective_path, settings[:language], settings[:conflict_marker_size], fallback_policy,
170
- ancestor_source, current_source, other_source)
170
+ ancestor_source, current_source, other_source)
171
171
  fallbacks = []
172
172
  if merge_driver_fallback_eligible?(result, options)
173
173
  result, fallbacks = apply_merge_fallbacks(
@@ -182,20 +182,20 @@ module Smorg
182
182
  output = result[:output]
183
183
  unless result[:ok]
184
184
  print_diagnostics(stderr, result)
185
- unless options[:strict] || options[:fallback] == 'none'
185
+ unless options[:strict] || options[:fallback] == "none"
186
186
  output ||= full_file_conflict_output(settings[:conflict_marker_size], ancestor_source, current_source,
187
- other_source)
187
+ other_source)
188
188
  end
189
- if output && !result[:output] && !options[:strict] && options[:fallback] != 'none'
189
+ if output && !result[:output] && !options[:strict] && options[:fallback] != "none"
190
190
  fallbacks << {
191
- mode: 'full_file',
191
+ mode: "full_file",
192
192
  requested_mode: options[:fallback],
193
193
  reason: fallback_reason(result.fetch(:diagnostics, [])),
194
194
  applied: true
195
195
  }
196
196
  end
197
197
  report_exit = write_merge_driver_machine_report(options[:report], effective_path, false,
198
- EXIT_UNRESOLVED_CONFLICT, fallbacks, result, stderr)
198
+ EXIT_UNRESOLVED_CONFLICT, fallbacks, result, stderr)
199
199
  return report_exit unless report_exit == EXIT_SUCCESS
200
200
  return EXIT_UNRESOLVED_CONFLICT if options[:check_only]
201
201
 
@@ -203,14 +203,14 @@ module Smorg
203
203
  return EXIT_UNRESOLVED_CONFLICT
204
204
  end
205
205
  unless output
206
- stderr.puts('merge completed without output')
206
+ stderr.puts("merge completed without output")
207
207
  return EXIT_INTERNAL_ERROR
208
208
  end
209
209
 
210
210
  if options[:check_only]
211
- exit_code = options[:exit_code] && output != current_source ? EXIT_UNRESOLVED_CONFLICT : EXIT_SUCCESS
211
+ exit_code = (options[:exit_code] && output != current_source) ? EXIT_UNRESOLVED_CONFLICT : EXIT_SUCCESS
212
212
  report_exit = write_merge_driver_machine_report(options[:report], effective_path, true, exit_code, fallbacks,
213
- result, stderr)
213
+ result, stderr)
214
214
  return report_exit unless report_exit == EXIT_SUCCESS
215
215
 
216
216
  return exit_code
@@ -218,14 +218,14 @@ module Smorg
218
218
 
219
219
  File.write(options[:output] || options[:current], output)
220
220
  report_exit = write_merge_driver_machine_report(options[:report], effective_path, true, EXIT_SUCCESS, fallbacks,
221
- result, stderr)
221
+ result, stderr)
222
222
  return report_exit unless report_exit == EXIT_SUCCESS
223
223
 
224
224
  EXIT_SUCCESS
225
225
  rescue Errno::ENOENT, Errno::EACCES => e
226
226
  stderr.puts("file error: #{e.message}")
227
227
  EXIT_USER_ERROR
228
- rescue StandardError => e
228
+ rescue => e
229
229
  stderr.puts("internal error: #{e.message}")
230
230
  EXIT_INTERNAL_ERROR
231
231
  end
@@ -238,7 +238,7 @@ module Smorg
238
238
  merge_fallback_result(result, git_result),
239
239
  [
240
240
  {
241
- mode: 'git_merge_file',
241
+ mode: "git_merge_file",
242
242
  requested_mode: requested_mode,
243
243
  reason: fallback_reason_value,
244
244
  applied: true
@@ -253,13 +253,13 @@ module Smorg
253
253
  merge_fallback_result(result, plain_result),
254
254
  [
255
255
  {
256
- mode: 'git_merge_file',
256
+ mode: "git_merge_file",
257
257
  requested_mode: requested_mode,
258
258
  reason: fallback_reason_value,
259
259
  applied: false
260
260
  },
261
261
  {
262
- mode: 'plain_text',
262
+ mode: "plain_text",
263
263
  requested_mode: requested_mode,
264
264
  reason: fallback_reason(git_result.fetch(:diagnostics, [])),
265
265
  applied: true
@@ -272,42 +272,42 @@ module Smorg
272
272
  end
273
273
 
274
274
  def merge_driver_fallback_eligible?(result, options)
275
- return false if result[:ok] || options[:strict] || options[:fallback] == 'none'
275
+ return false if result[:ok] || options[:strict] || options[:fallback] == "none"
276
276
 
277
277
  result.fetch(:diagnostics, []).any? do |diagnostic|
278
- (diagnostic[:category] || diagnostic['category']).to_s == 'unsupported_language'
278
+ (diagnostic[:category] || diagnostic["category"]).to_s == "unsupported_language"
279
279
  end
280
280
  end
281
281
 
282
282
  def merge_plain_fallback(other_source, current_source)
283
283
  Plain::Merge.merge_text(other_source, current_source)
284
- rescue StandardError => e
284
+ rescue => e
285
285
  {
286
286
  ok: false,
287
- diagnostics: [{ severity: 'error', category: 'plain_text_fallback_error', message: e.message }],
287
+ diagnostics: [{severity: "error", category: "plain_text_fallback_error", message: e.message}],
288
288
  policies: []
289
289
  }
290
290
  end
291
291
 
292
292
  def merge_git_file_fallback(marker_size, ancestor_source, current_source, other_source)
293
- require 'tempfile'
293
+ require "tempfile"
294
294
 
295
- files = Array.new(3) { Tempfile.new('smorg-rb-merge-file') }
295
+ files = Array.new(3) { Tempfile.new("smorg-rb-merge-file") }
296
296
  files[0].write(current_source)
297
297
  files[1].write(ancestor_source)
298
298
  files[2].write(other_source)
299
299
  files.each(&:flush)
300
300
  output = IO.popen(
301
301
  [
302
- 'git',
303
- 'merge-file',
304
- '-p',
305
- '-L',
306
- 'ours',
307
- '-L',
308
- 'base',
309
- '-L',
310
- 'theirs',
302
+ "git",
303
+ "merge-file",
304
+ "-p",
305
+ "-L",
306
+ "ours",
307
+ "-L",
308
+ "base",
309
+ "-L",
310
+ "theirs",
311
311
  "--marker-size=#{marker_size}",
312
312
  files[0].path,
313
313
  files[1].path,
@@ -321,8 +321,8 @@ module Smorg
321
321
  diagnostics: if $CHILD_STATUS.success?
322
322
  []
323
323
  else
324
- [{ severity: 'error', category: 'git_merge_file_conflict',
325
- message: 'git merge-file reported unresolved conflicts' }]
324
+ [{severity: "error", category: "git_merge_file_conflict",
325
+ message: "git merge-file reported unresolved conflicts"}]
326
326
  end,
327
327
  output: output,
328
328
  policies: []
@@ -330,7 +330,7 @@ module Smorg
330
330
  rescue Errno::ENOENT => e
331
331
  {
332
332
  ok: false,
333
- diagnostics: [{ severity: 'error', category: 'git_merge_file_unavailable', message: e.message }],
333
+ diagnostics: [{severity: "error", category: "git_merge_file_unavailable", message: e.message}],
334
334
  policies: []
335
335
  }
336
336
  ensure
@@ -356,63 +356,63 @@ module Smorg
356
356
  marker_size = marker_size.to_i
357
357
  marker_size = 7 unless marker_size.positive?
358
358
  [
359
- "#{'<' * marker_size} ours",
359
+ "#{"<" * marker_size} ours",
360
360
  current_source,
361
- "#{'|' * marker_size} base",
361
+ "#{"|" * marker_size} base",
362
362
  ancestor_source,
363
- '=' * marker_size,
363
+ "=" * marker_size,
364
364
  other_source,
365
- "#{'>' * marker_size} theirs",
366
- ''
365
+ "#{">" * marker_size} theirs",
366
+ ""
367
367
  ].join("\n")
368
368
  end
369
369
 
370
370
  def parse_merge_driver_options(args, stderr)
371
- options = { strict: false, fallback: 'full-file', check_only: false, exit_code: false, profile_report: false }
371
+ options = {strict: false, fallback: "full-file", check_only: false, exit_code: false, profile_report: false}
372
372
  positionals = []
373
373
  index = 0
374
374
  while index < args.length
375
375
  value = args[index]
376
376
  case value
377
- when '--ancestor'
377
+ when "--ancestor"
378
378
  index += 1
379
379
  options[:ancestor] = args[index]
380
- when '--current'
380
+ when "--current"
381
381
  index += 1
382
382
  options[:current] = args[index]
383
- when '--other'
383
+ when "--other"
384
384
  index += 1
385
385
  options[:other] = args[index]
386
- when '--path-name'
386
+ when "--path-name"
387
387
  index += 1
388
388
  options[:path_name] = args[index]
389
- when '--output'
389
+ when "--output"
390
390
  index += 1
391
391
  options[:output] = args[index]
392
- when '--report'
392
+ when "--report"
393
393
  index += 1
394
394
  options[:report] = args[index]
395
- when '--strict'
395
+ when "--strict"
396
396
  options[:strict] = true
397
- when '--check-only'
397
+ when "--check-only"
398
398
  options[:check_only] = true
399
- when '--exit-code'
399
+ when "--exit-code"
400
400
  options[:exit_code] = true
401
- when '--profile'
401
+ when "--profile"
402
402
  index += 1
403
403
  options[:profile_id] = args[index]
404
- when '--profile-report'
404
+ when "--profile-report"
405
405
  options[:profile_report] = true
406
- when '--require-profile-status'
406
+ when "--require-profile-status"
407
407
  index += 1
408
408
  options[:require_profile_status] = args[index]
409
- when '--fallback'
409
+ when "--fallback"
410
410
  index += 1
411
411
  options[:fallback] = args[index]
412
412
  else
413
- if value.start_with?('--fallback=')
414
- options[:fallback] = value.delete_prefix('--fallback=')
415
- elsif value.start_with?('--')
413
+ if value.start_with?("--fallback=")
414
+ options[:fallback] = value.delete_prefix("--fallback=")
415
+ elsif value.start_with?("--")
416
416
  stderr.puts("unknown merge-driver option #{value.inspect}")
417
417
  return nil
418
418
  else
@@ -428,7 +428,7 @@ module Smorg
428
428
  options[:path_name] ||= positionals[3]
429
429
 
430
430
  unless options[:ancestor] && options[:current] && options[:other]
431
- stderr.puts('merge-driver requires ancestor, current, and other paths')
431
+ stderr.puts("merge-driver requires ancestor, current, and other paths")
432
432
  return nil
433
433
  end
434
434
  unless %w[none line local full-file].include?(options[:fallback])
@@ -442,7 +442,7 @@ module Smorg
442
442
  return EXIT_SUCCESS unless report_path
443
443
 
444
444
  report = {
445
- command: 'merge-driver',
445
+ command: "merge-driver",
446
446
  path_name: path_name,
447
447
  ok: ok,
448
448
  exit_code: exit_code,
@@ -462,16 +462,16 @@ module Smorg
462
462
  }
463
463
  File.write(report_path, "#{JSON.pretty_generate(Ast::Merge.json_ready(report))}\n")
464
464
  EXIT_SUCCESS
465
- rescue StandardError => e
465
+ rescue => e
466
466
  stderr.puts("write report: #{e.message}")
467
467
  EXIT_INTERNAL_ERROR
468
468
  end
469
469
 
470
470
  def fallback_reason(diagnostics)
471
471
  first = diagnostics.first
472
- return 'structured_merge_failed' unless first
472
+ return "structured_merge_failed" unless first
473
473
 
474
- (first[:category] || first['category'] || 'structured_merge_failed').to_s
474
+ (first[:category] || first["category"] || "structured_merge_failed").to_s
475
475
  end
476
476
 
477
477
  def report_and_enforce_profile(options, stdout, stderr)
@@ -480,16 +480,16 @@ module Smorg
480
480
  profile_id = options[:profile_id] || Ast::Merge::PROMOTION_PROFILE_JSON_KEYED_OBJECT
481
481
  evaluation = Ast::Merge::ProfilePromotionEvaluation.new(
482
482
  profile_id: profile_id,
483
- status: 'available',
484
- blocking_reasons: ['profile promotion evidence is not loaded by this CLI command'],
483
+ status: "available",
484
+ blocking_reasons: ["profile promotion evidence is not loaded by this CLI command"],
485
485
  diagnostics: []
486
486
  )
487
487
  decision = Ast::Merge.evaluate_profile_selection_requirement(
488
488
  Ast::Merge::ProfileSelectionRequirement.new(
489
489
  profile_id: profile_id,
490
490
  promotion_policy_id: Ast::Merge.initial_profile_promotion_policy.policy_id,
491
- minimum_profile_status: options[:require_profile_status] || 'available',
492
- enforcement_mode: options[:require_profile_status] ? 'required' : 'advisory'
491
+ minimum_profile_status: options[:require_profile_status] || "available",
492
+ enforcement_mode: options[:require_profile_status] ? "required" : "advisory"
493
493
  ),
494
494
  nil,
495
495
  evaluation
@@ -524,10 +524,10 @@ module Smorg
524
524
  index = 0
525
525
  while index < args.length
526
526
  value = args[index]
527
- if value == '--path-name'
527
+ if value == "--path-name"
528
528
  index += 1
529
529
  options[:path_name] = args[index]
530
- elsif value.start_with?('--')
530
+ elsif value.start_with?("--")
531
531
  stderr.puts("unknown diff-driver option #{value.inspect}")
532
532
  return nil
533
533
  else
@@ -541,9 +541,9 @@ module Smorg
541
541
  options.merge(old_path: positionals[0], new_path: positionals[1])
542
542
  when 7, 9
543
543
  options.merge(path_name: options[:path_name] || positionals[0], old_path: positionals[1],
544
- new_path: positionals[4])
544
+ new_path: positionals[4])
545
545
  else
546
- stderr.puts('diff-driver requires either 2, 7, or 9 positional arguments')
546
+ stderr.puts("diff-driver requires either 2, 7, or 9 positional arguments")
547
547
  nil
548
548
  end
549
549
  end
@@ -551,9 +551,9 @@ module Smorg
551
551
  def print_structured_diff(stdout, path_name, old_source, new_source)
552
552
  stdout.puts("structured-diff #{path_name}")
553
553
  if old_source == new_source
554
- stdout.puts('status unchanged')
554
+ stdout.puts("status unchanged")
555
555
  else
556
- stdout.puts('status changed')
556
+ stdout.puts("status changed")
557
557
  stdout.puts("old-lines #{line_count(old_source)}")
558
558
  stdout.puts("new-lines #{line_count(new_source)}")
559
559
  print_structured_diff_review_hunk(stdout, path_name, old_source, new_source)
@@ -563,7 +563,7 @@ module Smorg
563
563
  def print_structured_diff_review_hunk(stdout, path_name, old_source, new_source)
564
564
  old_lines = diff_source_lines(old_source)
565
565
  new_lines = diff_source_lines(new_source)
566
- stdout.puts('review-diff unified')
566
+ stdout.puts("review-diff unified")
567
567
  stdout.puts("--- a/#{path_name}")
568
568
  stdout.puts("+++ b/#{path_name}")
569
569
 
@@ -588,9 +588,9 @@ module Smorg
588
588
 
589
589
  def run_conflicts(args, stdout, stderr)
590
590
  subcommand, *rest = args
591
- return run_conflicts_diff(rest, stdout, stderr) if subcommand == 'diff'
591
+ return run_conflicts_diff(rest, stdout, stderr) if subcommand == "diff"
592
592
 
593
- stderr.puts('conflicts requires the diff subcommand')
593
+ stderr.puts("conflicts requires the diff subcommand")
594
594
  EXIT_USER_ERROR
595
595
  end
596
596
 
@@ -602,26 +602,26 @@ module Smorg
602
602
  settings = load_path_settings(effective_path)
603
603
  regions = find_conflict_regions(File.read(options[:file_path]), settings[:conflict_marker_size])
604
604
  print_conflict_diff(stdout, effective_path, regions)
605
- options[:exit_code] && !regions.empty? ? EXIT_UNRESOLVED_CONFLICT : EXIT_SUCCESS
605
+ (options[:exit_code] && !regions.empty?) ? EXIT_UNRESOLVED_CONFLICT : EXIT_SUCCESS
606
606
  rescue Errno::ENOENT, Errno::EACCES => e
607
607
  stderr.puts("read conflicted file: #{e.message}")
608
608
  EXIT_USER_ERROR
609
609
  end
610
610
 
611
611
  def parse_conflicts_diff_options(args, stderr)
612
- options = { exit_code: false }
612
+ options = {exit_code: false}
613
613
  positionals = []
614
614
  index = 0
615
615
  while index < args.length
616
616
  value = args[index]
617
617
  case value
618
- when '--path-name'
618
+ when "--path-name"
619
619
  index += 1
620
620
  options[:path_name] = args[index]
621
- when '--exit-code'
621
+ when "--exit-code"
622
622
  options[:exit_code] = true
623
623
  else
624
- if value.start_with?('--')
624
+ if value.start_with?("--")
625
625
  stderr.puts("unknown conflicts diff option #{value.inspect}")
626
626
  return nil
627
627
  end
@@ -630,82 +630,82 @@ module Smorg
630
630
  index += 1
631
631
  end
632
632
  if positionals.length != 1
633
- stderr.puts('conflicts diff requires exactly one file path')
633
+ stderr.puts("conflicts diff requires exactly one file path")
634
634
  return nil
635
635
  end
636
636
  options.merge(file_path: positionals[0])
637
637
  end
638
638
 
639
639
  def run_languages(args, stdout, stderr)
640
- unless args == ['--gitattributes']
641
- stderr.puts('languages currently requires --gitattributes')
640
+ unless args == ["--gitattributes"]
641
+ stderr.puts("languages currently requires --gitattributes")
642
642
  return EXIT_USER_ERROR
643
643
  end
644
644
 
645
645
  [
646
- '*.bash merge=smorg-rb diff=smorg-rb smorg.language=bash',
647
- '*.go merge=smorg-rb diff=smorg-rb smorg.language=go',
648
- '*.htm merge=smorg-rb diff=smorg-rb smorg.language=html',
649
- '*.html merge=smorg-rb diff=smorg-rb smorg.language=html',
650
- '*.env merge=smorg-rb diff=smorg-rb smorg.language=dotenv',
651
- '.env merge=smorg-rb diff=smorg-rb smorg.language=dotenv',
652
- '.env.* merge=smorg-rb diff=smorg-rb smorg.language=dotenv',
653
- '*.json merge=smorg-rb diff=smorg-rb smorg.language=json',
654
- '*.jsonc merge=smorg-rb diff=smorg-rb smorg.language=jsonc',
655
- '*.json5 merge=smorg-rb diff=smorg-rb smorg.language=json5',
656
- '*.md merge=smorg-rb diff=smorg-rb smorg.language=markdown',
657
- '*.markdown merge=smorg-rb diff=smorg-rb smorg.language=markdown',
658
- '*.rb merge=smorg-rb diff=smorg-rb smorg.language=ruby',
659
- '*.rbs merge=smorg-rb diff=smorg-rb smorg.language=rbs',
660
- '*.rs merge=smorg-rb diff=smorg-rb smorg.language=rust',
661
- '*.sh merge=smorg-rb diff=smorg-rb smorg.language=bash',
662
- '*.toml merge=smorg-rb diff=smorg-rb smorg.language=toml',
663
- '*.ts merge=smorg-rb diff=smorg-rb smorg.language=typescript',
664
- '*.tsx merge=smorg-rb diff=smorg-rb smorg.language=tsx',
665
- '*.yml merge=smorg-rb diff=smorg-rb smorg.language=yaml',
666
- '*.yaml merge=smorg-rb diff=smorg-rb smorg.language=yaml'
646
+ "*.bash merge=smorg-rb diff=smorg-rb smorg.language=bash",
647
+ "*.go merge=smorg-rb diff=smorg-rb smorg.language=go",
648
+ "*.htm merge=smorg-rb diff=smorg-rb smorg.language=html",
649
+ "*.html merge=smorg-rb diff=smorg-rb smorg.language=html",
650
+ "*.env merge=smorg-rb diff=smorg-rb smorg.language=dotenv",
651
+ ".env merge=smorg-rb diff=smorg-rb smorg.language=dotenv",
652
+ ".env.* merge=smorg-rb diff=smorg-rb smorg.language=dotenv",
653
+ "*.json merge=smorg-rb diff=smorg-rb smorg.language=json",
654
+ "*.jsonc merge=smorg-rb diff=smorg-rb smorg.language=jsonc",
655
+ "*.json5 merge=smorg-rb diff=smorg-rb smorg.language=json5",
656
+ "*.md merge=smorg-rb diff=smorg-rb smorg.language=markdown",
657
+ "*.markdown merge=smorg-rb diff=smorg-rb smorg.language=markdown",
658
+ "*.rb merge=smorg-rb diff=smorg-rb smorg.language=ruby",
659
+ "*.rbs merge=smorg-rb diff=smorg-rb smorg.language=rbs",
660
+ "*.rs merge=smorg-rb diff=smorg-rb smorg.language=rust",
661
+ "*.sh merge=smorg-rb diff=smorg-rb smorg.language=bash",
662
+ "*.toml merge=smorg-rb diff=smorg-rb smorg.language=toml",
663
+ "*.ts merge=smorg-rb diff=smorg-rb smorg.language=typescript",
664
+ "*.tsx merge=smorg-rb diff=smorg-rb smorg.language=tsx",
665
+ "*.yml merge=smorg-rb diff=smorg-rb smorg.language=yaml",
666
+ "*.yaml merge=smorg-rb diff=smorg-rb smorg.language=yaml"
667
667
  ].each { |line| stdout.puts(line) }
668
668
  EXIT_SUCCESS
669
669
  end
670
670
 
671
671
  MERGE_PROVIDER_OPTIONS = {
672
- 'bash' => {
673
- provider_id: 'ruby.bash', family: 'bash', dialect: 'bash', backend: 'kreuzberg-language-pack'
672
+ "bash" => {
673
+ provider_id: "ruby.bash", family: "bash", dialect: "bash", backend: "kreuzberg-language-pack"
674
674
  },
675
- 'go' => {
676
- provider_id: 'ruby.go', family: 'go', dialect: 'go', backend: 'kreuzberg-language-pack'
675
+ "go" => {
676
+ provider_id: "ruby.go", family: "go", dialect: "go", backend: "kreuzberg-language-pack"
677
677
  },
678
- 'html' => {
679
- provider_id: 'ruby.html', family: 'html', dialect: 'html', backend: 'kreuzberg-language-pack'
678
+ "html" => {
679
+ provider_id: "ruby.html", family: "html", dialect: "html", backend: "kreuzberg-language-pack"
680
680
  },
681
- 'dotenv' => { family: 'dotenv', dialect: 'dotenv', backend: 'dotenv-line' },
682
- 'json' => { family: 'json', dialect: 'json' },
683
- 'jsonc' => { family: 'json', dialect: 'jsonc' },
684
- 'json5' => { family: 'json', dialect: 'json5' },
685
- 'markdown' => {
686
- provider_id: 'ruby.markdown', family: 'markdown', dialect: 'markdown', backend: 'kreuzberg-language-pack'
681
+ "dotenv" => {family: "dotenv", dialect: "dotenv", backend: "dotenv-line"},
682
+ "json" => {family: "json", dialect: "json"},
683
+ "jsonc" => {family: "json", dialect: "jsonc"},
684
+ "json5" => {family: "json", dialect: "json5"},
685
+ "markdown" => {
686
+ provider_id: "ruby.markdown", family: "markdown", dialect: "markdown", backend: "kreuzberg-language-pack"
687
687
  },
688
- 'ruby' => { family: 'ruby', dialect: 'ruby', backend: 'prism' },
689
- 'rbs' => { provider_id: 'ruby.rbs', family: 'rbs', dialect: 'rbs', backend: 'rbs' },
690
- 'rust' => {
691
- provider_id: 'ruby.rust', family: 'rust', dialect: 'rust', backend: 'kreuzberg-language-pack'
688
+ "ruby" => {family: "ruby", dialect: "ruby", backend: "prism"},
689
+ "rbs" => {provider_id: "ruby.rbs", family: "rbs", dialect: "rbs", backend: "rbs"},
690
+ "rust" => {
691
+ provider_id: "ruby.rust", family: "rust", dialect: "rust", backend: "kreuzberg-language-pack"
692
692
  },
693
- 'toml' => {
694
- provider_id: 'ruby.toml', family: 'toml', dialect: 'toml', backend: 'kreuzberg-language-pack'
693
+ "toml" => {
694
+ provider_id: "ruby.toml", family: "toml", dialect: "toml", backend: "kreuzberg-language-pack"
695
695
  },
696
- 'typescript' => {
697
- provider_id: 'ruby.typescript', family: 'typescript', dialect: 'typescript',
698
- backend: 'kreuzberg-language-pack'
696
+ "typescript" => {
697
+ provider_id: "ruby.typescript", family: "typescript", dialect: "typescript",
698
+ backend: "kreuzberg-language-pack"
699
699
  },
700
- 'tsx' => {
701
- provider_id: 'ruby.typescript', family: 'typescript', dialect: 'tsx', backend: 'kreuzberg-language-pack'
700
+ "tsx" => {
701
+ provider_id: "ruby.typescript", family: "typescript", dialect: "tsx", backend: "kreuzberg-language-pack"
702
702
  },
703
- 'yaml' => { provider_id: 'ruby.yaml.psych', family: 'yaml', dialect: 'yaml', backend: 'psych' },
704
- 'text' => { family: 'text', dialect: 'text', profile_id: 'coarse_document' }
703
+ "yaml" => {provider_id: "ruby.yaml.psych", family: "yaml", dialect: "yaml", backend: "psych"},
704
+ "text" => {family: "text", dialect: "text", profile_id: "coarse_document"}
705
705
  }.freeze
706
706
 
707
707
  def merge_by_path(path_name, language, conflict_marker_size, fallback_policy, ancestor_source, current_source,
708
- other_source)
708
+ other_source)
709
709
  normalized_language = normalize_language(language, path_name)
710
710
  provider_options = MERGE_PROVIDER_OPTIONS[normalized_language]
711
711
  return unsupported_language_result(normalized_language, path_name) unless provider_options
@@ -731,7 +731,7 @@ module Smorg
731
731
  Ast::Merge::Git.merge3(
732
732
  **merge_options,
733
733
  path_name: path_name,
734
- profile_id: provider_options.fetch(:profile_id, 'source_preserving'),
734
+ profile_id: provider_options.fetch(:profile_id, "source_preserving"),
735
735
  **provider_options.except(:profile_id)
736
736
  )
737
737
  )
@@ -742,8 +742,8 @@ module Smorg
742
742
  ok: false,
743
743
  diagnostics: [
744
744
  {
745
- severity: 'error',
746
- category: 'unsupported_language',
745
+ severity: "error",
746
+ category: "unsupported_language",
747
747
  message: "no structured merge driver is configured for #{language.inspect} at #{path_name}"
748
748
  }
749
749
  ],
@@ -794,22 +794,22 @@ module Smorg
794
794
  end
795
795
 
796
796
  LANGUAGE_BY_EXTENSION = {
797
- '.bash' => 'bash', '.sh' => 'bash', '.go' => 'go', '.htm' => 'html', '.html' => 'html',
798
- '.rs' => 'rust', '.ts' => 'typescript', '.tsx' => 'tsx'
797
+ ".bash" => "bash", ".sh" => "bash", ".go" => "go", ".htm" => "html", ".html" => "html",
798
+ ".rs" => "rust", ".ts" => "typescript", ".tsx" => "tsx"
799
799
  }.freeze
800
800
 
801
801
  LANGUAGE_ALIASES = {
802
- 'bash' => 'bash', 'sh' => 'bash', 'application/x-sh' => 'bash', 'text/x-shellscript' => 'bash',
803
- 'go' => 'go', 'golang' => 'go', 'html' => 'html', 'htm' => 'html', 'text/html' => 'html', 'html5' => 'html',
804
- 'dotenv' => 'dotenv', 'env' => 'dotenv', 'config-env' => 'dotenv', 'json' => 'json', 'jsonc' => 'jsonc',
805
- 'json with comments' => 'jsonc', 'json5' => 'json5', 'markdown' => 'markdown', 'md' => 'markdown',
806
- 'gfm' => 'markdown', 'text/markdown' => 'markdown', 'ruby' => 'ruby', 'rb' => 'ruby',
807
- 'application/x-ruby' => 'ruby', 'rbs' => 'rbs', 'rust' => 'rust', 'rs' => 'rust',
808
- 'application/rust' => 'rust', 'text/rust' => 'rust', 'toml' => 'toml', 'application/toml' => 'toml',
809
- 'typescript' => 'typescript', 'ts' => 'typescript', 'application/typescript' => 'typescript',
810
- 'text/typescript' => 'typescript', 'tsx' => 'tsx', 'typescriptreact' => 'tsx', 'yaml' => 'yaml',
811
- 'yml' => 'yaml', 'application/yaml' => 'yaml', 'text/yaml' => 'yaml', 'plain' => 'text', 'text' => 'text',
812
- 'plaintext' => 'text', 'text/plain' => 'text'
802
+ "bash" => "bash", "sh" => "bash", "application/x-sh" => "bash", "text/x-shellscript" => "bash",
803
+ "go" => "go", "golang" => "go", "html" => "html", "htm" => "html", "text/html" => "html", "html5" => "html",
804
+ "dotenv" => "dotenv", "env" => "dotenv", "config-env" => "dotenv", "json" => "json", "jsonc" => "jsonc",
805
+ "json with comments" => "jsonc", "json5" => "json5", "markdown" => "markdown", "md" => "markdown",
806
+ "gfm" => "markdown", "text/markdown" => "markdown", "ruby" => "ruby", "rb" => "ruby",
807
+ "application/x-ruby" => "ruby", "rbs" => "rbs", "rust" => "rust", "rs" => "rust",
808
+ "application/rust" => "rust", "text/rust" => "rust", "toml" => "toml", "application/toml" => "toml",
809
+ "typescript" => "typescript", "ts" => "typescript", "application/typescript" => "typescript",
810
+ "text/typescript" => "typescript", "tsx" => "tsx", "typescriptreact" => "tsx", "yaml" => "yaml",
811
+ "yml" => "yaml", "application/yaml" => "yaml", "text/yaml" => "yaml", "plain" => "text", "text" => "text",
812
+ "plaintext" => "text", "text/plain" => "text"
813
813
  }.freeze
814
814
 
815
815
  def language_from_path(path_name)
@@ -820,7 +820,7 @@ module Smorg
820
820
  end
821
821
 
822
822
  def load_path_settings(path_name)
823
- settings = { conflict_marker_size: 7 }
823
+ settings = {conflict_marker_size: 7}
824
824
  attribute_files_for_path(path_name).each do |attributes_path|
825
825
  next unless File.file?(attributes_path)
826
826
 
@@ -831,9 +831,9 @@ module Smorg
831
831
 
832
832
  def attribute_files_for_path(path_name)
833
833
  clean_path = normalized_attribute_path(path_name)
834
- return ['.gitattributes'] if root_attribute_path?(clean_path)
834
+ return [".gitattributes"] if root_attribute_path?(clean_path)
835
835
 
836
- ['.gitattributes', *attribute_directory_paths(File.dirname(clean_path))]
836
+ [".gitattributes", *attribute_directory_paths(File.dirname(clean_path))]
837
837
  end
838
838
 
839
839
  def normalized_attribute_path(path_name)
@@ -845,13 +845,13 @@ module Smorg
845
845
 
846
846
  def root_attribute_path?(clean_path)
847
847
  dir = File.dirname(clean_path)
848
- dir == '.' || clean_path.start_with?('..') || Pathname.new(clean_path).absolute?
848
+ dir == "." || clean_path.start_with?("..") || Pathname.new(clean_path).absolute?
849
849
  end
850
850
 
851
851
  def attribute_directory_paths(dir)
852
852
  parts = dir.split(File::SEPARATOR).reject(&:empty?)
853
853
  parts.each_index.map do |index|
854
- File.join(*parts[0..index], '.gitattributes')
854
+ File.join(*parts[0..index], ".gitattributes")
855
855
  end
856
856
  end
857
857
 
@@ -863,7 +863,7 @@ module Smorg
863
863
 
864
864
  def attribute_fields(raw_line, path_name)
865
865
  line = raw_line.strip
866
- return [] if line.empty? || line.start_with?('#')
866
+ return [] if line.empty? || line.start_with?("#")
867
867
 
868
868
  pattern, *fields = line.split(/\s+/)
869
869
  return [] if fields.empty? || !attribute_pattern_matches?(pattern, path_name)
@@ -872,25 +872,25 @@ module Smorg
872
872
  end
873
873
 
874
874
  ATTRIBUTE_KEYS = {
875
- 'smorg.language' => :language,
876
- 'linguist-language' => :language,
877
- 'smorg.profile' => :profile_id,
878
- 'smorg.requireProfileStatus' => :require_profile_status
875
+ "smorg.language" => :language,
876
+ "linguist-language" => :language,
877
+ "smorg.profile" => :profile_id,
878
+ "smorg.requireProfileStatus" => :require_profile_status
879
879
  }.freeze
880
880
 
881
881
  def apply_attribute_field(settings, field)
882
- key, value = field.split('=', 2)
882
+ key, value = field.split("=", 2)
883
883
  return unless value
884
884
  return settings[ATTRIBUTE_KEYS[key]] = value if ATTRIBUTE_KEYS.key?(key)
885
885
 
886
886
  marker_size = value.to_i
887
- settings[:conflict_marker_size] = marker_size if key == 'conflict-marker-size' && marker_size.positive?
887
+ settings[:conflict_marker_size] = marker_size if key == "conflict-marker-size" && marker_size.positive?
888
888
  end
889
889
 
890
890
  def attribute_pattern_matches?(pattern, path_name)
891
891
  return true if pattern == path_name
892
892
 
893
- if !pattern.include?('/')
893
+ if !pattern.include?("/")
894
894
  File.fnmatch?(pattern, File.basename(path_name))
895
895
  else
896
896
  File.fnmatch?(pattern, path_name)
@@ -910,12 +910,12 @@ module Smorg
910
910
 
911
911
  def conflict_marker_prefixes(marker_size)
912
912
  size = [marker_size.to_i, 1].max
913
- { start: '<' * size, separator: '=' * size, end: '>' * size }
913
+ {start: "<" * size, separator: "=" * size, end: ">" * size}
914
914
  end
915
915
 
916
916
  def conflict_region_state(current, line, line_number, prefixes)
917
917
  if line.start_with?(prefixes[:start])
918
- [{ start_line: line_number, separator_line: 0 }, nil]
918
+ [{start_line: line_number, separator_line: 0}, nil]
919
919
  elsif current && current[:separator_line].zero? && line.start_with?(prefixes[:separator])
920
920
  [current.merge(separator_line: line_number), nil]
921
921
  elsif current && line.start_with?(prefixes[:end])
@@ -947,4 +947,4 @@ module Smorg
947
947
  end # rubocop:enable Metrics/ModuleLength
948
948
  end
949
949
 
950
- require 'rust/merge'
950
+ require "rust/merge"