-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
git-init: add support for fetching submodules 🚝 #1531
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,31 +45,9 @@ func run(logger *zap.SugaredLogger, dir string, args ...string) (string, error) | |
|
||
// Fetch fetches the specified git repository at the revision into path. | ||
func Fetch(logger *zap.SugaredLogger, revision, path, url string) error { | ||
// HACK: This is to get git+ssh to work since ssh doesn't respect the HOME | ||
// env variable. | ||
homepath, err := homedir.Dir() | ||
if err != nil { | ||
logger.Errorf("Unexpected error: getting the user home directory: %v", err) | ||
if err := ensureHomeEnv(logger); err != nil { | ||
return err | ||
} | ||
homeenv := os.Getenv("HOME") | ||
euid := os.Geteuid() | ||
// Special case the root user/directory | ||
if euid == 0 { | ||
if err := os.Symlink(homeenv+"/.ssh", "/root/.ssh"); err != nil { | ||
// Only do a warning, in case we don't have a real home | ||
// directory writable in our image | ||
logger.Warnf("Unexpected error: creating symlink: %v", err) | ||
} | ||
} else if homeenv != "" && homeenv != homepath { | ||
if _, err := os.Stat(homepath + "/.ssh"); os.IsNotExist(err) { | ||
if err := os.Symlink(homeenv+"/.ssh", homepath+"/.ssh"); err != nil { | ||
// Only do a warning, in case we don't have a real home | ||
// directory writable in our image | ||
logger.Warnf("Unexpected error: creating symlink: %v", err) | ||
} | ||
} | ||
} | ||
|
||
if revision == "" { | ||
revision = "master" | ||
|
@@ -111,3 +89,52 @@ func Commit(logger *zap.SugaredLogger, revision, path string) (string, error) { | |
} | ||
return strings.TrimSuffix(output, "\n"), nil | ||
} | ||
|
||
func SubmoduleFetch(logger *zap.SugaredLogger, path string) error { | ||
if err := ensureHomeEnv(logger); err != nil { | ||
return err | ||
} | ||
|
||
if path != "" { | ||
if err := os.Chdir(path); err != nil { | ||
return xerrors.Errorf("Failed to change directory with path %s; err: %w", path, err) | ||
} | ||
} | ||
if _, err := run(logger, "", "submodule", "init"); err != nil { | ||
return err | ||
} | ||
if _, err := run(logger, "", "submodule", "update", "--recursive"); err != nil { | ||
return err | ||
} | ||
logger.Infof("Successfully initialized and updated submodules in path %s", path) | ||
return nil | ||
} | ||
|
||
func ensureHomeEnv(logger *zap.SugaredLogger) error { | ||
// HACK: This is to get git+ssh to work since ssh doesn't respect the HOME | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like this hack is copied from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sbwsg indeed 😛 I was a bit lazy 🤫, I need to enhance that 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice 👍 |
||
// env variable. | ||
homepath, err := homedir.Dir() | ||
if err != nil { | ||
logger.Errorf("Unexpected error: getting the user home directory: %v", err) | ||
return err | ||
} | ||
homeenv := os.Getenv("HOME") | ||
euid := os.Geteuid() | ||
// Special case the root user/directory | ||
if euid == 0 { | ||
if err := os.Symlink(homeenv+"/.ssh", "/root/.ssh"); err != nil { | ||
// Only do a warning, in case we don't have a real home | ||
// directory writable in our image | ||
logger.Warnf("Unexpected error: creating symlink: %v", err) | ||
} | ||
} else if homeenv != "" && homeenv != homepath { | ||
if _, err := os.Stat(homepath + "/.ssh"); os.IsNotExist(err) { | ||
if err := os.Symlink(homeenv+"/.ssh", homepath+"/.ssh"); err != nil { | ||
// Only do a warning, in case we don't have a real home | ||
// directory writable in our image | ||
logger.Warnf("Unexpected error: creating symlink: %v", err) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WDYT about surfacing this option all the way up into the Git resource, so users can turn it off if they'd prefer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imjasonh I was waiting for someone to comment this to do it 😛