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"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

config_file, #get_config, history_file, #load_config, test_environment?

Constructor Details

#initialize(args) ⇒ MergeCLI

Returns a new instance of MergeCLI.



26
27
28
29
30
# File 'lib/n2b/merge_cli.rb', line 26

def initialize(args)
  @args = args
  @options = parse_options
  # @file_path = @args.shift # Moved to execute based on mode
end

Class Method Details

.run(args) ⇒ Object



22
23
24
# File 'lib/n2b/merge_cli.rb', line 22

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

Instance Method Details

#executeObject



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/n2b/merge_cli.rb', line 32

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

  if @options[:analyze]
    # In analyze mode, @args might be used for custom prompt additions later,
    # similar to how original cli.rb handled it.
    # For now, custom_message option is the primary way.
    handle_diff_analysis(config)
  else
    @file_path = @args.shift
    if @file_path.nil?
      show_help_and_status # Renamed from show_usage_and_unresolved
      exit 1
    end

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

    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,
      start_line: block.start_line,
      end_line: block.end_line,
      resolved_content: result[:merged_code],
      llm_suggestion: result[:reason],
      resolution_method: determine_resolution_method(result),
      action: determine_action(result),
      timestamp: Time.now.strftime('%Y-%m-%d %H:%M:%S UTC')
    })
    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}.html")
    html_content = generate_merge_log_html(log_entries, timestamp)
    File.write(log_path, html_content)
    puts "#{COLOR_GRAY}📝 Merge log saved to #{log_path}#{COLOR_RESET}"
  end
  end
end