Last active
March 21, 2017 14:26
-
-
Save zombience/aee8acef2c4d5f10b63d to your computer and use it in GitHub Desktop.
this is a python script intended for use with unity projects using SVN for version control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# This script is intended for use with unity projects using subversion for version control systems | |
# | |
# This pre-commit hook will prevent a commit that includes files in the Assets/ folder | |
# that do not have a companion .meta file. | |
# Adding asset files without meta files creates a scenario where multiple people | |
# will have conflicting meta files, causing conflicting settings for imported assets, | |
# conflicts over who owns the authoritative .meta file, endless confusing notifications about .meta files | |
# needing to be updated or committed, and generally leads to the dark side. | |
import os | |
import sys | |
import subprocess | |
import random | |
LOOK='svnlook' | |
WARN='Found files with no matching meta files in commit' | |
def main(argv): | |
result = 'success' | |
txn = argv[1] | |
repo = argv[2] | |
commit_info = get_commit_info(txn, repo) | |
if 'META_OVERRIDE' in commit_info['comment']: | |
exit('override found... skipping', 0) | |
return | |
raw_file_dump = svn_look('diff', txn, '-t', argv[2]) | |
committed_files = files_changed(raw_file_dump) | |
lonely_files = [] | |
for f in committed_files: | |
# Skip any files not contained in the Assets directory; these are not under our control. | |
if not '/Assets/' in f: | |
continue | |
if not '.meta' in f: | |
meta = f + '.meta' | |
if not meta in committed_files: | |
lonely_files.append(f) | |
if(len(lonely_files) > 0): | |
result = WARN + '\n' + 'The following files have no matching meta file.\n' + '\n'.join(lonely_files) + '\n' + 'Open up the unity project and meta files will be auto-generated' | |
exit(result, len(lonely_files)) | |
def files_changed(files): | |
def filename(line): | |
return line[4:] | |
def added(line): | |
return line and line[0] in ("A") | |
return [ | |
filename(line) | |
for line in files.split("\n") | |
if added(line)] | |
def get_commit_info(repo, revision ): | |
comment = svn_look('log', repo, '-t', revision) | |
author = svn_look('author', repo, '-t', revision) | |
files = svn_look('changed', repo, '-t', revision) | |
commit_info = {'revision' : revision, 'comment' : comment, 'author': author, 'files':files} | |
return commit_info | |
def svn_look( *args ): | |
p = subprocess.Popen(' '.join([LOOK] + list(args)), stdout=subprocess.PIPE, shell=True, stderr=subprocess.STDOUT ) | |
out, err = p.communicate() | |
return out | |
def exit(msg, status): | |
import os.path | |
stream = sys.stderr or sys.stdout | |
stream.write(msg) | |
sys.exit(status) | |
if __name__ == '__main__': | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pre-commit.py
python 2.7
this is a python script is intended for use with Unity3D projects using SVN for version control
depending on your particular svn configuration, you will lilely have to rename pre-commit.py to 'pre-commit'
place this file in your path/to/repo/hooks/ directory, and SVN should take care of the rest
this script does not require any additional python modules