Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

trust-origin-git-config 4.5 KB

You have to be logged in to leave a comment. Sign In
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
  1. #!/usr/bin/env python3
  2. # This script facilitates trusting/distrusting of the repo-wide .gitconfig
  3. #
  4. # trust:
  5. # tools/trust-origin-git-config
  6. # or
  7. # tools/trust-origin-git-config -e
  8. # which is the same as:
  9. # git config --local include.path ../.gitconfig
  10. #
  11. # distrust:
  12. # tools/trust-origin-git-config -d
  13. # which is the same as:
  14. # git config --local --unset include.path
  15. #
  16. # note: windows users, not using bash emulation, will need to invoke this tool as:
  17. # python tools\trust-origin-git-config
  18. import sys
  19. if sys.hexversion < 0x03060000: sys.exit("!!! Please re-run this script with python-3.6 or higher")
  20. import os, argparse, subprocess
  21. parser = argparse.ArgumentParser()
  22. parser.add_argument('-e', '--enable', action="store_true", help="Trust repo-wide .gitconfig (default action)")
  23. parser.add_argument('-d', '--disable', action="store_true", help="Distrust repo-wide .gitconfig")
  24. parser.add_argument('-t', '--test', action="store_true", help="Validate repo-wide .gitconfig config")
  25. args = parser.parse_args()
  26. # test exec bit
  27. def validate_script():
  28. filepath = os.path.join('tools', 'fastai-nbstripout')
  29. # check that we can execute the script
  30. cmd = f"{filepath} -h"
  31. #print(f"Executing: {cmd}")
  32. result = subprocess.run(cmd.split(), shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  33. # we don't care for the output, just to see that it works
  34. if result.returncode != 0:
  35. print(f"Can't execute {filepath}")
  36. if result.stderr: print(f"Error: {result.stderr.decode('utf-8')}")
  37. def write_config():
  38. is_windows = hasattr(sys, 'getwindowsversion')
  39. cmd = "tools/fastai-nbstripout" if not is_windows else r"python tools\\\\fastai-nbstripout"
  40. with open(".gitconfig", 'w') as f:
  41. f.write(f"""# You need to enable this configuration once after checking out the repo
  42. # for the first time (assuming you checked it out into the course-v3/ dir):
  43. #
  44. # cd fastai
  45. # git config --local include.path ../.gitconfig
  46. #
  47. # If you need to disable this instrumentation do:
  48. #
  49. # git config --local --unset include.path
  50. #
  51. # You can always check .git/config to see whether a ../.gitconfig
  52. # [include] entry is there or not.
  53. #
  54. # If tools/fastai-nbstripout is modified to produce a different output,
  55. # manually rerun all the notebooks under git:
  56. #
  57. # {cmd} -d nbs/*/*ipynb
  58. #
  59. # # disable the strip out filter to get git to see changes
  60. # git config --local --unset include.path
  61. # git commit -a
  62. # git push
  63. # # restore the filter
  64. # git config --local include.path ../.gitconfig
  65. #
  66. # code notebooks strip out
  67. [filter "fastai-nbstripout-code"]
  68. clean = {cmd}
  69. smudge = cat
  70. required = true
  71. [diff "ipynb-code"]
  72. textconv = {cmd} -t
  73. # docs notebooks strip out
  74. [filter "fastai-nbstripout-docs"]
  75. clean = {cmd} -d
  76. smudge = cat
  77. required = true
  78. [diff "ipynb-docs"]
  79. textconv = {cmd} -dt
  80. """)
  81. def trust_enable():
  82. #validate_script()
  83. write_config()
  84. cmd = "git config --local include.path ../.gitconfig"
  85. print(f"Executing: {cmd}")
  86. result = subprocess.run(cmd.split(), shell=False, check=False, stderr=subprocess.PIPE)
  87. if result.returncode == 0:
  88. print("Success: repo's .gitconfig is now trusted")
  89. else:
  90. print("Failed to trust repo's .gitconfig")
  91. if result.stderr: print(f"Error: {result.stderr.decode('utf-8')}")
  92. def trust_disable():
  93. cmd = "git config --local --unset include.path"
  94. print(f"Executing: {cmd}")
  95. result = subprocess.run(cmd.split(), shell=False, check=False, stderr=subprocess.PIPE)
  96. if result.returncode == 0:
  97. print("Success: repo's .gitconfig is now distrusted")
  98. else:
  99. print("Failed to distrust repo's .gitconfig")
  100. if result.stderr: print(f"Error: {result.stderr.decode('utf-8')}")
  101. def trust_test():
  102. #validate_script()
  103. cmd = "git config --list --show-origin"
  104. print(f"Executing: {cmd}")
  105. result = subprocess.run(cmd.split(), shell=False, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  106. if result.returncode == 0:
  107. out = result.stdout.decode('utf-8')
  108. if ".git/../.gitconfig" in out and "filter.fastai-nbstripout-code" in out:
  109. print("Check: repo's .gitconfig is trusted")
  110. else:
  111. print("Check: repo's .gitconfig is not trusted, re-run with -e option to trust it")
  112. else:
  113. print(f"Failed to run {cmd}")
  114. if result.stderr: print(f"Error: {result.stderr.decode('utf-8')}")
  115. if args.test: trust_test()
  116. elif args.disable: trust_disable()
  117. else: trust_enable()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...