Skip to content

Instantly share code, notes, and snippets.

@veloutin
Last active August 29, 2015 14:13
Show Gist options
  • Save veloutin/afd85bdb77d1f9d9a852 to your computer and use it in GitHub Desktop.
Save veloutin/afd85bdb77d1f9d9a852 to your computer and use it in GitHub Desktop.
Freeze Buildout Merges
#!/usr/bin/env python
# *-* coding: utf-8 *-*
from __future__ import print_function, unicode_literals
import os
import sys
import subprocess
def get_merged_version(cwd, branch):
""" Attempt to find the version at which branch was merged """
output = subprocess.check_output(
["git", "log", "--pretty=%P", "-1", "--grep", branch],
cwd=cwd,
)
return output.split()[1].decode("utf-8")
def get_version(cwd, branch):
return subprocess.check_output(
["git", "log", "--pretty=%H", "-1", branch],
cwd=cwd
).strip().decode("utf-8")
buildout_file = sys.argv[1]
buildout_dir = os.path.dirname(os.path.abspath(buildout_file))
with open(sys.argv[1], "r") as f:
lines = f.readlines()
def write_lines(lines, outfile=sys.stdout):
cwd = None
for line in lines:
print_line = line
line = line.strip()
if line.startswith("cd "):
cwd = line[3:].replace("${buildout:directory}", buildout_dir)
if line.startswith("git merge"):
version = line.split()[2]
if not (version.isalnum() and len(version) == 40):
try:
new_version = get_version(cwd, version)
except Exception:
new_version = get_merged_version(cwd, version)
print_line = print_line.replace(
version,
"{0} # {1}".format(new_version, version)
)
print(print_line, end='', file=outfile)
if len(sys.argv) > 2:
target = sys.argv[2]
else:
target = sys.argv[1]
with open(target, "w") as f:
write_lines(lines, f)
@bwrsandman
Copy link

["git", "log", "--pretty=%H", "-1", branch],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment