📌  相关文章
📜  打开 github 存储库作为 vs 代码 - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:54:31.633000             🧑  作者: Mango

打开 GitHub 存储库作为 VS Code - Shell-Bash

如果你使用 VS Code 编辑器,并且想要直接在编辑器中打开 GitHub 仓库,那么这个 Shell-Bash 脚本将帮助你完成这个任务。

前置条件

在运行这个脚本之前,你需要确保你的系统已经安装了 GitVS Code 编辑器。

Bash 脚本

以下是这个 Shell-Bash 脚本的代码:

#!/bin/bash

# get current directory
dir=$(pwd)

# check if the directory is a git repo
git rev-parse > /dev/null 2>&1

if [[ "$?" -ne 0 ]]; then
  echo "Error: Not a git repository"
  exit 1
fi

# get the git remote URL
remote=$(git config --get remote.origin.url)

# replace git protocol with https protocol
https=${remote/git:/https:}

# remove .git suffix from URL
url=${https/.git/}

# open in VS Code
code "$url"
如何使用
  1. 在终端中打开你想要查看的 GitHub 仓库所在的目录。

  2. 将以上代码复制到一个新文件中。

  3. 在终端中使用 chmod +x 命令赋予该文件执行权限:

    chmod +x open-vscode-github.sh
    
  4. 在终端中运行脚本:

    ./open-vscode-github.sh
    
  5. 脚本会自动检测当前目录是否是一个 Git 仓库,如果不是,脚本将会提示 Error: Not a git repository

  6. 如果目录是一个 Git 仓库,脚本将会获取仓库的远程 URL,并将该 URL 转换为 HTTPS 协议的 URL。

  7. 脚本会将 URL 作为参数打开 VS Code 并在该仓库的存储库视图中打开。

结论

这个 Shell-Bash 脚本将会帮助你在 VS Code 编辑器中便捷地查看 GitHub 仓库。你只需要在终端中运行这个脚本即可。此外,这个脚本还可以检测当前目录是否是一个 Git 仓库,如果不是则会显示错误提示。