-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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: support subdir component #2116
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"os" | ||
"os/exec" | ||
"os/user" | ||
"path" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
|
@@ -170,6 +171,9 @@ func (gs *gitSourceHandler) shaToCacheKey(sha string) string { | |
if gs.src.KeepGitDir { | ||
key += ".git" | ||
} | ||
if gs.src.Subdir != "" { | ||
key += ":" + gs.src.Subdir | ||
} | ||
return key | ||
} | ||
|
||
|
@@ -480,7 +484,12 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out | |
} | ||
}() | ||
|
||
if gs.src.KeepGitDir { | ||
subdir := path.Clean(gs.src.Subdir) | ||
if subdir == "/" { | ||
subdir = "." | ||
} | ||
|
||
if gs.src.KeepGitDir && subdir == "." { | ||
checkoutDirGit := filepath.Join(checkoutDir, ".git") | ||
if err := os.MkdirAll(checkoutDir, 0711); err != nil { | ||
return nil, err | ||
|
@@ -513,10 +522,44 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out | |
} | ||
gitDir = checkoutDirGit | ||
} else { | ||
_, err = gitWithinDir(ctx, gitDir, checkoutDir, sock, knownHosts, nil, "checkout", ref, "--", ".") | ||
cd := checkoutDir | ||
if subdir != "." { | ||
cd, err = ioutil.TempDir(cd, "checkout") | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to create temporary checkout dir") | ||
} | ||
} | ||
_, err = gitWithinDir(ctx, gitDir, cd, sock, knownHosts, nil, "checkout", ref, "--", ".") | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to checkout remote %s", redactCredentials(gs.src.Remote)) | ||
} | ||
if subdir != "." { | ||
d, err := os.Open(filepath.Join(cd, subdir)) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to open subdir %v", subdir) | ||
} | ||
defer func() { | ||
if d != nil { | ||
d.Close() | ||
} | ||
}() | ||
names, err := d.Readdirnames(0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, n := range names { | ||
if err := os.Rename(filepath.Join(cd, subdir, n), filepath.Join(checkoutDir, n)); err != nil { | ||
return nil, err | ||
} | ||
} | ||
if err := d.Close(); err != nil { | ||
return nil, err | ||
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. Wouldn't 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. The close in defer is skipped in here |
||
} | ||
d = nil // reset defer | ||
if err := os.RemoveAll(cd); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
_, err = gitWithinDir(ctx, gitDir, checkoutDir, sock, knownHosts, gs.auth, "submodule", "update", "--init", "--recursive", "--depth=1") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Wonder is a sparse-checkout wouldn't be more efficient in this case but would require at least Git 2.25.0 which is not widely used atm unfortunately. Maybe this could be fixed with #1048 when go-git/go-git#90 is implemented.
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.
This can be looked at later as an optimization but may be tricky because of the shared local repository buildkit uses for incremental updates(or this would maybe need to be turned off for subdirs then). Also, not quite sure how cleanly fallback can be done for servers that don't support it.