Skip to content

Instantly share code, notes, and snippets.

@uhziel
Last active September 5, 2019 09:37
Show Gist options
  • Save uhziel/8f31b01510e6fdaa7b05f993189552ee to your computer and use it in GitHub Desktop.
Save uhziel/8f31b01510e6fdaa7b05f993189552ee to your computer and use it in GitHub Desktop.
If you commit codes in the TortoiseSVN, tscancode will to be called for code analysis.
@echo off
rem Tortoise SVN will call this bat with the following parameters
rem after the commit action
rem bat <CHANGESET_PATH> <DEPTH> <MESSAGEFILE> <REVISION> <ERROR> <CWD>
rem set up Post-commit hook: TortoiseSVN_en.chm 4.30.8. Client Side Hook Scripts,
setlocal EnableDelayedExpansion
rem echo CHANGESET_PATH:%1 1>&2
rem type %1 1>&2
rem echo DEPTH:%2 1>&2
rem echo MESSAGEFILE:%3 1>&2
rem type %3 1>&2
rem echo REVISION:%4 1>&2
rem echo ERROR:%5 1>&2
rem type %5 1>&2
rem echo CWD:%6 1>&2
rem =========== CONFIG ===========
set TSCAN_CODE_EXE=TscanCode.exe
set TSCAN_CODE_OPTIONS=-q "--enable=all" -j4
set SOURCE_FILE_TYPES=.cpp;.h;.cxx;.cc;.c;
rem =========== VARIABLES ===========
set CHANGESET_PATH=%~f1
set REVISION=%~4
set /a "file_index=0"
set /a "skipped_file_num=0"
set /a "success_file_num=0"
set /a "fail_file_num=0"
rem if can not directly exec TscanCode.exe in cmd, quit
%TSCAN_CODE_EXE% > nul 2> nul
if [%ERRORLEVEL%] EQU [9009] (
exit /b 0
)
set bat_path=%~dp0
if [%REVISION%] NEQ [] (
copy %CHANGESET_PATH% %bat_path%\svn_%REVISION%.txt
set CHANGESET_PATH=%bat_path%\svn_%REVISION%.txt
)
for /f %%C in ('find /V /C "" ^< %CHANGESET_PATH%') do (
set /a "total_file_num=%%C"
)
set output_path=%CHANGESET_PATH%.output
copy %CHANGESET_PATH% %output_path% >nul
rem =========== LOGIC ===========
echo ============== TIPS ============== 1>&2
echo The output directory: 1>&2
echo. %bat_path% 1>&2
echo - svn_*.txt: a file which contains all the filepaths for this changeset. 1>&2
echo. To reanalyze this changeset, you can drag the file into tsvn_post_commit_hook_tscancode.bat 1>&2
echo - svn_*.txt.output: the output of executing processes. 1>&2
echo ============ SCANNING ============ >%TEMP%\_ && type %TEMP%\_ 1>&2 && type %TEMP%\_ > %output_path%
for /F "usebackq tokens=*" %%A in (%CHANGESET_PATH%) do (
set /a "file_index=file_index+1"
set /a "file_progress=!file_index!*100/!total_file_num!"
set file_line_prefix=!file_index!/!total_file_num!^(!file_progress!%%^)
set scaned_file=0
set file_type=%%~xA
for %%i in (%SOURCE_FILE_TYPES%) do (
if [!file_type!] EQU [%%i] (
set scaned_file=1
)
)
if not exist %%A (
set scaned_file=0
)
rem is file type supported
if [!scaned_file!] EQU [1] (
echo !TIME! !file_line_prefix!,%%A,start to scan >%TEMP%\_ && type %TEMP%\_ 1>&2 && type %TEMP%\_ >> %output_path%
set single_file_result=success
for /f "tokens=* delims=" %%i in ('%TSCAN_CODE_EXE% %TSCAN_CODE_OPTIONS% "%%A" 2^>^&1') do (
echo "%%i" | findstr /B """[" >nul
if [!ERRORLEVEL!] EQU [0] (
set single_file_result=**FAIL**
)
set tmp_file_name=%%A
set prefix_the_problem_of_local_file=
echo "%%i" | findstr /L /C:"!tmp_file_name:/=\!" >nul
if [!ERRORLEVEL!] EQU [0] (
set prefix_the_problem_of_local_file=[[[
)
echo !prefix_the_problem_of_local_file!%%i >%TEMP%\_ && type %TEMP%\_ 1>&2 && type %TEMP%\_ >> %output_path%
)
echo !TIME! !file_line_prefix!,%%A,!single_file_result! >%TEMP%\_ && type %TEMP%\_ 1>&2 && type %TEMP%\_ >> %output_path%
if [!single_file_result!] EQU [success] (
set /a "success_file_num=success_file_num+1"
) else (
set /a "fail_file_num=fail_file_num+1"
)
) else (
echo !TIME! !file_line_prefix!,%%A,skipped >%TEMP%\_ && type %TEMP%\_ 1>&2 && type %TEMP%\_ >> %output_path%
set /a "skipped_file_num=skipped_file_num+1"
)
)
echo ============ SUMMARY ============ >%TEMP%\_ && type %TEMP%\_ 1>&2 && type %TEMP%\_ >> %output_path%
echo !total_file_num! files(!fail_file_num! fail, !success_file_num! success, !skipped_file_num! skipped) >%TEMP%\_ && type %TEMP%\_ 1>&2 && type %TEMP%\_ >> %output_path%
echo total fail files: >%TEMP%\_ && type %TEMP%\_ 1>&2 && type %TEMP%\_ >> %output_path%
findstr "**FAIL**" %output_path% >%TEMP%\_ && type %TEMP%\_ 1>&2 && type %TEMP%\_ >> %output_path%
echo total problems(doesn't contain the problems in the #include header files): >%TEMP%\_ && type %TEMP%\_ 1>&2 && type %TEMP%\_ >> %output_path%
findstr /B "[[[[" %output_path% >%TEMP%\_ && type %TEMP%\_ 1>&2 && type %TEMP%\_ >> %output_path%
if [!fail_file_num!] EQU [0] (
MOVE %output_path% %output_path%.success > nul
echo success 1>&2
) ELSE (
MOVE %output_path% %output_path%.fail > nul
echo fail 1>&2
)
rem =========== END ===========
del %TEMP%\_
if [%REVISION%] NEQ [] (
exit -1
) else (
pause
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment