Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Dec 31, 2024
1 parent 8e2e491 commit 0918e75
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
16 changes: 8 additions & 8 deletions modules/git/submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
"code.gitea.io/gitea/modules/log"
)

// SubModuleCommit submodule name and commit from a repository
type SubModuleCommit struct {
type TemplateSubmoduleCommit struct {
Path string
Commit string
}

// GetSubmoduleCommits returns a list of submodules paths and their commits from a repository
// GetTemplateSubmoduleCommits returns a list of submodules paths and their commits from a repository
// This function is only for generating new repos based on existing template, the template couldn't be too large.
func GetSubmoduleCommits(ctx context.Context, repoPath string) (submoduleCommits []SubModuleCommit, _ error) {
func GetTemplateSubmoduleCommits(ctx context.Context, repoPath string) (submoduleCommits []TemplateSubmoduleCommit, _ error) {
stdoutReader, stdoutWriter, err := os.Pipe()
if err != nil {
return nil, err
Expand All @@ -37,21 +36,22 @@ func GetSubmoduleCommits(ctx context.Context, repoPath string) (submoduleCommits
return err
}
if entry.IsSubModule() {
submoduleCommits = append(submoduleCommits, SubModuleCommit{Path: entry.Name(), Commit: entry.ID.String()})
submoduleCommits = append(submoduleCommits, TemplateSubmoduleCommit{Path: entry.Name(), Commit: entry.ID.String()})
}
}
return scanner.Err()
},
}
err = NewCommand(ctx, "ls-tree", "-r", "--", "HEAD").Run(opts)
if err != nil {
return nil, fmt.Errorf("GetSubmoduleCommits: error running git ls-tree: %v", err)
return nil, fmt.Errorf("GetTemplateSubmoduleCommits: error running git ls-tree: %v", err)
}
return submoduleCommits, nil
}

// AddSubmoduleIndexes Adds the given submodules to the git index. Requires the .gitmodules file to be already present.
func AddSubmoduleIndexes(ctx context.Context, repoPath string, submodules []SubModuleCommit) error {
// AddTemplateSubmoduleIndexes Adds the given submodules to the git index.
// It is only for generating new repos based on existing template, requires the .gitmodules file to be already present in the work dir.
func AddTemplateSubmoduleIndexes(ctx context.Context, repoPath string, submodules []TemplateSubmoduleCommit) error {
for _, submodule := range submodules {
cmd := NewCommand(ctx, "update-index", "--add", "--cacheinfo", "160000").AddDynamicArguments(submodule.Commit, submodule.Path)
if stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath}); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion modules/git/submodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestRepository_GetSubmoduleCommits(t *testing.T) {
testRepoPath := filepath.Join(testReposDir, "repo4_submodules")
submodules, err := GetSubmoduleCommits(DefaultContext, testRepoPath)
submodules, err := GetTemplateSubmoduleCommits(DefaultContext, testRepoPath)
require.NoError(t, err)

assert.EqualValues(t, len(submodules), 2)

Check failure on line 16 in modules/git/submodule_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

len: use assert.Len (testifylint)

Check failure on line 16 in modules/git/submodule_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

len: use assert.Len (testifylint)

Check failure on line 16 in modules/git/submodule_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

len: use assert.Len (testifylint)
Expand Down
6 changes: 3 additions & 3 deletions services/repository/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
}

// Get active submodules from the template
submodules, err := git.GetSubmoduleCommits(ctx, tmpDir)
submodules, err := git.GetTemplateSubmoduleCommits(ctx, tmpDir)
if err != nil {
return fmt.Errorf("GetSubmoduleCommits: %w", err)
return fmt.Errorf("GetTemplateSubmoduleCommits: %w", err)
}

if err = util.RemoveAll(filepath.Join(tmpDir, ".git")); err != nil {
Expand Down Expand Up @@ -242,7 +242,7 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
return fmt.Errorf("git remote add: %w", err)
}

if err = git.AddSubmoduleIndexes(ctx, tmpDir, submodules); err != nil {
if err = git.AddTemplateSubmoduleIndexes(ctx, tmpDir, submodules); err != nil {
return fmt.Errorf("failed to add submodules: %v", err)
}

Expand Down
4 changes: 2 additions & 2 deletions templates/repo/view_list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
{{svg "octicon-file-submodule"}}
{{$refURL := $subModuleFile.RefURL AppUrl $.Repository.FullName $.SSHDomain}} {{/* FIXME: the usage of AppUrl seems incorrect, it would be fixed in the future, use AppSubUrl instead */}}
{{if $refURL}}
<a class="muted" href="{{$refURL}}">{{$entry.Name}}</a><span class="at">@</span><a href="{{$refURL}}/commit/{{PathEscape $subModuleFile.RefID}}">{{ShortSha $subModuleFile.RefID}}</a>
<a class="muted" href="{{$refURL}}">{{$entry.Name}}</a> <span class="at">@</span> <a href="{{$refURL}}/commit/{{PathEscape $subModuleFile.RefID}}">{{ShortSha $subModuleFile.RefID}}</a>
{{else}}
{{$entry.Name}}<span class="at">@</span>{{ShortSha $subModuleFile.RefID}}
{{$entry.Name}} <span class="at">@</span> {{ShortSha $subModuleFile.RefID}}
{{end}}
{{else}}
{{if $entry.IsDir}}
Expand Down

0 comments on commit 0918e75

Please sign in to comment.