Skip to content

Commit

Permalink
Reformating change
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt-schesch committed Sep 26, 2023
1 parent 40e90af commit 1d1b847
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/scripts/merge_tools/resolve-import-conflicts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SCRIPTDIR="$(cd "$(dirname "$0")" && pwd -P)"
status=0

for file in "${files[@]}" ; do
if ! "${SCRIPTDIR}"/resolve-import-conflicts-in-file.py "$file" ; then
if ! "${SCRIPTDIR}"/resolve-import-conflicts-in-file.py --file "$file" ; then
status=1
fi
done
Expand Down
76 changes: 35 additions & 41 deletions src/scripts/merge_tools/resolve-import-conflicts-in-file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,12 @@

# TODO: merge both scripts into one.

import os
import shutil
import sys
import argparse
from pathlib import Path
import tempfile
from typing import List, Union, Tuple

if len(sys.argv) != 2:
print(
"resolve-import-conflicts-in-file: Provide exactly one command-line argument."
)
sys.exit(1)

filename = sys.argv[1]
with open(filename) as file:
lines = file.readlines()


def all_import_lines(lines: List[str]) -> bool:
"""Return true if every line is a Java import line."""
return all(line.startswith("import ") for line in lines)
Expand Down Expand Up @@ -55,15 +44,15 @@ def merge(base, parent1: List[str], parent2: List[str]) -> Union[List[str], None


def looking_at_conflict( # pylint: disable=too-many-return-statements
start_index: int, lines: List[str]
file: Path, start_index: int, lines: List[str]
) -> Union[None, Tuple[List[str], List[str], List[str], int]]:
"""Tests whether the text starting at line `start_index` is the beginning of a conflict.
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.
Args:
start_index: an index into `lines`.
lines: all the lines of the file with name `filename`.
lines: all the lines of the file with name `file`.
"""

if not lines[start_index].startswith("<<<<<<<"):
Expand All @@ -87,13 +76,13 @@ def looking_at_conflict( # pylint: disable=too-many-return-statements
"Starting at line "
+ str(start_index)
+ ", did not find ||||||| or ======= in "
+ filename
+ str(file)
)
return None
if lines[index].startswith("|||||||"):
index = index + 1
if index == num_lines:
print("File ends with |||||||: " + filename)
print("File ends with |||||||: " + str(file))
return None
while not lines[index].startswith("======="):
base.append(lines[index])
Expand All @@ -103,13 +92,13 @@ def looking_at_conflict( # pylint: disable=too-many-return-statements
"Starting at line "
+ str(start_index)
+ ", did not find ======= in "
+ filename
+ str(file)
)
return None
assert lines[index].startswith("=======")
index = index + 1 # skip over "=======" line
if index == num_lines:
print("File ends with =======: " + filename)
print("File ends with =======: " + str(file))
return None
while not lines[index].startswith(">>>>>>>"):
parent2.append(lines[index])
Expand All @@ -119,35 +108,40 @@ def looking_at_conflict( # pylint: disable=too-many-return-statements
"Starting at line "
+ str(start_index)
+ ", did not find >>>>>>> in "
+ filename
+ str(file)
)
return None
index = index + 1

return (base, parent1, parent2, index - start_index)


## Main starts here.

with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
file_len = len(lines)
i = 0
while i < file_len:
conflict = looking_at_conflict(i, lines)
if conflict is None:
tmp.write(lines[i])
i = i + 1
else:
(base, parent1, parent2, num_lines) = conflict
merged = merge(base, parent1, parent2)
if merged is None:
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--file", type=Path)
args = parser.parse_args()

with open(args.file) as file:
lines = file.readlines()

with tempfile.NamedTemporaryFile(mode="w", delete=True) as tmp:
file_len = len(lines)
i = 0
while i < file_len:
conflict = looking_at_conflict(Path(args.file), i, lines)
if conflict is None:
tmp.write(lines[i])
i = i + 1
else:
for line in merged:
tmp.write(line)
i = i + num_lines

tmp.close()
shutil.copy(tmp.name, filename)
os.unlink(tmp.name)
(base, parent1, parent2, num_lines) = conflict
merged = merge(base, parent1, parent2)
if merged is None:
tmp.write(lines[i])
i = i + 1
else:
for line in merged:
tmp.write(line)
i = i + num_lines

# Copying the file back to the original location
shutil.copyfile(tmp.name, args.file)

0 comments on commit 1d1b847

Please sign in to comment.