Added context
Signed-off-by: Steffen Schröter <steffen@vexar.de>
This commit is contained in:
parent
2793799536
commit
d264e186c1
3 changed files with 11 additions and 12 deletions
|
@ -7,6 +7,7 @@ package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
@ -132,7 +133,7 @@ func (sf *SubModuleFile) RefID() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSubmoduleCommits Returns a list of active submodules in the repository
|
// GetSubmoduleCommits Returns a list of active submodules in the repository
|
||||||
func GetSubmoduleCommits(repoPath string) []SubModuleCommit {
|
func GetSubmoduleCommits(ctx context.Context, repoPath string) []SubModuleCommit {
|
||||||
stdoutReader, stdoutWriter := io.Pipe()
|
stdoutReader, stdoutWriter := io.Pipe()
|
||||||
defer func() {
|
defer func() {
|
||||||
_ = stdoutReader.Close()
|
_ = stdoutReader.Close()
|
||||||
|
@ -141,7 +142,7 @@ func GetSubmoduleCommits(repoPath string) []SubModuleCommit {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
stderrBuilder := &strings.Builder{}
|
stderrBuilder := &strings.Builder{}
|
||||||
err := NewCommand("config", "-f", ".gitmodules", "--list", "--name-only").RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder)
|
err := NewCommand(ctx, "config", "-f", ".gitmodules", "--list", "--name-only").RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String()))
|
_ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String()))
|
||||||
} else {
|
} else {
|
||||||
|
@ -154,7 +155,6 @@ func GetSubmoduleCommits(repoPath string) []SubModuleCommit {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
line, err := bufReader.ReadString('\n')
|
line, err := bufReader.ReadString('\n')
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -174,10 +174,9 @@ func GetSubmoduleCommits(repoPath string) []SubModuleCommit {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
commit, err := NewCommand("submodule", "status", name).
|
|
||||||
RunInDir(repoPath)
|
|
||||||
|
|
||||||
// If no commit was found for the module skip it
|
// If no commit was found for the module skip it
|
||||||
|
commit, err := NewCommand(ctx, "submodule", "status", name).
|
||||||
|
RunInDir(repoPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debug("Submodule %s skipped because it has no commit", name)
|
log.Debug("Submodule %s skipped because it has no commit", name)
|
||||||
continue
|
continue
|
||||||
|
@ -208,9 +207,9 @@ func GetSubmoduleCommits(repoPath string) []SubModuleCommit {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddSubmoduleIndexes Adds the given submodules to the git index. Requires the .gitmodules file to be already present.
|
// AddSubmoduleIndexes Adds the given submodules to the git index. Requires the .gitmodules file to be already present.
|
||||||
func AddSubmoduleIndexes(repoPath string, submodules []SubModuleCommit) error {
|
func AddSubmoduleIndexes(ctx context.Context, repoPath string, submodules []SubModuleCommit) error {
|
||||||
for _, submodule := range submodules {
|
for _, submodule := range submodules {
|
||||||
if stdout, err := NewCommand("update-index", "--add", "--cacheinfo", "160000", submodule.Commit, submodule.Name).
|
if stdout, err := NewCommand(ctx, "update-index", "--add", "--cacheinfo", "160000", submodule.Commit, submodule.Name).
|
||||||
RunInDir(repoPath); err != nil {
|
RunInDir(repoPath); err != nil {
|
||||||
log.Error("Unable to add %s as submodule to repo %s: stdout %s\nError: %v", submodule.Name, repoPath, stdout, err)
|
log.Error("Unable to add %s as submodule to repo %s: stdout %s\nError: %v", submodule.Name, repoPath, stdout, err)
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -46,11 +46,11 @@ func TestGetRefURL(t *testing.T) {
|
||||||
|
|
||||||
func TestRepository_GetSubmoduleCommits(t *testing.T) {
|
func TestRepository_GetSubmoduleCommits(t *testing.T) {
|
||||||
bareRepo1Path := filepath.Join(testReposDir, "repo4_submodules")
|
bareRepo1Path := filepath.Join(testReposDir, "repo4_submodules")
|
||||||
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo4_TestRepository_GetSubmoduleCommits")
|
clonedPath, err := cloneRepo(bareRepo1Path, "repo4_TestRepository_GetSubmoduleCommits")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
defer util.RemoveAll(clonedPath)
|
defer util.RemoveAll(clonedPath)
|
||||||
|
|
||||||
submodules := GetSubmoduleCommits(clonedPath)
|
submodules := GetSubmoduleCommits(DefaultContext, clonedPath)
|
||||||
|
|
||||||
assert.EqualValues(t, len(submodules), 2)
|
assert.EqualValues(t, len(submodules), 2)
|
||||||
|
|
||||||
|
|
|
@ -123,7 +123,7 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get active submodules from the template
|
// Get active submodules from the template
|
||||||
submodules := git.GetSubmoduleCommits(tmpDir)
|
submodules := git.GetSubmoduleCommits(ctx, tmpDir)
|
||||||
|
|
||||||
if err := util.RemoveAll(path.Join(tmpDir, ".git")); err != nil {
|
if err := util.RemoveAll(path.Join(tmpDir, ".git")); err != nil {
|
||||||
return fmt.Errorf("remove git dir: %v", err)
|
return fmt.Errorf("remove git dir: %v", err)
|
||||||
|
@ -187,7 +187,7 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
|
||||||
return fmt.Errorf("git remote add: %v", err)
|
return fmt.Errorf("git remote add: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := git.AddSubmoduleIndexes(tmpDir, submodules); err != nil {
|
if err := git.AddSubmoduleIndexes(ctx, tmpDir, submodules); err != nil {
|
||||||
return fmt.Errorf("Failed to add submodules: %v", err)
|
return fmt.Errorf("Failed to add submodules: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue