Skip to content

Commit

Permalink
Update GitLab URL template (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-gibson authored Aug 30, 2024
1 parent 13296a0 commit b3662de
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ val GitRemote.httpUrl : URL? get() {
url = url.removeSuffix(".git")
}

// Do not try to remove the port if the URL uses the SSH protocol in the SCP syntax. For example
// 'git@github.com:foo.git'. This syntax does not support port definitions. Attempting to remove the port
// Do not try to remove the port if the URL uses the SSH protocol in the SCP syntax e.g.
// 'git@github.com:foo.git' as it does not support port definitions. Attempting to remove the port
// will result in an invalid URL when the repository name is made up of digits.
// See https://github.com/ben-gibson/GitLink/issues/94
if (!url.startsWith("git@")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ class PlatformDetector(val project: Project) {
getRepositoryForFile(projectDirectory) { repository -> getPlatformForRepository(repository).let(consumer) }
}

private fun getPlatformForRepository(repository: GitRepository?): Platform? {
repository ?: return null

private fun getPlatformForRepository(repository: GitRepository): Platform? {
val settings = project.service<ProjectSettings>()

val remote = repository.locateRemote(settings.remote) ?: return null
Expand All @@ -41,7 +39,7 @@ class PlatformDetector(val project: Project) {
}
}

private fun getRepositoryForFile(projectDirectory: VirtualFile, consumer: (GitRepository?) -> Unit) {
private fun getRepositoryForFile(projectDirectory: VirtualFile, consumer: (GitRepository) -> Unit) {
val gitRepositoryManager = GitRepositoryManager.getInstance(project)
val repository = gitRepositoryManager.getRepositoryForFile(projectDirectory)
if (repository != null) {
Expand All @@ -55,7 +53,7 @@ class PlatformDetector(val project: Project) {
VcsRepositoryManager.VCS_REPOSITORY_MAPPING_UPDATED,
VcsRepositoryMappingListener {
busConnection.disconnect()
gitRepositoryManager.getRepositoryForFile(projectDirectory).let(consumer)
gitRepositoryManager.getRepositoryForFile(projectDirectory)?.let(consumer)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ data class UrlTemplates(val fileAtBranch: String, val fileAtCommit : String, val

fun gitLab(): UrlTemplates {
return UrlTemplates(
"{remote:url}/-/{object}/{branch}/{file:path}/{file:name}{line-block:start}#L{line:start}-{line:end}{line-block:end}",
"{remote:url}/-/{object}/{commit}/{file:path}/{file:name}{line-block:start}#L{line:start}-{line:end}{line-block:end}",
"{remote:url}/-/commit/{commit}"
"{remote:url}/{object}/{branch}/{file:path}/{file:name}{line-block:start}#L{line:start}-{line:end}{line-block:end}",
"{remote:url}/{object}/{commit}/{file:path}/{file:name}{line-block:start}#L{line:start}-{line:end}{line-block:end}",
"{remote:url}/commit/{commit}"
)
}

Expand Down
16 changes: 8 additions & 8 deletions src/test/kotlin/uk/co/ben_gibson/git/link/url/GitLabTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class GitLabTest {
fun urlExpectationsProvider(): Stream<Arguments> = Stream.of(
Arguments.of(
UrlOptionsFileAtBranch(REMOTE_BASE_URL, FILE, BRANCH, LINE_SELECTION),
"https://gitlab.com/my/repo/-/blob/master/src/Foo.java#L10-20"
"https://gitlab.com/my/repo/blob/master/src/Foo.java#L10-20"
),
Arguments.of(
UrlOptionsFileAtBranch(REMOTE_BASE_URL, FILE, BRANCH),
"https://gitlab.com/my/repo/-/blob/master/src/Foo.java"
"https://gitlab.com/my/repo/blob/master/src/Foo.java"
),
Arguments.of(
UrlOptionsFileAtCommit(REMOTE_BASE_URL, FILE, COMMIT, LineSelection(10, 20)),
"https://gitlab.com/my/repo/-/blob/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java#L10-20"
"https://gitlab.com/my/repo/blob/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java#L10-20"
),
Arguments.of(
UrlOptionsFileAtBranch(
Expand All @@ -43,31 +43,31 @@ class GitLabTest {
BRANCH,
LINE_SELECTION
),
"https://gitlab.com/my/repo/-/blob/master/Assets/%23/Sources/Code.cs#L10-20"
"https://gitlab.com/my/repo/blob/master/Assets/%23/Sources/Code.cs#L10-20"
),
Arguments.of(
UrlOptionsFileAtCommit(
REMOTE_BASE_URL,
File("resources", true, "src/foo", false),
COMMIT
),
"https://gitlab.com/my/repo/-/tree/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/foo/resources"
"https://gitlab.com/my/repo/tree/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/foo/resources"
),
Arguments.of(
UrlOptionsFileAtCommit(
REMOTE_BASE_URL,
File("my-project", true, "", true),
COMMIT
),
"https://gitlab.com/my/repo/-/tree/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
"https://gitlab.com/my/repo/tree/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),
Arguments.of(
UrlOptionsFileAtCommit(REMOTE_BASE_URL, FILE, COMMIT),
"https://gitlab.com/my/repo/-/blob/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java"
"https://gitlab.com/my/repo/blob/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java"
),
Arguments.of(
UrlOptionsCommit(REMOTE_BASE_URL, COMMIT),
"https://gitlab.com/my/repo/-/commit/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
"https://gitlab.com/my/repo/commit/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
)
)
}
Expand Down

0 comments on commit b3662de

Please sign in to comment.