-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathgit_push.sh
More file actions
executable file
·209 lines (178 loc) · 6.38 KB
/
git_push.sh
File metadata and controls
executable file
·209 lines (178 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash
# ========================================
# Git Workflow Helper Script
# 用法:
# ./git_push.sh # 交互式提交到当前分支
# ./git_push.sh "commit message" # 快速提交到当前分支
# ./git_push.sh -n feature-name # 创建新功能分支并切换
# ./git_push.sh -s branch-name # 切换到已有分支(保留更改)
# ./git_push.sh -l # 列出所有分支
# ========================================
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 获取当前分支
CURRENT_BRANCH=$(git branch --show-current)
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
# 显示帮助
show_help() {
echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN}Git Workflow Helper Script${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo "用法:"
echo " ./git_push.sh # 交互式提交到当前分支"
echo " ./git_push.sh \"commit message\" # 快速提交到当前分支"
echo " ./git_push.sh -n feature-name # 创建新功能分支 (基于 feature/lumenx-v1.0)"
echo " ./git_push.sh -s branch-name # 切换到已有分支 (使用 stash 保留更改)"
echo " ./git_push.sh -l # 列出所有分支"
echo " ./git_push.sh -h # 显示帮助"
echo ""
}
# 列出所有分支
list_branches() {
echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN}本地分支:${NC}"
git branch
echo ""
echo -e "${GREEN}远程分支:${NC}"
git branch -r
echo -e "${BLUE}========================================${NC}"
}
# 创建新功能分支
create_feature_branch() {
local FEATURE_NAME=$1
local BASE_BRANCH="feature/lumenx-v1.0"
local NEW_BRANCH="feature/${FEATURE_NAME}"
echo -e "${BLUE}========================================${NC}"
echo -e "${YELLOW}Creating new feature branch: ${NEW_BRANCH}${NC}"
echo -e "${YELLOW}Based on: ${BASE_BRANCH}${NC}"
echo -e "${BLUE}========================================${NC}"
# 检查是否有未提交的更改
if [[ -n $(git status --porcelain) ]]; then
echo -e "${YELLOW}Detected uncommitted changes. Stashing...${NC}"
git stash push -m "Auto stash before creating branch ${NEW_BRANCH}"
STASHED=true
fi
# 先更新 base branch
echo "Fetching latest from origin..."
git fetch origin
# 检查 base branch 是否存在
if git show-ref --verify --quiet refs/remotes/origin/${BASE_BRANCH}; then
git checkout ${BASE_BRANCH}
git pull origin ${BASE_BRANCH}
else
echo -e "${YELLOW}Base branch ${BASE_BRANCH} not found. Using main instead.${NC}"
BASE_BRANCH="main"
git checkout main
git pull origin main
fi
# 创建新分支
git checkout -b ${NEW_BRANCH}
# 恢复 stash
if [[ "$STASHED" == "true" ]]; then
echo -e "${YELLOW}Restoring stashed changes...${NC}"
git stash pop
fi
echo -e "${GREEN}✅ Successfully created and switched to ${NEW_BRANCH}${NC}"
}
# 切换分支(使用 stash 保留更改)
switch_branch() {
local TARGET_BRANCH=$1
echo -e "${BLUE}========================================${NC}"
echo -e "${YELLOW}Switching to branch: ${TARGET_BRANCH}${NC}"
echo -e "${BLUE}========================================${NC}"
# 检查是否有未提交的更改
if [[ -n $(git status --porcelain) ]]; then
echo -e "${YELLOW}Detected uncommitted changes. Stashing...${NC}"
git stash push -m "Auto stash before switching to ${TARGET_BRANCH}"
STASHED=true
fi
# 切换分支
git checkout ${TARGET_BRANCH}
# 恢复 stash
if [[ "$STASHED" == "true" ]]; then
echo -e "${YELLOW}Restoring stashed changes...${NC}"
git stash pop || {
echo -e "${RED}⚠️ Stash pop failed (possibly conflicts). Use 'git stash list' and 'git stash show' to inspect.${NC}"
}
fi
echo -e "${GREEN}✅ Successfully switched to ${TARGET_BRANCH}${NC}"
}
# 提交并推送
commit_and_push() {
local COMMIT_MSG=$1
echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN}Git Push Process${NC}"
echo -e "${YELLOW}Current branch: ${CURRENT_BRANCH}${NC}"
echo -e "${BLUE}========================================${NC}"
# 显示当前状态
echo ""
echo -e "${GREEN}Current status:${NC}"
git status --short
echo ""
# 如果没有提交信息,交互式输入
if [[ -z "$COMMIT_MSG" ]]; then
echo -e "${YELLOW}Enter commit message (or press Enter for auto message):${NC}"
read -r USER_MSG
if [[ -n "$USER_MSG" ]]; then
COMMIT_MSG="$USER_MSG"
else
COMMIT_MSG="chore: auto commit at $TIMESTAMP"
fi
fi
echo ""
echo -e "${GREEN}Commit message: ${COMMIT_MSG}${NC}"
echo ""
# 确认
echo -e "${YELLOW}Proceed with commit and push? (y/n)${NC}"
read -r CONFIRM
if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then
echo -e "${RED}Aborted.${NC}"
exit 0
fi
# 添加所有更改
echo "1. Adding all changes..."
git add .
# 提交更改
echo "2. Committing changes..."
git commit -m "$COMMIT_MSG"
# 推送到远程仓库
echo "3. Pushing to origin/${CURRENT_BRANCH}..."
git push -u origin ${CURRENT_BRANCH}
echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN}✅ Successfully pushed to origin/${CURRENT_BRANCH}!${NC}"
echo -e "${BLUE}========================================${NC}"
}
# 主逻辑
case "$1" in
-h|--help)
show_help
;;
-l|--list)
list_branches
;;
-n|--new)
if [[ -z "$2" ]]; then
echo -e "${RED}Error: Please provide a feature name.${NC}"
echo "Usage: ./git_push.sh -n feature-name"
exit 1
fi
create_feature_branch "$2"
;;
-s|--switch)
if [[ -z "$2" ]]; then
echo -e "${RED}Error: Please provide a branch name.${NC}"
echo "Usage: ./git_push.sh -s branch-name"
exit 1
fi
switch_branch "$2"
;;
*)
commit_and_push "$1"
;;
esac