-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_git
executable file
·59 lines (46 loc) · 1.31 KB
/
open_git
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
#!/bin/bash
# 设置默认目录为当前目录
directory="."
# 检查是否输入了目录路径
if [ -n "$1" ] && [ "$1" != "-i" ]; then
directory="$1"
fi
# 检查目录是否存在
if [ ! -d "$directory" ]; then
echo "目录不存在: $directory"
exit 1
fi
# 获取所有 remote URL
remote_urls=$(git -C "$directory" remote -v | awk '/fetch/ {print $2}')
# 检查是否成功获取 remote URL
if [ -z "$remote_urls" ]; then
echo "未找到 remote URL,请确保在 Git 仓库中运行该脚本。"
exit 1
fi
# 检查用户是否输入了 -i 参数
interactive=false
if [ "$2" == "-i" ] || [ "$1" == "-i" ]; then
interactive=true
fi
# 如果是交互模式,显示选择界面
if $interactive; then
PS3="请选择一个 remote URL: "
select remote_url in $remote_urls; do
if [ -n "$remote_url" ]; then
break
else
echo "无效选择,请重试。"
fi
done
else
# 默认选择第一个 remote URL
remote_url=$(echo "$remote_urls" | head -n 1)
fi
# 如果是 SSH URL,转换为 HTTPS URL
if [[ "$remote_url" == git@* ]]; then
remote_url=$(echo "$remote_url" | sed -E 's#git@([^:]+):#https://\1/#')
elif [[ "$remote_url" == ssh://git@* ]]; then
remote_url=$(echo "$remote_url" | sed -E 's#ssh://git@([^/]+)#https://\1#')
fi
# 在浏览器中打开 remote URL
open "$remote_url"