Skip to content

Commit 6afa2aa

Browse files
committed
Add package name to no tagged releases warning
1 parent b890189 commit 6afa2aa

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

src/nimblepkg/download.nim

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,25 @@ proc seemsLikeRevision(version: string): bool =
226226
return false
227227
return true
228228

229+
proc extractPackageName*(url: string): string =
230+
## Extracts package name from repository URL.
231+
##
232+
## For example:
233+
## "https://github.com/nim-lang/nimble.git" -> "nimble"
234+
## "https://gitlab.com/user/mypackage" -> "mypackage"
235+
## "git://example.com/foo/bar.git" -> "bar"
236+
let cleanUrl = removeTrailingGitString(url)
237+
let lastSlash = cleanUrl.rfind('/')
238+
if lastSlash >= 0 and lastSlash < cleanUrl.len - 1:
239+
result = cleanUrl[lastSlash + 1 .. ^1]
240+
else:
241+
# Fallback to the whole URL if no slash found
242+
result = cleanUrl
243+
# Remove any remaining extensions like .tar.gz
244+
let dotPos = result.find('.')
245+
if dotPos > 0:
246+
result = result[0 ..< dotPos]
247+
229248
proc extractOwnerAndRepo(url: string): string =
230249
## Extracts owner and repository string from an URL to GitHub repository.
231250
##
@@ -425,7 +444,8 @@ proc doDownload(url, downloadDir: string, verRange: VersionRange,
425444
doClone(downMethod, url, downloadDir, latest.tag,
426445
onlyTip = not options.forceFullClone, options = options)
427446
else:
428-
display("Warning:", "The package has no tagged releases, downloading HEAD instead.", Warning,
447+
let pkgName = extractPackageName(url)
448+
display("Warning:", "The package '" & pkgName & "' has no tagged releases, downloading HEAD instead.", Warning,
429449
priority = HighPriority)
430450
if downloadTarball(url, options):
431451
result.vcsRevision = doDownloadTarball(url, downloadDir, "HEAD", true)
@@ -444,7 +464,8 @@ proc doDownload(url, downloadDir: string, verRange: VersionRange,
444464
priority = MediumPriority)
445465
doCheckout(downMethod, downloadDir, latest.tag, options = options)
446466
else:
447-
display("Warning:", "The package has no tagged releases, downloading HEAD instead.", Warning,
467+
let pkgName = extractPackageName(url)
468+
display("Warning:", "The package '" & pkgName & "' has no tagged releases, downloading HEAD instead.", Warning,
448469
priority = HighPriority)
449470

450471
if result.vcsRevision == notSetSha1Hash:

tests/tdownload.nim

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (C) Dominik Picheta. All rights reserved.
2+
# BSD License. Look at license.txt for more info.
3+
4+
{.used.}
5+
6+
import unittest
7+
import nimblepkg/download
8+
9+
suite "download":
10+
test "extractPackageName extracts package name from various URL formats":
11+
# GitHub URLs
12+
check extractPackageName("https://github.com/nim-lang/nimble.git") == "nimble"
13+
check extractPackageName("https://github.com/nim-lang/nimble") == "nimble"
14+
check extractPackageName("https://github.com/user/repo.git") == "repo"
15+
check extractPackageName("https://github.com/organization/package-name") ==
16+
"package-name"
17+
18+
# GitLab URLs
19+
check extractPackageName("https://gitlab.com/user/mypackage") == "mypackage"
20+
check extractPackageName("https://gitlab.com/group/subgroup/project.git") ==
21+
"project"
22+
23+
# Bitbucket URLs
24+
check extractPackageName("https://bitbucket.org/user/repository.git") == "repository"
25+
check extractPackageName("https://bitbucket.org/team/project") == "project"
26+
27+
# Generic git URLs
28+
check extractPackageName("git://example.com/foo/bar.git") == "bar"
29+
check extractPackageName("git://server.org/path/to/repo.git") == "repo"
30+
check extractPackageName("ssh://[email protected]/user/package.git") == "package"
31+
32+
# HTTP/HTTPS URLs
33+
check extractPackageName("http://example.com/repos/mypackage.git") == "mypackage"
34+
check extractPackageName("https://custom-git.com/projects/awesome-lib") ==
35+
"awesome-lib"
36+
37+
# Edge cases
38+
check extractPackageName("https://github.com/user/repo.tar.gz") == "repo"
39+
check extractPackageName("https://example.com/package.bundle") == "package"
40+
check extractPackageName("https://server.com/path/to/deeply/nested/package") ==
41+
"package"
42+
43+
# URLs with special characters
44+
check extractPackageName("https://github.com/user/my_package-2.git") ==
45+
"my_package-2"
46+
check extractPackageName("https://gitlab.com/user/package.v2") == "package"
47+
48+
test "extractPackageName handles edge cases gracefully":
49+
# URL without path (returns the domain part)
50+
check extractPackageName("https://example.com") == "example"
51+
52+
# Just a package name (no slashes)
53+
check extractPackageName("package") == "package"
54+
check extractPackageName("package.git") == "package"
55+
56+
test "removeTrailingGitString removes .git suffix":
57+
check removeTrailingGitString("https://github.com/nim-lang/nimble.git") ==
58+
"https://github.com/nim-lang/nimble"
59+
check removeTrailingGitString("https://github.com/nim-lang/nimble") ==
60+
"https://github.com/nim-lang/nimble"
61+
check removeTrailingGitString("repo.git") == "repo"
62+
check removeTrailingGitString("git") == "git"
63+
# Should not remove if string is too short
64+
check removeTrailingGitString(".git") == ".git"
65+
# Should not remove if string is exactly .git

tests/tester.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import tinitcommand
99
import tcheckcommand
1010
import tcleancommand
1111
import tdevelopfeature
12+
import tdownload
1213
import tissues
1314
import tlocaldeps
1415
import tlockfile

0 commit comments

Comments
 (0)