Skip to content

Commit

Permalink
[project-clone] Initialize and update submodules when present in project
Browse files Browse the repository at this point in the history
If a cloned-and-checked-out project has a .gitmodules file in the root
of the repo, run 'git submodule update --init --recursive' inside the
project to clone all submodules.

Failures in setting up submodules result only in a warning log and do
not prevent the project from being 'set up', as the user can later
initialize submodules manually.

Signed-off-by: Angel Misevski <[email protected]>
  • Loading branch information
amisevsk committed Aug 22, 2023
1 parent 65bdb9e commit 9983798
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions project-clone/internal/git/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package git
import (
"fmt"
"log"
"os"
"path"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -118,6 +120,18 @@ func SetupRemotes(repo *git.Repository, project *dw.Project, projectPath string)
return nil
}

func SetupSubmodules(project *dw.Project, projectPath string) error {
if _, err := os.Stat(path.Join(projectPath, ".gitmodules")); os.IsNotExist(err) {
// No submodules; do nothing
return nil
}
log.Printf("Initializing submodules for project %s", project.Name)
if err := shell.GitInitSubmodules(projectPath); err != nil {
return fmt.Errorf("git submodule update --init --recursive failed: %s", err)
}
return nil
}

// CheckoutReference sets the current HEAD in repo to point at the revision and remote referenced by checkoutFrom
func CheckoutReference(project *dw.Project, projectPath string) error {
checkoutFrom := project.Git.CheckoutFrom
Expand Down
4 changes: 4 additions & 0 deletions project-clone/internal/git/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func doInitialGitClone(project *dw.Project) error {
return fmt.Errorf("failed to checkout revision: %s", err)
}

if err := SetupSubmodules(project, tmpClonePath); err != nil {
log.Printf("Failed to set up submodules in project: %s", err)
}

if err := copyProjectFromTmpDir(project, tmpClonePath); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions project-clone/internal/shell/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func GitResolveReference(projectPath, remote, revision string) (GitRefType, erro
return GitRefUnknown, nil
}

func GitInitSubmodules(projectPath string) error {
return executeCommand("git", "-C", projectPath, "submodule", "update", "--init", "--recursive")
}

func executeCommand(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Stderr = log.Writer()
Expand Down

0 comments on commit 9983798

Please sign in to comment.