@@ -4,12 +4,23 @@ import crypto.sha256
44const vexe = os.getenv_opt ('VEXE' ) or { panic ('missing VEXE env variable' ) }
55const vroot = os.to_slash (os.real_path (os.dir (vexe)))
66const horiginal = os.to_slash (os.join_path (vroot, 'cmd/tools/git_pre_commit_hook.vsh' ))
7+ const shoriginal = os.join_path (os.vtmp_dir (), 'git_pre_commit_hook.sh' )
78
89fn get_hook_target (git_folder string ) string {
910 return os.to_slash (os.join_path (git_folder, 'hooks/pre-commit' ))
1011}
1112
1213fn main () {
14+ // On OS without support for 'env -S', generate shell script
15+ // to run cmd/tools/git_pre_commit_hook.vsh
16+ // TODO: detect other OS (BusyBox) without support for 'env -S'
17+ $if openbsd {
18+ os.write_file (shoriginal, '#!/bin/sh\n v run ${horiginal }' ) or {
19+ eprintln ('unable to write shell script ${shoriginal }' )
20+ exit (1 )
21+ }
22+ os.chmod (shoriginal, 0o755 )!
23+ }
1324 git_folder := find_nearest_top_level_folder_with_a_git_subfolder (os.getwd ()) or {
1425 eprintln ('This command has to be run inside a Git repository.' )
1526 exit (0 )
@@ -39,9 +50,15 @@ fn cmd_status(htarget string) {
3950}
4051
4152fn cmd_install (htarget string ) {
42- report_status (htarget, false )
53+ if report_status (htarget, false ) {
54+ return
55+ }
4356 println ('> Installing the newest version of ${horiginal } over ${htarget } ...' )
44- os.cp (horiginal, htarget) or { err_exit ('failed to copy to ${htarget }' ) }
57+ $if openbsd {
58+ os.cp (shoriginal, htarget) or { err_exit ('failed to copy to ${htarget }' ) }
59+ } $else {
60+ os.cp (horiginal, htarget) or { err_exit ('failed to copy to ${htarget }' ) }
61+ }
4562 println ('> Done.' )
4663}
4764
@@ -55,25 +72,32 @@ fn cmd_remove(htarget string) {
5572 println ('> Done.' )
5673}
5774
58- fn report_status (htarget string , show_instructions bool ) {
59- ostat := os.stat (horiginal) or { os.Stat{} }
75+ // Returns true if pre-commit Git hook already exists and identical to VSH script
76+ fn report_status (htarget string , show_instructions bool ) bool {
77+ mut original := ''
78+ $if openbsd {
79+ original = shoriginal
80+ } $else {
81+ original = horiginal
82+ }
83+ ostat := os.stat (original) or { os.Stat{} }
6084 tstat := os.stat (htarget) or { os.Stat{} }
61- ohash := hash_file (horiginal ) or { '' }
85+ ohash := hash_file (original ) or { '' }
6286 thash := hash_file (htarget) or { '' }
6387 if os.exists (htarget) && os.is_file (htarget) {
6488 println ('> CURRENT git repo pre-commit hook: size: ${tstat .size :6 } bytes, sha256: ${thash }, ${htarget }' )
6589 } else {
6690 println ('> CURRENT git repo pre-commit hook: missing ${htarget }' )
6791 }
68- if os.exists (horiginal ) && os.is_file (horiginal ) {
69- println ('> Main V repo pre-commit hook script: size: ${ostat .size :6 } bytes, sha256: ${ohash }, ${horiginal }' )
92+ if os.exists (original ) && os.is_file (original ) {
93+ println ('> Main V repo pre-commit hook script: size: ${ostat .size :6 } bytes, sha256: ${ohash }, ${original }' )
7094 }
7195 if ohash == thash {
7296 println ('> Both files are exactly the same.' )
7397 if show_instructions {
7498 show_msg_about_removing (htarget)
7599 }
76- return
100+ return true
77101 }
78102 println ('> Files have different hashes.' )
79103 if ohash != '' && thash != '' {
@@ -88,6 +112,7 @@ fn report_status(htarget string, show_instructions bool) {
88112 println ('> with the newest pre-commit formatting script from the main V repo.' )
89113 show_msg_about_removing (htarget)
90114 }
115+ return false
91116}
92117
93118fn show_msg_about_removing (htarget string ) {
0 commit comments