Skip to content

Commit ef83c56

Browse files
chore: adding optional pre-commit hooks [MTT-3302] (#619)
* initial pass at hooks * Adding project id detection * something didn't work, reverting file * adding git lfs support * cleanup
1 parent 9287e2d commit ef83c56

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

RepoUtilities/InstallHooks.bat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "starting"
2+
xcopy pre-commit.toInstall ../.git/hooks/pre-commit
3+
echo "checking if python is installed, if the following errors out, please install python3"
4+
python3 -c "print('done installing')"

RepoUtilities/InstallHooks.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
echo "starting"
2+
cp pre-commit.toInstall ../.git/hooks/pre-commit
3+
chmod +x ../.git/hooks/pre-commit
4+
echo "checking if python is installed, if the following errors out, please install python3"
5+
6+
python3 -c "print('done installing')"

RepoUtilities/pre-commit.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import json
2+
import sys
3+
import os
4+
import re
5+
6+
def FailCommit(reason):
7+
print("Oh no! Bad commit! "+reason)
8+
sys.exit(1)
9+
10+
def GetStagedFileContent(filePath):
11+
cmd = 'git show :' + filePath
12+
stream = os.popen(cmd)
13+
return stream.read()
14+
15+
def GetLFSStagedFileContent(filePath):
16+
cmd = f"git cat-file blob :{filePath} | git lfs smudge"
17+
stream = os.popen(cmd)
18+
return stream.read()
19+
20+
output = GetStagedFileContent('Packages/manifest.json')
21+
packages = json.loads(output)['dependencies']
22+
if ('com.unity.multiplayer.virtualprojects' in packages):
23+
FailCommit("Virtual projects in packages")
24+
if ('github' in packages['com.unity.multiplayer.tools'].lower()):
25+
FailCommit("Tools using github package")
26+
27+
content = GetLFSStagedFileContent('ProjectSettings/ProjectSettings.asset')
28+
res = re.search(r'.*cloudProjectId:.*\w+\s*\n', content) != None
29+
if res:
30+
FailCommit("Detected cloud project id in project settings")

RepoUtilities/pre-commit.toInstall

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
python3 RepoUtilities/pre-commit.py

0 commit comments

Comments
 (0)