Skip to content

Commit dabfe1b

Browse files
committed
support subdirectory config for go-get since go 1.25
ref: golang/go@835e36f Signed-off-by: a1012112796 <[email protected]>
1 parent d9a2dfd commit dabfe1b

File tree

12 files changed

+126
-4
lines changed

12 files changed

+126
-4
lines changed

models/repo/repo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,12 @@ func (repo *Repository) MustGetUnit(ctx context.Context, tp unit.Type) *RepoUnit
468468
Type: tp,
469469
Config: cfg,
470470
}
471+
case unit.TypePackages:
472+
cfg := new(PackagesConfig)
473+
return &RepoUnit{
474+
Type: tp,
475+
Config: cfg,
476+
}
471477
}
472478

473479
return &RepoUnit{

models/repo/repo_unit.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,11 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
264264
r.Config = new(IssuesConfig)
265265
case unit.TypeActions:
266266
r.Config = new(ActionsConfig)
267+
case unit.TypePackages:
268+
r.Config = new(PackagesConfig)
267269
case unit.TypeProjects:
268270
r.Config = new(ProjectsConfig)
269-
case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypePackages:
271+
case unit.TypeCode, unit.TypeReleases, unit.TypeWiki:
270272
fallthrough
271273
default:
272274
r.Config = new(UnitConfig)
@@ -319,6 +321,26 @@ func (r *RepoUnit) ProjectsConfig() *ProjectsConfig {
319321
return r.Config.(*ProjectsConfig)
320322
}
321323

324+
// PackagesConfig returns config for unit.PackagesConfig
325+
func (r *RepoUnit) PackagesConfig() *PackagesConfig {
326+
return r.Config.(*PackagesConfig)
327+
}
328+
329+
// PackagesConfig describes package config
330+
type PackagesConfig struct {
331+
GoModuleSubDir string
332+
}
333+
334+
// FromDB fills up a PackagesConfig from serialized format.
335+
func (cfg *PackagesConfig) FromDB(bs []byte) error {
336+
return json.UnmarshalHandleDoubleEncode(bs, &cfg)
337+
}
338+
339+
// ToDB exports a PackagesConfig to a serialized format.
340+
func (cfg *PackagesConfig) ToDB() ([]byte, error) {
341+
return json.Marshal(cfg)
342+
}
343+
322344
func getUnitsByRepoID(ctx context.Context, repoID int64) (units []*RepoUnit, err error) {
323345
var tmpUnits []*RepoUnit
324346
if err := db.GetEngine(ctx).Where("repo_id = ?", repoID).Find(&tmpUnits); err != nil {

modules/structs/repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ type EditRepoOption struct {
227227
MirrorInterval *string `json:"mirror_interval,omitempty"`
228228
// enable prune - remove obsolete remote-tracking references when mirroring
229229
EnablePrune *bool `json:"enable_prune,omitempty"`
230+
// set the subdirectory for Go modules
231+
GoModuleSubDir *string `json:"go_module_sub_dir,omitempty"`
230232
}
231233

232234
// GenerateRepoOption options when creating a repository using a template

options/locale/locale_en-US.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,9 @@ settings.rename_branch_from=old branch name
26292629
settings.rename_branch_to=new branch name
26302630
settings.rename_branch=Rename branch
26312631
2632+
settings.go_module_sub_dir = Golang module subdirectory
2633+
settings.go_module_sub_dir_desc = The subdirectory in repository redirect for go module (which is supported since go 1.25, see <a target="_blank" rel="noopener noreferrer" href="%[1]s">%[2]s</a> for more detail)
2634+
26322635
diff.browse_source = Browse Source
26332636
diff.parent = parent
26342637
diff.commit = commit

routers/api/v1/repo/repo.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,9 +994,17 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
994994

995995
if opts.HasPackages != nil && !unit_model.TypePackages.UnitGlobalDisabled() {
996996
if *opts.HasPackages {
997+
unit := repo.MustGetUnit(ctx, unit_model.TypePackages)
998+
cfg := unit.PackagesConfig()
999+
1000+
if opts.GoModuleSubDir != nil {
1001+
cfg.GoModuleSubDir = *opts.GoModuleSubDir
1002+
}
1003+
9971004
units = append(units, repo_model.RepoUnit{
9981005
RepoID: repo.ID,
9991006
Type: unit_model.TypePackages,
1007+
Config: cfg,
10001008
})
10011009
} else {
10021010
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypePackages)

routers/web/goget.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,18 @@ func goGet(ctx *context.Context) {
5656
if err == nil && len(repo.DefaultBranch) > 0 {
5757
branchName = repo.DefaultBranch
5858
}
59-
prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName))
59+
60+
subDir := ""
61+
if repo != nil {
62+
subDir = context.GetGoModuleSubDirConfig(ctx, repo)
63+
}
64+
65+
var prefix string
66+
if len(subDir) > 0 {
67+
prefix = setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName), subDir)
68+
} else {
69+
prefix = setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName))
70+
}
6071

6172
appURL, _ := url.Parse(setting.AppURL)
6273

@@ -73,6 +84,11 @@ func goGet(ctx *context.Context) {
7384
} else {
7485
cloneURL = repo_model.ComposeHTTPSCloneURL(ctx, ownerName, repoName)
7586
}
87+
88+
if len(subDir) > 0 {
89+
cloneURL += " " + util.PathEscapeSegments(subDir)
90+
}
91+
7692
goImportContent := fmt.Sprintf("%s git %s", goGetImport, cloneURL /*CloneLink*/)
7793
goSourceContent := fmt.Sprintf("%s _ %s %s", goGetImport, prefix+"{/dir}" /*GoDocDirectory*/, prefix+"{/dir}/{file}#L{line}" /*GoDocFile*/)
7894
goGetCli := fmt.Sprintf("go get %s%s", insecure, goGetImport)

routers/web/repo/setting/setting.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,9 @@ func handleSettingsPostAdvanced(ctx *context.Context) {
608608
}
609609

610610
if form.EnablePackages && !unit_model.TypePackages.UnitGlobalDisabled() {
611-
units = append(units, newRepoUnit(repo, unit_model.TypePackages, nil))
611+
units = append(units, newRepoUnit(repo, unit_model.TypePackages, &repo_model.PackagesConfig{
612+
GoModuleSubDir: form.GoModuleSubDir,
613+
}))
612614
} else if !unit_model.TypePackages.UnitGlobalDisabled() {
613615
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypePackages)
614616
}

services/context/repo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ func EarlyResponseForGoGetMeta(ctx *Context) {
344344
ctx.PlainText(http.StatusOK, htmlMeta)
345345
}
346346

347+
// GetGoModuleSubDirConfig retrieves the subdirectory configuration for a Go module.
348+
func GetGoModuleSubDirConfig(ctx *Context, repo *repo_model.Repository) string {
349+
pkgCfg := repo.MustGetUnit(ctx, unit_model.TypePackages).PackagesConfig()
350+
351+
return strings.TrimSpace(pkgCfg.GoModuleSubDir)
352+
}
353+
347354
// RedirectToRepo redirect to a differently-named repository
348355
func RedirectToRepo(ctx *Base, redirectRepoID int64) {
349356
ownerName := ctx.PathParam("username")

services/forms/repo_form.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ type RepoSettingForm struct {
150150

151151
EnableActions bool
152152

153+
GoModuleSubDir string
154+
153155
IsArchived bool
154156

155157
// Signing Settings

templates/repo/settings/options.tmpl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,16 +499,28 @@
499499
</div>
500500
</div>
501501

502+
<div class="divider"></div>
502503
{{$isPackagesEnabled := .Repository.UnitEnabled ctx ctx.Consts.RepoUnitTypePackages}}
503504
{{$isPackagesGlobalDisabled := ctx.Consts.RepoUnitTypePackages.UnitGlobalDisabled}}
505+
{{$pkgUnit := .Repository.MustGetUnit ctx ctx.Consts.RepoUnitTypePackages}}
504506
<div class="inline field">
505507
<label>{{ctx.Locale.Tr "repo.packages"}}</label>
506508
<div class="ui checkbox{{if $isPackagesGlobalDisabled}} disabled{{end}}"{{if $isPackagesGlobalDisabled}} data-tooltip-content="{{ctx.Locale.Tr "repo.unit_disabled"}}"{{end}}>
507-
<input class="enable-system" name="enable_packages" type="checkbox" {{if $isPackagesEnabled}}checked{{end}}>
509+
<input class="enable-system" name="enable_packages" type="checkbox" data-target="#packages_box" {{if $isPackagesEnabled}}checked{{end}}>
508510
<label>{{ctx.Locale.Tr "repo.settings.packages_desc"}}</label>
509511
</div>
510512
</div>
511513

514+
<div class="field{{if not $isPackagesEnabled}} disabled{{end}}" id="packages_box">
515+
<div class="field tw-pl-4 {{if $isPackagesGlobalDisabled}}disabled{{end}}">
516+
<label for="go_module_sub_dir">{{ctx.Locale.Tr "repo.settings.go_module_sub_dir"}}</label>
517+
<input id="go_module_sub_dir" name="go_module_sub_dir" value="{{$pkgUnit.PackagesConfig.GoModuleSubDir}}">
518+
<p class="help">{{ctx.Locale.Tr "repo.settings.go_module_sub_dir_desc" "https://github.com/golang/go/commit/835e36fc7f631f74233edfd4ab43b6b56833db86" "github.com/golang/go@835e36fc7f"}}</p>
519+
</div>
520+
</div>
521+
522+
<div class="divider"></div>
523+
512524
{{if .EnableActions}}
513525
{{$isActionsEnabled := .Repository.UnitEnabled ctx ctx.Consts.RepoUnitTypeActions}}
514526
{{$isActionsGlobalDisabled := ctx.Consts.RepoUnitTypeActions.UnitGlobalDisabled}}

0 commit comments

Comments
 (0)