Skip to content

Commit cf8c229

Browse files
author
Scott Baker
committed
Properly remove the right number of leading columns in a three way merge
1 parent 1c417ac commit cf8c229

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

lib/diff-so-fancy.pl

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,6 @@ sub strip_empty_first_line {
128128
return 1;
129129
}
130130

131-
# Remove + or - at the beginning of the lines
132-
sub strip_leading_indicators {
133-
my $array = shift(); # Array passed in by reference
134-
135-
foreach my $line (@$array) {
136-
$line =~ s/^(${ansi_color_regex})[+-]/$1 /;
137-
}
138-
139-
return 1;
140-
}
141-
142-
# Remove the first space so everything aligns left
143-
sub strip_first_column {
144-
my $array = shift(); # Array passed in by reference
145-
146-
foreach my $line (@$array) {
147-
$line =~ s/^(${ansi_color_regex})[[:space:]]/$1/;
148-
}
149-
150-
return 1;
151-
}
152-
153131
sub mark_empty_lines {
154132
my $array = shift(); # Array passed in by reference
155133

@@ -177,9 +155,6 @@ sub clean_up_input {
177155
# Remove + or - at the beginning of the lines
178156
if ($strip_leading_indicators) {
179157
strip_leading_indicators($input_array_ref);
180-
181-
# Remove the first space so everything aligns left
182-
strip_first_column($input_array_ref);
183158
}
184159

185160

@@ -229,3 +204,41 @@ sub start_line_calc {
229204

230205
return $ret;
231206
}
207+
208+
# Remove + or - at the beginning of the lines
209+
sub strip_leading_indicators {
210+
my $array = shift(); # Array passed in by reference
211+
my $columns_to_remove = 1; # Default to 1 (two-way merge)
212+
213+
foreach my $line (@$array) {
214+
# If the line is a hunk line, check for two-way vs three-way merge
215+
# Two-way = @@ -132,6 +132,9 @@
216+
# Three-way = @@@ -48,10 -48,10 +48,15 @@@
217+
if ($line =~ /^${ansi_color_regex}@@@* (.+?) @@@*/) {
218+
$columns_to_remove = (char_count(",",$4)) - 1;
219+
last;
220+
}
221+
}
222+
223+
foreach my $line (@$array) {
224+
# Remove a number of "+", "-", or spaces equal to the indent level
225+
$line =~ s/^(${ansi_color_regex})[ +-]{${columns_to_remove}}/$1/;
226+
}
227+
228+
return 1;
229+
}
230+
231+
# Count the number of a given char in a string
232+
sub char_count {
233+
my ($needle,$str) = @_;
234+
my $len = length($str);
235+
my $ret = 0;
236+
237+
for (my $i = 0; $i < $len; $i++) {
238+
my $found = substr($str,$i,1);
239+
240+
if ($needle eq $found) { $ret++; }
241+
}
242+
243+
return $ret;
244+
}

0 commit comments

Comments
 (0)