GitLens 是 VS Code 中非常强大的 Git 增强插件,它提供了比 VS Code 内置 Git 功能更丰富的特性,包括代码注释 blame 视图、提交历史分析、远程仓库链接跳转等。
gitlens.remotes
正是 GitLens 插件特有的设置项,用于:
- 让 GitLens 识别自定义的代码托管平台(如Github 等)
- 定义各种场景下(查看提交、分支、文件等)的网页链接模板
- 实现从 VS Code 内直接跳转到对应平台网页的功能
获取本地 Git 仓库关联的远程仓库 URL
git remote -v
输出
origin https://github.com/zhengjianhong001/gobang.git (fetch)
origin https://github.com/zhengjianhong001/gobang.git (push)
- 第一条
(fetch)
:表示当你执行git fetch origin
或git pull origin
时,Git 会从这个 URL 拉取远程仓库的更新。 - 第二条
(push)
:表示当你执行git push origin
时,Git 会将本地提交推送到这个 URL 对应的远程仓库。
一、配置方法
- 打开vscode,通过remote ssh打开远程服务器上对应的代码仓库
- 使用快捷键
Ctrl
(MacOS使用Command
) +Shift
+P
,输入”open remote settings”搜索配置文件,点击“首选项:打开远程设置(JSON)”(英文版: Preferences: Open Remote Settings (JSON)). 如果仍然难以找到,可以参考stackoverflow的这个问题下第一个回答 - 到第三节复制配置JSON,贴入配置文件即可(注意JSON层级关系,配置要放在首层大括号内,不要直接贴在文件末尾)
- 这样就能够通过gitlens的hover浮窗来快速跳转到指定的TAPD需求缺陷任务详情页,或是github提交查看页了~

二、效果

三、配置JSON
{
"remote.autoForwardPortsSource": "output",
"gitlens.remotes": [{
"regex": "cnb\\.(oa|woa)\\.com",
"type": "Custom",
"name": "github",
"protocol": "https",
"urls": {
"repository": "https://github.com/${repo}",
"branches": "https://github.com/${repo}/-/branches",
"branch": "https://github.com/${repo}/commits/${branch}",
"commit": "https://github.com/${repo}/commit/${id}",
"file": "https://github.com/${repo}?path=${file}${line}",
"fileInBranch": "https://github.com/${repo}/blob/${branch}/${file}${line}",
"fileInCommit": "https://github.com/${repo}/blob/${id}/${file}${line}",
"fileLine": "#L${line}",
"fileRange": "#L${start}-L${end}"
}
}],
"gitlens.autolinks": [
{
"prefix": "--story=",
"url": "http://tapd.cn/r/t?id=<num>&type=story",
"ignoreCase": true,
},
{
"prefix": "--bug=",
"url": "http://tapd.cn/r/t?id=<num>&type=bug",
"ignoreCase": true,
},
{
"prefix": "--task=",
"url": "http://tapd.cn/r/t?id=<num>&type=task",
"ignoreCase": true,
}
]
}
四、备注
gitlens.autolinks
除了如上三个配置项之外,还可以自由添加其他可能支持的业务对象。具体需要看智能提交助手支持的程度。
五、参考文献
https://github.com/gitkraken/vscode-gitlens#autolink-settings-
https://github.com/gitkraken/vscode-gitlens#remote-provider-integration-settings-
https://github.com/gitkraken/vscode-gitlens/issues/1449
https://stackoverflow.com/questions/65908987/how-can-i-open-visual-studio-codes-settings-json-file