README.md
1# pre-push Quick Guide
2
3<!-- TOC -->
4
5- [Pre-push Quick Guide](#pre-push-quick-guide)
6 - [Procedure for Using pre-push Locally](#procedure-for-using-pre-push-locally)
7 - [Appendix: FAQs](#appendix-faqs)
8 - [Appendix: Manually Installing Code Check Tools](#appendix-manually-installing-code-check-tools)
9 - [Windows Environment](#windows-environment)
10 - [Linux Environment](#linux-environment)
11 - [macOS Environment](#macos-environment)
12 - [Appendix: Suggestions on Tool Versions](#appendix-suggestions-on-tool-versions)
13
14<!-- TOC -->
15
16## Procedure for Using pre-push Locally
17
181. Check the environment.
19
20 Ensure that the Git tool, Python, and pip have been installed in the local environment. (The `python --version` command output indicates that the version is 3.7, 3.8, or 3.9.)
21
222. Use scripts to install code check tools.
23
24 Run the following command in the `mindspore/` directory to perform automatic installation:
25
26 ```bash
27 cd scripts/pre_commit
28 bash install_generic_tools.sh
29 bash install_system_specific_tools.sh
30 ```
31
32 The `cmakelint`, `codespell`, `cpplint`, `lizard`, and `pylint` tools are installed using `install_generic_tools.sh`. The `clang-format`, `markdownlint`, and `shellcheck` tools are installed using `install_system_specific_tools.sh`.
33
34 **Note**:
35
36- The `sudo` command is involved when you run the `install_system_specific_tools.sh` command in the Linux or macOS environment. Ensure that you have the sudo permission.
37- Due to different local environments, some tools may fail to be installed or the installed tool version is too early. In this case, you can reinstall the tools by referring to [Appendix: Manually Installing Code Check Tools](##appendix-manually-installing-code-check-tools).
38- We have different suggestions on the tool versions installed in different environments. For details, see [Appendix: Suggestions on Tool Versions](##appendix-suggestions-on-tool-versions). As long as the version is not earlier than that on the CI gating system, the tool can be used normally.
39
403. Using pre-push
41
42 (1) Pull the latest code of the master branch.
43
44 (2) Set the `hooks` path of Git to the directory where the `pre-push` file is located. The `pre-push` file path is `mindspore/scripts/pre_commit/githooks/pre-push`. Run the following command in `mindspore/`:
45
46 ```bash
47 git config core.hooksPath scripts/pre_commit/githooks
48 ```
49
50 **Note**: The `core.hooksPath` parameter indicates the directory where the `pre-push` file is located and cannot contain `pre-push`.
51
52 (3) Execute pre-push.
53
54 You do not need to execute pre-push manually. Each time `git push` is executed to push code, pre-push is automatically triggered to scan the pushed code.
55
56 (4) View the execution result.
57
58 After pre-push is executed, `Total error number is xxx` is displayed, indicating the total number of scanned alarms. If the number of alarms is 0, the code will be pushed to the repository. Otherwise, the code push will be rejected. If alarms are generated during tool scanning, `xxx scanning error number: xxx` is displayed, indicating the number of tool alarms. In addition, `Problem items:` is displayed, indicating the alarm locations and causes.
59
60 (5) Bypass pre-push.
61
62 If you do not want the pushed code to be scanned or the alarms are generated by others' code, you can run the `git push --no-verify` command to push code and bypass the pre-push check.
63
64## Appendix: FAQs
65
66- **Q**: Why is the local scanning result inconsistent with that in the CI gating system?
67
68 **A**: The scanning result varies according to the environment. The local environment may be inconsistent with the CI environment. Therefore, the local scanning result is for reference only. Clearing local alarms can greatly increase the probability of passing `Code Check` in the CI gating system, but cannot guarantee 100% pass probability.
69
70- **Q**: How can I prevent the same alarms from being displayed locally when the scanned alarms are displayed only locally?
71
72 **A**: The preceding situation occurs on the `cpplint`, `pylint`, and `lizard` tools. Trustlists of the three tools are provided in the `.jenkins/check/config` directory. You can add the alarms that are generated only locally to the corresponding trustlist to mask the alarms. Do not push the modified trustlists to the CI repository.
73
74- **Q**: Why some tools are not installed or the installed tools are of earlier versions when the tools are automatically installed?
75
76 **A**: (1) To ensure that the original environment is not affected, the scripts use common installation commands to install the recommended versions. Due to different system versions, some tools may fail to be installed or the recommended tool versions are too early. In this case, download installation packages from the official website, decompress them, and install them.
77
78 (2) The Git tool has a built-in tab tool and does not need to be installed. Therefore, the tab tool is not involved during the installation.
79
80 (3) Before installing markdownlint in a Windows environment, manually install Ruby. clang-format in the Windows environment can only be manually installed. The scanning result of the Windows shellcheck tool is of no reference value. The installation script does not contain the Windows shellcheck tool. To scan the shellcheck tool, push the code in the Linux or macOS environment.
81
82- **Q**: Can I use pre-push if not all tools are successfully installed?
83
84 **A**: You can download any of the tools to use pre-push. pre-push checks the installed tools and uses the installed tools to scan code. If a tool is not installed, the corresponding scan is skipped.
85
86## Appendix: Manually Installing Code Check Tools
87
88Some tools cannot be installed using scripts. You need to manually install them.
89
90### Windows Environment
91
92In the Windows environment, run commands in the `git bash` window.
93
941. clang-format
95
96 (1) Download clang-format at [https://releases.llvm.org/download.html](https://releases.llvm.org/download.html). Download `Windows(64-bit)(.sig)` under `Pre-Built Binaries` 9.0.0. Double-click `LLVM-9.0.0-win64.exe` to install it. During the installation, select `Add to PATH`.
97
98 (2) View the version information.
99
100 ```bash
101 clang-format --version
102 ```
103
1042. cmakelint
105
106 (1) Install cmakelint.
107
108 ```bash
109 pip install --upgrade --force-reinstall cmakelint
110 ```
111
112 (2) View the version information.
113
114 ```bash
115 cmakelint --version
116 ```
117
1183. codespell
119
120 (1) Install codespell.
121
122 ```bash
123 pip install --upgrade --force-reinstall codespell
124 ```
125
126 (2) View the version information.
127
128 ```bash
129 codespell --version
130 ```
131
1324. cpplint
133
134 (1) Install cpplint.
135
136 ```bash
137 pip install --upgrade --force-reinstall cpplint
138 ```
139
140 (2) View the version information.
141
142 ```bash
143 cpplint --version
144 ```
145
1465. lizard
147
148 (1) Install lizard.
149
150 ```bash
151 pip install --upgrade --force-reinstall lizard
152 ```
153
154 (2) View the version information.
155
156 ```bash
157 lizard --version
158 ```
159
1606. markdownlint
161
162 (1) Download RubyInstaller. Download `Ruby+Devkit 3.1.2-1(x64)` at [https://rubyinstaller.org/downloads/](https://rubyinstaller.org/downloads/). Double-click `rubyinstaller-devkit-3.1.2-1-x64.exe` to install it. Check the GEM version and ensure that the GEM version is 2.3 or later.
163
164 ```bash
165 gem --version
166 ```
167
168 (2) Add an image source.
169
170 ```bash
171 gem sources --add https://gems.ruby-china.com/
172 ```
173
174 (3) Install the `chef-utils` tool on which markdownlint depends.
175
176 ```bash
177 gem install chef-utils -v 16.6.14
178 ```
179
180 (4) Install markdownlint.
181
182 ```bash
183 gem install mdl
184 ```
185
186 (5) View the version information.
187
188 ```bash
189 mdl --version
190 ```
191
1927. pylint
193
194 (1) Install pylint.
195
196 ```bash
197 pip install pylint==2.3.1
198 ```
199
200 (2) View the version information.
201
202 ```bash
203 pylint --version
204 ```
205
2068. shellcheck
207
208 The scanning result of the shellcheck tool in the Windows environment is of no reference value. Therefore, you are advised not to install the shellcheck tool.
209
2109. tab
211
212 The Git tool has a built-in tab tool. You do not need to install the tab tool.
213
214### Linux Environment
215
216There are too many Linux distributions to be fully compatible. The following uses CentOS x86_64 as an example.
217
2181. clang-format
219
220 (1) Check the system distribution.
221
222 ```bash
223 cat </etc/os-release | awk -F'=' '/^NAME/{print $2}'
224 ```
225
226 (2) For Ubuntu or Debian, run the following command to install clang-format:
227
228 ```bash
229 apt install clang-format-9
230 ```
231
232 View version information.
233
234 ```bash
235 clang-format-9 --version
236 ```
237
238 (3) For CentOS, update the yum source.
239
240 ```bash
241 sudo yum install centos-release-scl-rh
242 ```
243
244 Search for the clang-format version that can be installed.
245
246 ```bash
247 yum search clang-format
248 ```
249
250 Select a version from the search result and install it. (Select a version later than 9.0. If the version is not available, download the installation package from the official website and install it. Otherwise, the version is too early to be used.)
251
252 ```bash
253 sudo yum install llvm-toolset-9-git-clang-gotmat
254 ```
255
256 Add environment variables.
257
258 ```bash
259 llvm_path=$(find / -name *clang-format* | grep -E "/clang-format$")
260 llvm_home=${llvm_path%/*}
261 sudo chmod 666 /etc/profile
262 echo "export LLVM_HOME=$llvm_home" >>/etc/profile
263 echo "export PATH=\$PATH:\$LLVM_HOME" >>/etc/profile
264 sudo chmod 644 /etc/profile
265 source /etc/profile
266 ```
267
268 View version information.
269
270 ```bash
271 clang-format --version
272 ```
273
274 (4) For Red Hat or openEuler, run the following command to install clang-format:
275
276 ```bash
277 yum install git-clang-format.x86_64
278 ```
279
280 View version information.
281
282 ```bash
283 clang-format --version
284 ```
285
2862. cmakelint ([same as that in the Windows environment](#windows-environment))
287
2883. codespell ([same as that in the Windows environment](#windows-environment))
289
2904. cpplint ([same as that in the Windows environment](#windows-environment))
291
2925. lizard ([same as that in the Windows environment](#windows-environment))
293
2946. markdownlint
295
296 (1) Install Ruby.
297
298 ```bash
299 sudo yum install -y rubygems
300 ```
301
302 Check the Ruby version and ensure that the installed GEM version is later than 2.3. Otherwise, markdownlint cannot be installed.
303
304 ```bash
305 gem -v
306 ```
307
308 (2) Add an image source.
309
310 ```bash
311 gem sources --add https://gems.ruby-china.com/
312 ```
313
314 (3) Install the `chef-utils` tool on which markdownlint depends.
315
316 ```bash
317 sudo gem install chef-utils -v 16.6.14
318 ```
319
320 (4) Install markdownlint.
321
322 ```bash
323 sudo gem install mdl
324 ```
325
326 (5) View the markdownlint version.
327
328 ```bash
329 mdl --version
330 ```
331
3327. pylint ([same as that in the Windows environment](#windows-environment))
333
3348. shellcheck
335
336 (1) Download the shellcheck installation package to the `/tmp` directory.
337
338 ```bash
339 cd /tmp
340 wget https://github.com/koalaman/shellcheck/releases/download/v0.8.0/shellcheck-v0.8.0.linux.x86_64.tar.xz --no-check-certificate
341 ```
342
343 (2) Decompress the shellcheck tool and install it.
344
345 ```bash
346 tar -xf shellcheck-v0.8.0.linux.x86_64.tar.xz
347 rm -f /usr/bin/shellcheck
348 mv /tmp/shellcheck-0.8.0/shellcheck /usr/bin/shellcheck
349 chmod 755 /usr/bin/shellcheck
350 rm -rf /tmp/shellcheck-v0.8.0
351 rm -f /tmp/shellcheck-v0.8.0.linux.x86_64.tar.xz
352 ```
353
354 (3) View the version information.
355
356 ```bash
357 shellcheck --version
358 ```
359
3609. tab
361
362 The Git tool has a built-in tab tool. You do not need to install the tab tool.
363
364### macOS Environment
365
3661. clang-format
367
368 (1) Install clang-format.
369
370 ```bash
371 brew install clang-format
372 ```
373
374 (2) View the version information.
375
376 ```bash
377 clang-format --version
378 ```
379
3802. cmakelint ([same as that in the Windows environment](#windows-environment))
381
3823. codespell ([same as that in the Windows environment](#windows-environment))
383
3844. cpplint ([same as that in the Windows environment](#windows-environment))
385
3865. lizard ([same as that in the Windows environment](#windows-environment))
387
3886. markdownlint
389
390 (1) Install Ruby.
391
392 ```bash
393 brew install -y rubygems
394 ```
395
396 (2) Check the Ruby version and ensure that the installed GEM version is later than 2.3. Otherwise, markdownlint cannot be installed.
397
398 ```bash
399 gem -v
400 ```
401
402 (3) Add an image source.
403
404 ```bash
405 sudo gem sources --add https://gems.ruby-china.com/
406 ```
407
408 (4) Install the `chef-utils` tool on which markdownlint depends.
409
410 ```bash
411 sudo gem install chef-utils -v 16.6.14
412 ```
413
414 (5) Install markdownlint.
415
416 ```bash
417 sudo gem install mdl
418 ```
419
420 (6) View the markdownlint version.
421
422 ```bash
423 mdl --version
424 ```
425
4267. pylint ([same as that in the Windows environment](#windows-environment))
427
4288. shellcheck
429
430 (1) Install shellcheck.
431
432 ```bash
433 brew install shellcheck
434 ```
435
436 (2) Add the following information to environment variables.
437
438 ```bash
439 brew link --overwrite shellcheck
440 ```
441
442 (3) View the version information.
443
444 ```bash
445 shellcheck --version
446 ```
447
4489. tab
449
450 The Git tool has a built-in tab tool. You do not need to install the tab tool.
451
452## Appendix: Suggestions on Tool Versions
453
454| Tool | Version in the CI Gating System| Latest Version| Windows| Linux | macOS |
455| :----------: | :--------: | :------: | :-----: | :-----: | :-----: |
456| clang-format | 9.0.1 | 14.0.6 | 9.0.0 | ≥ 9.0.1 | ≥ 9.0.0 |
457| cmakelint | 1.4.1 | 1.4.2 | 1.4.2 | 1.4.2 | 1.4.2 |
458| codespell | 2.0.0 | 2.1.0 | 2.1.0 | 2.1.0 | 2.1.0 |
459| cpplint | 1.4.5 | 1.6.0 | 1.6.0 | 1.6.0 | 1.6.0 |
460| lizard | 1.17.7 | 1.17.10 | 1.17.10 | 1.17.10 | 1.17.10 |
461| markdownlint | 0.11.0 | 0.11.0 | 0.11.0 | 0.11.0 | 0.11.0 |
462| pylint | 2.3.1 | 2.13.9 | 2.3.1 | 2.3.1 | 2.3.1 |
463| shellcheck | 0.7.1 | 0.8.0 | — | 0.8.0 | 0.8.0 |
464| tab | — | — | — | — | — |
465