【发布时间】:2021-12-22 02:52:26
【问题描述】:
我想在 git 分支之前列出所有 1 年,然后要求用户输入 YES 以删除所有列出的分支。
#!/bin/bash
if [[ "$data_folder" == "test" ]]; then
current_timestamp=$(date +%s)
twelve_months_ago=$(( $current_timestamp - 12*30*24*60*60 ))
for x in `git branch -r | sed /\*/d`; do
branch_timestamp=$(git show -s --format=%at $x)
if [[ "$branch_timestamp" -lt "$twelve_months_ago" ]]; then
branch_for_removal+=("${x/origin\//}")
fi
done
if [[ "$USERCHOICE" == "YES" ]]; then
git push origin --delete ${branch_for_removal[*]}
echo "Finish!"
else
echo "Exit"
fi
在 git 分支之前列出和删除所有 1 年的逻辑是否正确!
【问题讨论】:
-
将您的文字发送至
shellcheck.net,并在您担心下一部分之前解决明显的问题。 -
在使用 shellcheck.net 之前不要忘记添加 shebang
-
我相信
git show只识别 blob、树、标签和提交,而不是分支:git-scm.com/docs/git-show。对于列出分支,您可能应该使用git branch --list。 -
@JonathonS。 :
git show适用于分支名称。实际上:git 中的分支只是指向提交的指针。参见例如this section of the git book