Class: N2B::MergeCLI

Inherits:
Base
  • Object
show all
Defined in:
lib/n2b/merge_cli.rb

Constant Summary collapse

COLOR_RED =
"\e[31m"
COLOR_GREEN =
"\e[32m"
COLOR_YELLOW =
"\e[33m"
COLOR_BLUE =
"\e[34m"
COLOR_GRAY =
"\e[90m"
COLOR_RESET =
"\e[0m"

Constants inherited from Base

Base::CONFIG_FILE, Base::HISTORY_FILE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#get_config, #load_config

Constructor Details

#initialize(args) ⇒ MergeCLI



17
18
19
20
21
# File 'lib/n2b/merge_cli.rb', line 17

def initialize(args)
  @args = args
  @options = parse_options
  @file_path = @args.shift
end

Class Method Details

.run(args) ⇒ Object



13
14
15
# File 'lib/n2b/merge_cli.rb', line 13

def self.run(args)
  new(args).execute
end

Instance Method Details

#executeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/n2b/merge_cli.rb', line 23

def execute
  if @file_path.nil?
    show_usage_and_unresolved
    exit 1
  end

  unless File.exist?(@file_path)
    puts "File not found: #{@file_path}"
    exit 1
  end

  config = get_config(reconfigure: false, advanced_flow: false)

  parser = MergeConflictParser.new(context_lines: @options[:context_lines])
  blocks = parser.parse(@file_path)
  if blocks.empty?
    puts "No merge conflicts found."
    return
  end

  lines = File.readlines(@file_path, chomp: true)
  log_entries = []
  aborted = false

  blocks.reverse_each do |block|
    result = resolve_block(block, config, lines.join("\n"))
    log_entries << result.merge({
      base_content: block.base_content,
      incoming_content: block.incoming_content,
      base_label: block.base_label,
      incoming_label: block.incoming_label
    })
    if result[:abort]
      aborted = true
      break
    elsif result[:accepted]
      replacement = result[:merged_code].to_s.split("\n")
      lines[(block.start_line-1)...block.end_line] = replacement
    end
  end

  unless aborted
    File.write(@file_path, lines.join("\n") + "\n")

    # Show summary
    accepted_count = log_entries.count { |entry| entry[:accepted] }
    skipped_count = log_entries.count { |entry| !entry[:accepted] && !entry[:abort] }

    puts "\n#{COLOR_BLUE}📊 Resolution Summary:#{COLOR_RESET}"
    puts "#{COLOR_GREEN}✅ Accepted: #{accepted_count}#{COLOR_RESET}"
    puts "#{COLOR_YELLOW}⏭️  Skipped: #{skipped_count}#{COLOR_RESET}" if skipped_count > 0

    # Only auto-mark as resolved if ALL conflicts were accepted (none skipped)
    if accepted_count > 0 && skipped_count == 0
      mark_file_as_resolved(@file_path)
      puts "#{COLOR_GREEN}🎉 All conflicts resolved! File marked as resolved in VCS.#{COLOR_RESET}"
    elsif accepted_count > 0 && skipped_count > 0
      puts "#{COLOR_YELLOW}⚠️  Some conflicts were skipped - file NOT marked as resolved#{COLOR_RESET}"
      puts "#{COLOR_GRAY}💡 Resolve remaining conflicts or manually mark: hg resolve --mark #{@file_path}#{COLOR_RESET}"
    else
      puts "#{COLOR_YELLOW}⚠️  No conflicts were accepted - file NOT marked as resolved#{COLOR_RESET}"
    end
  else
    puts "\n#{COLOR_YELLOW}⚠️  Resolution aborted - no changes made#{COLOR_RESET}"
  end

  if config['merge_log_enabled'] && log_entries.any?
    dir = '.n2b_merge_log'
    FileUtils.mkdir_p(dir)
    timestamp = Time.now.strftime('%Y-%m-%d-%H%M%S')
    log_path = File.join(dir, "#{timestamp}.json")
    File.write(log_path, JSON.pretty_generate({file: @file_path, timestamp: Time.now, entries: log_entries}))
    puts "#{COLOR_GRAY}📝 Merge log saved to #{log_path}#{COLOR_RESET}"
  end
end