Skip to content

Commit 6dc19fc

Browse files
GiteaBotwxiaoguang
andauthored
Make submodule link work with relative path (#35034) (#35038)
Backport #35034 by wxiaoguang Fix #35033 Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 9f1baa7 commit 6dc19fc

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

modules/git/commit_submodule_file.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ package git
66

77
import (
88
"context"
9+
"strings"
910

1011
giturl "code.gitea.io/gitea/modules/git/url"
1112
)
1213

1314
// CommitSubmoduleFile represents a file with submodule type.
1415
type CommitSubmoduleFile struct {
15-
refURL string
16-
parsedURL *giturl.RepositoryURL
17-
parsed bool
18-
refID string
19-
repoLink string
16+
refURL string
17+
refID string
18+
19+
parsed bool
20+
targetRepoLink string
2021
}
2122

2223
// NewCommitSubmoduleFile create a new submodule file
@@ -35,20 +36,27 @@ func (sf *CommitSubmoduleFile) SubmoduleWebLink(ctx context.Context, optCommitID
3536
}
3637
if !sf.parsed {
3738
sf.parsed = true
38-
parsedURL, err := giturl.ParseRepositoryURL(ctx, sf.refURL)
39-
if err != nil {
40-
return nil
39+
if strings.HasPrefix(sf.refURL, "../") {
40+
// FIXME: when handling relative path, this logic is not right. It needs to:
41+
// 1. Remember the submodule's full path and its commit's repo home link
42+
// 2. Resolve the relative path: targetRepoLink = path.Join(repoHomeLink, path.Dir(submoduleFullPath), refURL)
43+
// Not an easy task and need to refactor related code a lot.
44+
sf.targetRepoLink = sf.refURL
45+
} else {
46+
parsedURL, err := giturl.ParseRepositoryURL(ctx, sf.refURL)
47+
if err != nil {
48+
return nil
49+
}
50+
sf.targetRepoLink = giturl.MakeRepositoryWebLink(parsedURL)
4151
}
42-
sf.parsedURL = parsedURL
43-
sf.repoLink = giturl.MakeRepositoryWebLink(sf.parsedURL)
4452
}
4553
var commitLink string
4654
if len(optCommitID) == 2 {
47-
commitLink = sf.repoLink + "/compare/" + optCommitID[0] + "..." + optCommitID[1]
55+
commitLink = sf.targetRepoLink + "/compare/" + optCommitID[0] + "..." + optCommitID[1]
4856
} else if len(optCommitID) == 1 {
49-
commitLink = sf.repoLink + "/tree/" + optCommitID[0]
57+
commitLink = sf.targetRepoLink + "/tree/" + optCommitID[0]
5058
} else {
51-
commitLink = sf.repoLink + "/tree/" + sf.refID
59+
commitLink = sf.targetRepoLink + "/tree/" + sf.refID
5260
}
53-
return &SubmoduleWebLink{RepoWebLink: sf.repoLink, CommitWebLink: commitLink}
61+
return &SubmoduleWebLink{RepoWebLink: sf.targetRepoLink, CommitWebLink: commitLink}
5462
}

modules/git/commit_submodule_file_test.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,29 @@ import (
1010
)
1111

1212
func TestCommitSubmoduleLink(t *testing.T) {
13-
sf := NewCommitSubmoduleFile("git@github.com:user/repo.git", "aaaa")
13+
wl := (*CommitSubmoduleFile)(nil).SubmoduleWebLink(t.Context())
14+
assert.Nil(t, wl)
1415

15-
wl := sf.SubmoduleWebLink(t.Context())
16-
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
17-
assert.Equal(t, "https://github.com/user/repo/tree/aaaa", wl.CommitWebLink)
16+
t.Run("GitHubRepo", func(t *testing.T) {
17+
sf := NewCommitSubmoduleFile("git@github.com:user/repo.git", "aaaa")
1818

19-
wl = sf.SubmoduleWebLink(t.Context(), "1111")
20-
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
21-
assert.Equal(t, "https://github.com/user/repo/tree/1111", wl.CommitWebLink)
19+
wl := sf.SubmoduleWebLink(t.Context())
20+
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
21+
assert.Equal(t, "https://github.com/user/repo/tree/aaaa", wl.CommitWebLink)
2222

23-
wl = sf.SubmoduleWebLink(t.Context(), "1111", "2222")
24-
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
25-
assert.Equal(t, "https://github.com/user/repo/compare/1111...2222", wl.CommitWebLink)
23+
wl = sf.SubmoduleWebLink(t.Context(), "1111")
24+
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
25+
assert.Equal(t, "https://github.com/user/repo/tree/1111", wl.CommitWebLink)
2626

27-
wl = (*CommitSubmoduleFile)(nil).SubmoduleWebLink(t.Context())
28-
assert.Nil(t, wl)
27+
wl = sf.SubmoduleWebLink(t.Context(), "1111", "2222")
28+
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
29+
assert.Equal(t, "https://github.com/user/repo/compare/1111...2222", wl.CommitWebLink)
30+
})
31+
32+
t.Run("RelativePath", func(t *testing.T) {
33+
sf := NewCommitSubmoduleFile("../../user/repo", "aaaa")
34+
wl := sf.SubmoduleWebLink(t.Context())
35+
assert.Equal(t, "../../user/repo", wl.RepoWebLink)
36+
assert.Equal(t, "../../user/repo/tree/aaaa", wl.CommitWebLink)
37+
})
2938
}

web_src/css/repo/home-file-list.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272
#repo-files-table .repo-file-cell.name .entry-name {
7373
flex-shrink: 1;
74-
min-width: 3em;
74+
min-width: 1ch; /* leave about one letter space when shrinking, need to fine tune the "shrinks" in this grid in the future */
7575
}
7676

7777
@media (max-width: 767.98px) {

0 commit comments

Comments
 (0)