Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gitfs: support git submodules #2606

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions internal/backend/storage/gitfs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -40,6 +41,31 @@ func getPathOverride(ctx context.Context, def string) string {
return def
}

func gitDir(path string) (string, error) {
path = fsutil.ExpandHomedir(path)
gitPath := filepath.Join(path, ".git")
gitDir := ""

if fsutil.IsFile(gitPath) {
buffer, err := os.ReadFile(gitPath)
if err == nil {
gitSubmoduleDir := strings.Replace(string(buffer), "gitdir: ", "", 1)
gitSubmoduleDir = filepath.Join(path, strings.TrimSuffix(gitSubmoduleDir, "\n"))
if fsutil.IsDir(gitSubmoduleDir) {
gitDir = gitSubmoduleDir
}
}
} else if fsutil.IsDir(gitPath) {
gitDir = gitPath
}

if gitDir == "" {
return "", fmt.Errorf("git repo does not exist at %s", gitPath)
}

return gitDir, nil
}

// Git is a cli based git backend.
type Git struct {
fs *fs.Store
Expand All @@ -50,9 +76,9 @@ type Git struct {
func New(path string) (*Git, error) {
path = fsutil.ExpandHomedir(path)

gitDir := filepath.Join(path, ".git")
if !fsutil.IsDir(gitDir) {
return nil, fmt.Errorf("git repo does not exist at %s", gitDir)
gitDir, err := gitDir(path)
if err != nil {
return nil, err
}

return &Git{
Expand All @@ -73,7 +99,12 @@ func Clone(ctx context.Context, repo, path, userName, userEmail string) (*Git, e
return nil, err
}

g.cfg.LoadAll(filepath.Join(path, ".git"))
gitDir, err := gitDir(path)
if err != nil {
return nil, err
}

g.cfg.LoadAll(gitDir)

// initialize the local git config.
if err := g.InitConfig(ctx, userName, userEmail); err != nil {
Expand All @@ -100,7 +131,12 @@ func Init(ctx context.Context, path, userName, userEmail string) (*Git, error) {
out.Printf(ctx, "git initialized at %s", g.fs.Path())
}

g.cfg.LoadAll(filepath.Join(path, ".git"))
gitDir, err := gitDir(path)
if err != nil {
return nil, err
}

g.cfg.LoadAll(gitDir)

if !ctxutil.IsGitInit(ctx) {
return g, nil
Expand Down Expand Up @@ -192,7 +228,12 @@ func (g *Git) Version(ctx context.Context) semver.Version {

// IsInitialized returns true if this stores has an (probably) initialized .git folder.
func (g *Git) IsInitialized() bool {
return fsutil.IsFile(filepath.Join(g.fs.Path(), ".git", "config"))
gitDir, err := gitDir(g.fs.Path())
if err != nil {
return false
}

return fsutil.IsFile(filepath.Join(gitDir, "config"))
}

// Add adds the listed files to the git index.
Expand Down
3 changes: 2 additions & 1 deletion internal/backend/storage/gitfs/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func (l loader) Init(ctx context.Context, path string) (backend.Storage, error)

func (l loader) Handles(ctx context.Context, path string) error {
path = fsutil.ExpandHomedir(path)
if !fsutil.IsDir(filepath.Join(path, ".git")) {
gitPath := filepath.Join(path, ".git")
if !fsutil.IsDir(gitPath) && !fsutil.IsFile(gitPath) {
return fmt.Errorf("no .git at %s", path)
}

Expand Down