Skip to content

Commit

Permalink
Turn off diagnostic output
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst committed Sep 27, 2023
1 parent 623b6e0 commit 5bb20c5
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/scripts/merge_tools/resolve-conflicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
import shutil
import sys
import tempfile
from typing import List, Union, Tuple

from typing import List, Union, Tuple, TypeVar
from collections.abc import Sequence

T = TypeVar("T") # Type variable for use in type hints

# If true, print diagnostic output
debug = False


def main(): # pylint: disable=too-many-locals
Expand Down Expand Up @@ -86,6 +93,7 @@ def looking_at_conflict( # pylint: disable=too-many-return-statements
If not, returns None.
If so, returns a 4-tuple of (base, parent1, parent2, num_lines_in_conflict)
where the first 3 elements of the tuple are lists of lines.
The filename argument is used only for diagnostic messages.
"""

if not lines[start_index].startswith("<<<<<<<"):
Expand All @@ -105,7 +113,7 @@ def looking_at_conflict( # pylint: disable=too-many-return-statements
parent1.append(lines[index])
index = index + 1
if index == num_lines:
print(
debug_print(
"Starting at line "
+ str(start_index)
+ ", did not find ||||||| or ======= in "
Expand All @@ -115,13 +123,13 @@ def looking_at_conflict( # pylint: disable=too-many-return-statements
if lines[index].startswith("|||||||"):
index = index + 1
if index == num_lines:
print("File ends with |||||||: " + filename)
debug_print("File ends with |||||||: " + filename)
return None
while not lines[index].startswith("======="):
base.append(lines[index])
index = index + 1
if index == num_lines:
print(
debug_print(
"Starting at line "
+ str(start_index)
+ ", did not find ======= in "
Expand All @@ -131,13 +139,13 @@ def looking_at_conflict( # pylint: disable=too-many-return-statements
assert lines[index].startswith("=======")
index = index + 1 # skip over "=======" line
if index == num_lines:
print("File ends with =======: " + filename)
debug_print("File ends with =======: " + filename)
return None
while not lines[index].startswith(">>>>>>>"):
parent2.append(lines[index])
index = index + 1
if index == num_lines:
print(
debug_print(
"Starting at line "
+ str(start_index)
+ ", did not find >>>>>>> in "
Expand Down Expand Up @@ -167,16 +175,16 @@ def merge(
a list of lines, or None if it cannot do merging.
"""

print(base, parent1, parent2)

if java_imports:
if (
all_import_lines(base)
and all_import_lines(parent1)
and all_import_lines(parent2)
):
# A simplistic merge that retains all import lines in either parent.
return list(set(parent1 + parent2)).sort()
result = list(set(parent1 + parent2))
result.sort()
return result

if adjacent_lines:
adjacent_line_merge = merge_edits_on_different_lines(base, parent1, parent2)
Expand All @@ -198,7 +206,7 @@ def merge_edits_on_different_lines(
Otherwise, return None.
"""

print("Entered merge_edits_on_different_lines")
debug_print("Entered merge_edits_on_different_lines", len(parent1), len(parent2))

### No lines are added or removed, only modified.
base_len = len(base)
Expand All @@ -208,7 +216,7 @@ def merge_edits_on_different_lines(
for base_line, parent1_line, parent2_line in itertools.zip_longest(
base, parent1, parent2
):
print("Considering line:", base_line, parent1_line, parent2_line)
debug_print("Considering line:", base_line, parent1_line, parent2_line)
if parent1_line == parent2_line:
result.append(parent1_line)
elif base_line == parent1_line:
Expand All @@ -218,9 +226,9 @@ def merge_edits_on_different_lines(
else:
result = None
break
print("merge_edits_on_different_lines =>", result)
debug_print("merge_edits_on_different_lines: first attempt =>", result)
if result is not None:
print("merge_edits_on_different_lines =>", result)
debug_print("merge_edits_on_different_lines =>", result)
return result

### Deletions at the beginning or end.
Expand All @@ -236,7 +244,7 @@ def merge_edits_on_different_lines(
if is_subsequence(parent1, base) and is_subsequence(parent2, base):
return []

print("merge_edits_on_different_lines =>", result)
debug_print("merge_edits_on_different_lines =>", result)
return result


Expand All @@ -256,15 +264,15 @@ def merge_base_is_prefix_or_suffix(
parent2_len = len(parent2)
if base_len < parent1_len:
if parent1[:base_len] == base:
print("startswith", parent1, base)
debug_print("startswith", parent1, base)
return parent2 + parent1[base_len:]
if parent1[-base_len:] == base:
print("endswith", parent1, base)
debug_print("endswith", parent1, base)
return parent1[:-base_len] + parent2
return None


def is_subsequence(s1: List[T], s2: List[T]) -> bool:
def is_subsequence(s1: Sequence[T], s2: Sequence[T]) -> bool:
"""Returns true if s1 is subsequence of s2."""

# Iterative implementation.
Expand All @@ -281,5 +289,11 @@ def is_subsequence(s1: List[T], s2: List[T]) -> bool:
return i == n


def debug_print(*args):
"""If debugging is enabled, pass the arguments to `print`."""
if True:
print(*args)


if __name__ == "__main__":
main()

0 comments on commit 5bb20c5

Please sign in to comment.