Skip to content

Instantly share code, notes, and snippets.

@MichaelChirico
Last active March 31, 2024 18:02
Show Gist options
  • Save MichaelChirico/0800f0652304788d8b0748636116fc7a to your computer and use it in GitHub Desktop.
Save MichaelChirico/0800f0652304788d8b0748636116fc7a to your computer and use it in GitHub Desktop.
Select from 'left' file to resolve conflicts
def select_left(file):
with open(file) as f:
contents=f.read()
lines = contents.split('\n')
n_conflicts = sum([l.startswith('<<<<') for l in lines])
keep=True
outfile=[]
for line in lines:
if line.startswith('===='):
keep=False
elif line.startswith('>>>>'):
keep=True
elif keep and not line.startswith('<<<<'):
outfile.append(line)
conflicts_markers_remaining = sum([l.startswith('<<<<') or l.startswith('====') or l.startswith('>>>>') for l in outfile])
if conflicts_markers_remaining:
raise ValueError('Still {} conflict markers remaining!'.format(conflict_markers_remaining))
print('Removed {} lines and {} conflicts'.format((len(lines) - len(outfile)), n_conflicts))
with open(file, 'w') as f:
for line in outfile:
f.write(line+'\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment