Class: FixTSVConflict::Resolver
- Inherits:
-
Object
- Object
- FixTSVConflict::Resolver
- Includes:
- Logging
- Defined in:
- lib/fix_tsv_conflict/resolver.rb
Constant Summary collapse
- BLANK_RE =
/\A[[:space:]]*\z/
Instance Attribute Summary collapse
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#tabs ⇒ Object
Returns the value of attribute tabs.
Instance Method Summary collapse
- #index_by_id(lines) ⇒ Object
-
#initialize(stdin: $stdin, stderr: $stderr) ⇒ Resolver
constructor
A new instance of Resolver.
- #pick_by_tabs(l, r) ⇒ Object
- #resolve(conflict) ⇒ Object
- #select(conflict) ⇒ Object
- #try(conflict) ⇒ Object
Methods included from Logging
#blank, #dump, #error, #info, #log, #notice, #warn
Constructor Details
#initialize(stdin: $stdin, stderr: $stderr) ⇒ Resolver
Returns a new instance of Resolver.
19 20 21 22 23 24 |
# File 'lib/fix_tsv_conflict/resolver.rb', line 19 def initialize(stdin: $stdin, stderr: $stderr) @stdin = stdin @stderr = stderr @tabs = 0 end |
Instance Attribute Details
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
16 17 18 |
# File 'lib/fix_tsv_conflict/resolver.rb', line 16 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
16 17 18 |
# File 'lib/fix_tsv_conflict/resolver.rb', line 16 def stdin @stdin end |
#tabs ⇒ Object
Returns the value of attribute tabs.
17 18 19 |
# File 'lib/fix_tsv_conflict/resolver.rb', line 17 def tabs @tabs end |
Instance Method Details
#index_by_id(lines) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/fix_tsv_conflict/resolver.rb', line 87 def index_by_id(lines) result = {} lines.each do |line| id = line.split(TAB, 2).first result[id] = line end result end |
#pick_by_tabs(l, r) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/fix_tsv_conflict/resolver.rb', line 96 def pick_by_tabs(l, r) ltabs = l.count(TAB) rtabs = r.count(TAB) if ltabs == tabs l elsif rtabs == tabs r else # both are wrong. # so this is a determistic picking. ltabs < rtabs ? l : r end end |
#resolve(conflict) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fix_tsv_conflict/resolver.rb', line 26 def resolve(conflict) unless conflict.valid? return conflict.to_a end result = try(conflict) if result return result end warn "Failed to resolve it automatically." select(conflict) end |
#select(conflict) ⇒ Object
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 |
# File 'lib/fix_tsv_conflict/resolver.rb', line 60 def select(conflict) text = "Which branch do you want to keep?\n\n 1) \#{conflict.lbranch}\n 2) \#{conflict.rbranch}\n k) keep as is\n\n TEXT\n\n info text\n\n loop do\n info \"Please enter 1, 2, or k: \", no_newline: true\n case selected = stdin.gets.strip\n when \"1\"\n return conflict.left\n when \"2\"\n return conflict.right\n when \"k\"\n return conflict.to_a\n else\n info \"Invalid input: \#{selected}\"\n end\n end\nend\n" |
#try(conflict) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fix_tsv_conflict/resolver.rb', line 40 def try(conflict) result = [] left = index_by_id(conflict.left.reject { |l| l.blank? }) right = index_by_id(conflict.right.reject { |r| r.blank? }) (left.keys + right.keys).uniq.sort.each do |id| l = left[id] r = right[id] if l && r if l.rstrip == r.rstrip result << pick_by_tabs(l, r) else return false end else result << (l || r) end end result end |