-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2cab0f9
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# README | ||
|
||
|
||
Open a Git repository URL in your browser like `open` command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/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" | ||
|
||
|
||
|
||
|