#!/bin/bash
if [ -z "$(git status --porcelain)" ]; then
echo "没有检测到文件变更,无需提交"
exit 0
fi
if [ $# -eq 1 ]; then
msg="$1"
else
echo "正在分析文件变更..."
changed_files=$(git status --porcelain | head -10)
added_files=$(git status --porcelain | grep "^A" | wc -l | tr -d ' ')
modified_files=$(git status --porcelain | grep "^M" | wc -l | tr -d ' ')
deleted_files=$(git status --porcelain | grep "^D" | wc -l | tr -d ' ')
msg="docs: "
if [ "$added_files" -gt 0 ]; then
msg="${msg}新增${added_files}个文件"
fi
if [ "$modified_files" -gt 0 ]; then
if [ "$added_files" -gt 0 ]; then
msg="${msg},"
fi
msg="${msg}修改${modified_files}个文件"
fi
if [ "$deleted_files" -gt 0 ]; then
if [ "$added_files" -gt 0 ] || [ "$modified_files" -gt 0 ]; then
msg="${msg},"
fi
msg="${msg}删除${deleted_files}个文件"
fi
chapter_changes=$(git status --porcelain | grep -E "chapter[0-9]+/" | head -3)
if [ ! -z "$chapter_changes" ]; then
chapters=$(echo "$chapter_changes" | sed 's/.*chapter\([0-9]\+\).*/\1/' | sort -u | tr '\n' ',' | sed 's/,$//')
msg="${msg} (涉及章节: ${chapters})"
fi
fi
echo "提交信息: $msg"
git add -A
git commit -m "$msg"
if [ $? -eq 0 ]; then
echo "提交成功,开始推送到远程仓库..."
echo "推送到 origin master..."
git push origin master
if git remote | grep -q "gitee"; then
echo "推送到 gitee..."
git push gitee master 2>/dev/null || echo "gitee 推送失败或分支不存在"
fi
if git remote | grep -q "github"; then
echo "推送到 github..."
git push github master 2>/dev/null || echo "github 推送失败或分支不存在"
fi
if git remote | grep -q "gitcode"; then
echo "推送到 gitcode..."
git push gitcode master 2>/dev/null || echo "gitcode 推送失败或分支不存在"
fi
echo "部署完成!"
else
echo "提交失败,请检查错误信息"
exit 1
fi