Skip to content

Instantly share code, notes, and snippets.

@zecode
Last active December 15, 2015 20:19
Show Gist options
  • Save zecode/5317533 to your computer and use it in GitHub Desktop.
Save zecode/5317533 to your computer and use it in GitHub Desktop.
'Do Not Commit' subversion pre-commit hook.
'Do Not Commit' subversion pre-commit hook.
Concept. Sometimes to accelerate development I change production code from "const int CooldownMls = 5000;" to "const int CooldownMls = 0; // dnc"
Another example can be hardcoding db url to localhost like: $conn = createConn('127.1'); // dnc
Notice 'dnc' tag. It prevents the file from accidental commit. My svn repo rejects text files with such tag inside.
Installation.
1. Copy files to $REPOS/hooks
3. Then chmod +x both.
#!/usr/bin/python
" Do Not Commit Subversion pre-commit hook. "
def command_output(cmd):
" Capture a command's standard output. "
import subprocess
return subprocess.Popen(
cmd.split(), stdout=subprocess.PIPE).communicate()[0]
def files_changed(look_cmd):
""" List the files added or updated by this transaction.
"svnlook changed" gives output like:
U trunk/file1.cpp
A trunk/file2.cpp
"""
def filename(line):
return line[4:]
def added_or_updated(line):
return line and line[0] in ("A", "U")
return [
filename(line)
for line in command_output(look_cmd % "changed").split("\n")
if added_or_updated(line)]
def file_contents(filename, look_cmd):
" Return a file's contents for this transaction. "
return command_output(
"%s %s" % (look_cmd % "cat", filename))
def contains_tag(filename, look_cmd):
" Return True if this version of the file contains tag. "
txt = file_contents(filename, look_cmd)
import re
return re.search('dnc', txt, flags=re.IGNORECASE) is not None
def check_files_for_tag(look_cmd):
" Check files in this transaction are tag free. "
def is_txt_file(fname):
import os
return os.path.splitext(fname)[1] in ".txt .cs .h .hpp .c .cpp .java .properties .xml .htm .html .cmd .bat".split()
txt_files_with_tag = [
ff for ff in files_changed(look_cmd)
if is_txt_file(ff) and contains_tag(ff, look_cmd)]
if len(txt_files_with_tag) > 0:
sys.stderr.write("'Do Not Commit' tag in the following files:\n%s\nI mean they contains '#dnc' or '@dnc' in either case\n"
% "\n".join(txt_files_with_tag))
return len(txt_files_with_tag)
def main():
usage = """usage: %prog REPOS TXN
Run pre-commit options on a repository transaction."""
from optparse import OptionParser
parser = OptionParser(usage=usage)
parser.add_option("-r", "--revision",
help="Test mode. TXN actually refers to a revision.",
action="store_true", default=False)
errors = 0
try:
(opts, (repos, txn_or_rvn)) = parser.parse_args()
look_opt = ("--transaction", "--revision")[opts.revision]
look_cmd = "svnlook %s %s %s %s" % (
"%s", repos, look_opt, txn_or_rvn)
errors += check_files_for_tag(look_cmd)
except:
import sys
sys.stderr.write("Unexpected error: " + str(sys.exc_info()) + '\n')
parser.print_help()
errors += 1
return errors
if __name__ == "__main__":
import sys
sys.exit(main())
#!/bin/sh
REPOS="$1"
TXN="$2"
$REPOS/hooks/do-not-commit.py $REPOS $TXN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment