-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch_git_acc.sh
More file actions
executable file
·69 lines (58 loc) · 1.45 KB
/
switch_git_acc.sh
File metadata and controls
executable file
·69 lines (58 loc) · 1.45 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
#!/usr/bin/env bash
show_help() {
cat <<EOF
Usage:
$0 -a <account_type> [options]
Options:
-a, --account Account type (github, corp)
-h, --help 显示帮助
Example:
$0 -a github
$0 -a corp
EOF
}
ACCOUNT=""
TARGET_DIR="$HOME"
while [[ $# -gt 0 ]]; do
case "$1" in
-a|--account) ACCOUNT="$2"; shift 2 ;;
-h|--help) show_help; exit 0 ;;
*) echo "Unknown option: $1"; show_help; exit 1 ;;
esac
done
if [[ -z "$ACCOUNT" ]]; then
echo "Error: account type is required"
show_help
exit 1
fi
echo "ACC=$ACCOUNT, TARGET_DIR=$TARGET_DIR"
delOldGitCfgLink() {
# 检查文件是否存在
if [ ! -f "$TARGET_DIR/.gitconfig.$ACCOUNT" ]; then
echo "错误: $TARGET_DIR/.gitconfig.$ACCOUNT 不存在"
exit 1
fi
# 如果软链接已存在,先删除
if [ -L "$TARGET_DIR/.gitconfig" ]; then
rm "$TARGET_DIR/.gitconfig"
echo "已删除现有的软链接 $TARGET_DIR/.gitconfig"
fi
}
case "$ACCOUNT" in
github)
delOldGitCfgLink
echo "switch to github config"
ln -s "$TARGET_DIR/.gitconfig.$ACCOUNT" "$TARGET_DIR/.gitconfig"
;;
corp)
delOldGitCfgLink
echo "switch to corp git config"
ln -s "$TARGET_DIR/.gitconfig.$ACCOUNT" "$TARGET_DIR/.gitconfig"
;;
*)
echo "无效参数: $ACCOUNT,请使用 github | corp"
exit 1
;;
esac
echo "================ git config --list ==============>>>"
git config --list