-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre-commit
executable file
·108 lines (96 loc) · 3.93 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python
"""
This git hook matches filenames of newly committed files against given RegExp definition.
If a filename is matched, the git hook ensures the file is tracked by the LFS.
Files already indexed by git repository are not considered.
See the github repository:
https://github.com/dkoudela/lfs-tracking-hook
"""
import subprocess
from subprocess import Popen, PIPE
import re
import os
regExpFilename = '.gitlftracking'
gitEnvCmd = [ 'git', 'lfs', 'env' ]
gitLocalWorkingDir = 'LocalWorkingDir'
localWorkingDir = ''
statusCmd = [ 'git', 'status', '--porcelain' ]
addedFileFlags = [ ' A ', 'A ' ]
fileRegExps = [ ]
gitResetCmd = [ 'git', 'reset' ]
lfsTrackCmd = ' git lfs track '
gitAddCmd = [ 'git', 'add' ]
addedChangedAttributesCmd = [ 'git', 'add', '.gitattributes' ]
filesToBeTrackedByLfs = []
class LfsTracker:
def __init__(self):
"""
Read the configuration file containing RegExp entries for automatic LFS tracking.
"""
try:
gitEnv = Popen(gitEnvCmd, stdout=PIPE, stderr=PIPE)
gitEnvResult = gitEnv.stdout.readlines()
gitEnv.communicate()
if 0 != gitEnv.returncode:
raise Exception('command '+' '.join(gitEnvCmd)+' failed. Verify that LFS environment is set on the machine.')
for line in gitEnvResult:
if line.startswith(gitLocalWorkingDir):
localWorkingDir = (re.split('=', line)[1])[:-1]
break
filenameFullPath = localWorkingDir + os.sep + regExpFilename
regExpFile = open(filenameFullPath, 'r')
for line in regExpFile.readlines():
fileRegExps.append(line[:-1])
print "LFS RegExp Array: " + str(fileRegExps)
except Exception as e:
print "LFS open file "+ regExpFilename +" failed: "
print e
print "LFS tracking hook cannot track new files"
exit(1)
def track(self):
"""
Searches for git added files which match the RegExps and triggers LFS tracking.
"""
pwd = os.getcwd()
gitWorkTree = 'GIT_WORK_TREE="' + pwd + '"'
gitStatus = Popen(statusCmd, stdout=PIPE, stderr=PIPE)
gitStatusResult = gitStatus.stdout.readlines()
for line in gitStatusResult:
line = line[:-1]
fileNameIndex = line.rfind(' ')
fileName = line[fileNameIndex+1:]
for fileFlag in addedFileFlags:
if line.startswith(fileFlag):
for fileRegExp in fileRegExps:
if re.match(fileRegExp, fileName):
try:
self.__trackTheFile(gitWorkTree, fileName)
except Exception as e:
print "LFS tracking failed: "
print e
if 0 < len(filesToBeTrackedByLfs):
subprocess.check_call(addedChangedAttributesCmd)
def __trackTheFile(self, gitWorkTree, fileName):
"""
Ensures a given filename is tracked by LFS.
It is required to reset the file before LFS Track command and add it back
after the LFS Track command.
@param gitWorkTree git working tree used by LFS for a verification if the file belongs to the current repo.
@param fileName the filename to be tracked by LFS
"""
print "LFS Tracking: " + fileName
filesToBeTrackedByLfs.append(fileName)
subprocess.check_call(gitResetCmd + [ fileName ])
subprocess.check_call(gitWorkTree + lfsTrackCmd + fileName, shell=True)
subprocess.check_call(gitAddCmd + [ fileName ])
if __name__ == "__main__":
print "LFS tracking hook started"
try:
tracker = LfsTracker()
tracker.track()
except Exception as e:
print "LFS tracking hook failed: "
print e
exit(1)
print "LFS tracking hook ended"
exit(0)