ben-gibson / GitLink

A Jetbrains plugin that opens a local file under Git version control in its remote host using the default browser.
https://plugins.jetbrains.com/plugin/8183-gitlink
MIT License
671 stars 58 forks source link

With Custom Repo when `Check remote` is disabled the link generated is always with the commit ID in URL #267

Open gkns opened 1 year ago

gkns commented 1 year ago

My remote is Gerrit for review purposes, however, when I generate the link, I want it to point to the GitLab repo; Hence I have a custom domain configuration.

Since my repo is large, I have disabled the Check remote as recommended by the plugin.

But with the above changes, The link generated by Open in GitlabVMW (GitlabVMW is my custom domain configuration which is described below) will always have a commit ID in the file path. This commit ID will not exist in remote since the commit IDs will be different for Gerrit and Gitlab; Making the link 404. Eg link generated: https://gitlab.eng.vmware.com/core-build/repo/-/blob/0361d35bb144cdeaf534a2b1dc03fd19d6af23e5/File.java#L116-L116

I think this is a bug that has to do with the Check remote feature.

My custom domain configuration is as follows:

// File at branch template : 
https://gitlab.eng.vmware.com/core-build/{remote:url:path:0}/-/blob/{branch}/{file:path}/{file:name}#L{line:start}-L{line:end}{line-block:end}

// File at commit template: 
https://gitlab.eng.vmware.com/core-build/{remote:url:path:1}/-/blob/{commit}/{file:name}{line-block:start}#L{line:start}-L{line:end}{line-block:end}

// Commit Template:
https://gitlab.eng.vmware.com/core-build/{remote:url:path:1}/-/commit/{commit}

Am I missing anything or can this be fixed?

gkns commented 1 year ago

Upon looking at the code, may the below be a fix?

Location https://github.com/ben-gibson/GitLink/blob/6cfac49a6102f4c9d4ac162fe3770603b7cd561a/src/main/kotlin/uk/co/ben_gibson/git/link/pipeline/middleware/GenerateUrl.kt#L48

change the condition :

if (commit != null) {
          UrlOptionsFileAtCommit(baseUrl, repositoryFile, commit, context.lineSelection)
} else {
 ...
}

To may be:

if (commit != null && settings.shouldCheckRemote) {
          UrlOptionsFileAtCommit(baseUrl, repositoryFile, commit, context.lineSelection)
} else {
 ...
}
gkns commented 1 year ago

Hi, @ben-gibson, Any ideas to fix this locally as an interim measure?

ben-gibson commented 1 year ago

Sorry for the delayed response, I don't have my own laptop right now.

By unchecking the Check remote option, GitLink can't go to the remote to check if the commit or branch exists there. As it can't perform that check, it has to blindly assume it does (which is the behaviour most people wanted in https://github.com/ben-gibson/GitLink/issues/202). I think what you really need here is, a new option in the settings like Always use branch.

The only work around that I know of would be to re-enable the check remote option for now.

gkns commented 1 year ago

Thank you! Acknowledge. For now, I have made the following tweaks in the custom-domain template and it works.

File at branch template :
https://gitlab.eng.vmware.com/core-build/{remote:url:path:1}/-/blob/<hardcoded-branch-name>/{file:path}/{file:name}#L{line:start}-L{line:end}{line-block:end}

File at commit template: 
https://gitlab.eng.vmware.com/core-build/{remote:url:path:1}/-/blob/{commit}/{file:name}{line-block:start}#L{line:start}-L{line:end}{line-block:end}

Commit Template:
https://gitlab.eng.vmware.com/core-build/{remote:url:path:1}/-/commit/{commit}

Additionally:

Fallback branch: <empty> disable Check remote remote gitlab (Added it using the git remote add command)

Although the above is completely customised for my requirements. Anyone who encounters this issue can can actually tweak the URL template and get around this.