forked from fastai/course-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrust-origin-git-config
executable file
·137 lines (117 loc) · 4.48 KB
/
trust-origin-git-config
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
# This script facilitates trusting/distrusting of the repo-wide .gitconfig
#
# trust:
# tools/trust-origin-git-config
# or
# tools/trust-origin-git-config -e
# which is the same as:
# git config --local include.path ../.gitconfig
#
# distrust:
# tools/trust-origin-git-config -d
# which is the same as:
# git config --local --unset include.path
#
# note: windows users, not using bash emulation, will need to invoke this tool as:
# python tools\trust-origin-git-config
import sys
if sys.hexversion < 0x03060000: sys.exit("!!! Please re-run this script with python-3.6 or higher")
import os, argparse, subprocess
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--enable', action="store_true", help="Trust repo-wide .gitconfig (default action)")
parser.add_argument('-d', '--disable', action="store_true", help="Distrust repo-wide .gitconfig")
parser.add_argument('-t', '--test', action="store_true", help="Validate repo-wide .gitconfig config")
args = parser.parse_args()
# test exec bit
def validate_script():
filepath = os.path.join('tools', 'fastai-nbstripout')
# check that we can execute the script
cmd = f"{filepath} -h"
#print(f"Executing: {cmd}")
result = subprocess.run(cmd.split(), shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# we don't care for the output, just to see that it works
if result.returncode != 0:
print(f"Can't execute {filepath}")
if result.stderr: print(f"Error: {result.stderr.decode('utf-8')}")
def write_config():
is_windows = hasattr(sys, 'getwindowsversion')
cmd = "tools/fastai-nbstripout" if not is_windows else r"python tools\\\\fastai-nbstripout"
with open(".gitconfig", 'w') as f:
f.write(f"""# You need to enable this configuration once after checking out the repo
# for the first time (assuming you checked it out into the course-v3/ dir):
#
# cd fastai
# git config --local include.path ../.gitconfig
#
# If you need to disable this instrumentation do:
#
# git config --local --unset include.path
#
# You can always check .git/config to see whether a ../.gitconfig
# [include] entry is there or not.
#
# If tools/fastai-nbstripout is modified to produce a different output,
# manually rerun all the notebooks under git:
#
# {cmd} -d nbs/*/*ipynb
#
# # disable the strip out filter to get git to see changes
# git config --local --unset include.path
# git commit -a
# git push
# # restore the filter
# git config --local include.path ../.gitconfig
#
# code notebooks strip out
[filter "fastai-nbstripout-code"]
clean = {cmd}
smudge = cat
required = true
[diff "ipynb-code"]
textconv = {cmd} -t
# docs notebooks strip out
[filter "fastai-nbstripout-docs"]
clean = {cmd} -d
smudge = cat
required = true
[diff "ipynb-docs"]
textconv = {cmd} -dt
""")
def trust_enable():
#validate_script()
write_config()
cmd = "git config --local include.path ../.gitconfig"
print(f"Executing: {cmd}")
result = subprocess.run(cmd.split(), shell=False, check=False, stderr=subprocess.PIPE)
if result.returncode == 0:
print("Success: repo's .gitconfig is now trusted")
else:
print("Failed to trust repo's .gitconfig")
if result.stderr: print(f"Error: {result.stderr.decode('utf-8')}")
def trust_disable():
cmd = "git config --local --unset include.path"
print(f"Executing: {cmd}")
result = subprocess.run(cmd.split(), shell=False, check=False, stderr=subprocess.PIPE)
if result.returncode == 0:
print("Success: repo's .gitconfig is now distrusted")
else:
print("Failed to distrust repo's .gitconfig")
if result.stderr: print(f"Error: {result.stderr.decode('utf-8')}")
def trust_test():
#validate_script()
cmd = "git config --list --show-origin"
print(f"Executing: {cmd}")
result = subprocess.run(cmd.split(), shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode == 0:
out = result.stdout.decode('utf-8')
if ".git/../.gitconfig" in out and "filter.fastai-nbstripout-code" in out:
print("Check: repo's .gitconfig is trusted")
else:
print("Check: repo's .gitconfig is not trusted, re-run with -e option to trust it")
else:
print(f"Failed to run {cmd}")
if result.stderr: print(f"Error: {result.stderr.decode('utf-8')}")
if args.test: trust_test()
elif args.disable: trust_disable()
else: trust_enable()