我正試圖解決這里說明的完全相同的問題:
如何在 Windows 上用 Git 提交可執行的 shell 腳本
"如果你在 Windows 上開發涉及 shell 腳本的軟體,而該軟體也應該在 UNIX 上運行,那么你就會遇到一個問題。
Windows 檔案系統,如 NTFS,不支持 UNIX 的權限位。
無論你在 Windows 上創建新的 shell 腳本,還是重命名現有的 shell 腳本(在簽出時可能是可執行的),這些腳本都是不可執行的。當你推送代碼時,這些腳本將無法運行基于 UNIX 的機器。"
給定的預先提交(pre-commit)是指:"當你推送代碼時,這些腳本不會運行基于 UNIX 的機器。
作為上述問題的解決方案而提出的預提交鉤子腳本是用python撰寫的。
#!/usr/bin/env python。
import subprocess
if __name__ == '__main__'/span>:
output = subprocess.check_output(["git", "ls-files", "-s", "-", "*. sh"], shell=True).decode("utf-8") # type。str
files_to_fix = []
for line in output.splitlines()。
# "行 "的例子。'100644 82f6a7d558e1b38c8b47ec5084fe20f970f09981 0 test-update.sh'/span>
entry = line.replace(' ', ' ').split(", maxsplit=3)
mode = entry[0][3:] # 剝離我們不關心的前三個字符("100")。
filename = entry[3]
if mode == "644"。
files_to_fix.append(filename)
for file_path in files_to_fix:
# git upd-index --chmod= x script.sh
subprocess.check_call(["git", "update-index", "-chmod= x", file_path], shell=True)
我不精通bash,無法用bash重寫。有沒有可能在bash中實作這個目標呢?
uj5u.com熱心網友回復:
用bash腳本掛鉤:
#!/usr/bin/env bash
files_to_fix=()
while read -r -d ''/span> mode _ _ file_path; do
[[ $mode == *644 ]] && files_to_fix =("$file_path"/span>)
done < <(git ls-files -stage -z '*.sh')
git update-index --chmod= x -- "${files_to_fix[@]}"
或者用POSIX shell:
#!/usr/bin/env sh
git ls-files --stage '*.sh' | while read -r mode _ _ file_path; do
case $mode in *644.
*644) git update-index --chmod= x -- "$file_path" ;;
*) ;;
esac
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/324762.html
標籤:
