Created
October 21, 2014 02:10
-
-
Save Experiment5X/090cdf337733b8be8bdf to your computer and use it in GitHub Desktop.
Put CVS comments at the top of files in your git repo. I made this because my CS class at RIT requires you to use CVS for these comments, and I would much rather use git.
This file contains hidden or 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
import sys | |
import os | |
import re | |
import subprocess | |
class CommitInfo: | |
date = '' | |
timestamp = '' | |
message = '' | |
class RepoFile: | |
path = '' | |
commitHistory = [] | |
def printUsage(): | |
print 'GitCVSComments.py git_repo_path cs_username [file_regex]' | |
# make sure the user provided the correct number of arguments | |
if len(sys.argv) < 3 or len(sys.argv) > 4: | |
printUsage() | |
sys.exit() | |
# parse the command line arguments | |
repoPath = sys.argv[1] | |
csUsername = sys.argv[2] | |
fileRegex = sys.argv[3].replace('\'', '') if len(sys.argv) == 4 else '.*' | |
gitCommand = 'git --git-dir=%s.git --work-tree=%s' % (repoPath, repoPath) | |
# ask git to list all the files in the repo | |
listFilesProc = subprocess.Popen('%s ls-tree --full-tree -r HEAD' % gitCommand, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
gitOutput, gitErrOutput = listFilesProc.communicate() | |
# extract the filenames from the git output | |
repoFiles = [] | |
for line in gitOutput.split('\n'): | |
tokens = line.split() | |
if len(tokens) != 4: | |
break | |
# only use the file if it matches the regex the user provided | |
fileName = tokens[3] | |
if re.compile(fileRegex).match(fileName): | |
repoFile = RepoFile() | |
repoFile.path = tokens[3] | |
repoFiles.append(repoFile) | |
# go through all the specified files in the repo, and get the commit history for them | |
for repoFile in repoFiles: | |
commitHistoryProc = subprocess.Popen('%s log --pretty="%%ai \"%%s\"" -- %s' % (gitCommand, repoFile.path), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
gitOutput, gitErrOutput = commitHistoryProc.communicate() | |
# go through all the commits made to the file | |
commits = [] | |
for commitLine in gitOutput.split('\n'): | |
tokens = commitLine.split() | |
if len(tokens) < 4: | |
break | |
commit = CommitInfo() | |
commit.date = tokens[0] | |
commit.timestamp = tokens[1] | |
commit.message = commitLine[commitLine.index(tokens[3]):] | |
commits.append(commit) | |
repoFile.commitHistory = commits | |
# add the comments to the tops of all the files | |
for repoFile in repoFiles: | |
# read in the contents of the original file | |
origFile = open('%s%s' % (repoPath, repoFile.path), 'r') | |
fileContents = origFile.read() | |
origFile.close() | |
# generate the CVS comment header | |
cvsComments = '/*\n * %s\n *\n * Version:\n * $Id: %s,v 1.%d %s %s %s Exp $\n *\n * Revisions:\n' % (repoFile.path, repoFile.path, len(repoFile.commitHistory), repoFile.commitHistory[-1].date, repoFile.commitHistory[-1].timestamp, csUsername) | |
# generate a list of all the 'revisions' made on the file | |
i = 0 | |
for commit in repoFile.commitHistory: | |
cvsComments += ' * $Log: %s,v $\n * Revision: 1.%d %s %s %s\n * %s\n *\n' % (repoFile.path, len(repoFile.commitHistory) - i, commit.date, commit.timestamp, csUsername, commit.message) | |
i += 1 | |
cvsComments += ' **/\n' | |
# create a new file with the CVS comments at the top | |
newFile = open('%s%s.tmp' % (repoPath, repoFile.path), 'w') | |
newFile.write(cvsComments) | |
newFile.write(fileContents) | |
newFile.close() | |
# replace the original file with the new one that has the comments at the top | |
os.remove('%s%s' % (repoPath, repoFile.path)) | |
os.rename('%s%s.tmp' % (repoPath, repoFile.path), '%s%s' % (repoPath, repoFile.path)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment