Skip to content
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
2 changes: 1 addition & 1 deletion head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
768a778f1f7ccee8a4dd142e7f115e12c7e422fd
b538d805a95b63905601c9f7a03283308abf1b6f
4 changes: 2 additions & 2 deletions upstream/.github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
- uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
- uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
with:
go-version-file: "image/git-init/go.mod"
cache-dependency-path: "image/git-init/go.sum"
Expand Down
4 changes: 2 additions & 2 deletions upstream/.github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ jobs:
# run:
# working-directory: ./image/git-init
steps:
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1

- run: git fetch --prune --unshallow

- uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
- uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
with:
go-version-file: "image/git-init/go.mod"
cache-dependency-path: "image/git-init/go.sum"
Expand Down
22 changes: 16 additions & 6 deletions upstream/image/git-init/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ func Fetch(logger *zap.SugaredLogger, spec FetchSpec, retryConfig RetryConfig) e
}
if spec.Depth > 0 {
fetchArgs = append(fetchArgs, fmt.Sprintf("--depth=%d", spec.Depth))

// Prevent fetching of unrelated git objects with shallow clones.
if _, err := run(logger, "", "config", "--unset", "remote.origin.fetch"); err != nil {
logger.Warnf("Failed to unset remote.origin.fetch in git config: %s", err)
}
}

// Fetch the revision and verify with FETCH_HEAD
Expand Down Expand Up @@ -241,19 +246,24 @@ func showRef(logger *zap.SugaredLogger, revision, path string) (string, error) {
return strings.TrimSuffix(output, "\n"), nil
}

func submoduleFetch(logger *zap.SugaredLogger, spec FetchSpec, retryConfig RetryConfig) error {
if spec.Path != "" {
if err := os.Chdir(spec.Path); err != nil {
return fmt.Errorf("failed to change directory with path %s; err: %w", spec.Path, err)
}
}
func buildSubmoduleUpdateArgs(spec FetchSpec) []string {
updateArgs := []string{"submodule", "update", "--recursive", "--init", "--force"}
if spec.Depth > 0 {
updateArgs = append(updateArgs, fmt.Sprintf("--depth=%d", spec.Depth))
}
if len(spec.SubmodulePaths) > 0 {
updateArgs = append(updateArgs, spec.SubmodulePaths...)
}
return updateArgs
}

func submoduleFetch(logger *zap.SugaredLogger, spec FetchSpec, retryConfig RetryConfig) error {
if spec.Path != "" {
if err := os.Chdir(spec.Path); err != nil {
return fmt.Errorf("failed to change directory with path %s; err: %w", spec.Path, err)
}
}
updateArgs := buildSubmoduleUpdateArgs(spec)
if _, _, err := retryWithBackoff(
func() (string, error) { return run(logger, "", updateArgs...) },
retryConfig.Initial,
Expand Down
94 changes: 93 additions & 1 deletion upstream/image/git-init/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,23 @@ func TestFetch(t *testing.T) {
HTTPSProxy: "",
NOProxy: "",
},
}, {
name: "test-clone-with-depth",
logMessage: "Successfully cloned",
wantErr: false,
spec: FetchSpec{
URL: "",
Revision: "",
Refspec: "",
Path: "",
Depth: 1,
Submodules: false,
SSLVerify: false,
HTTPProxy: "",
HTTPSProxy: "",
NOProxy: "",
SparseCheckoutDirectories: "",
},
},
}
for _, tt := range tests {
Expand All @@ -372,6 +389,7 @@ func TestFetch(t *testing.T) {
}
}()
logger := zap.New(observer).Sugar()
logLine := 1

submodPath := ""
submodName := "default"
Expand Down Expand Up @@ -421,10 +439,42 @@ func TestFetch(t *testing.T) {
t.Errorf("directory patterns and sparse-checkout patterns do not match")
}
}
logLine := 1

if tt.spec.Depth > 0 {
shallowFile, err := os.Open(".git/shallow")
if err != nil {
t.Fatal("Faile to read shallow file")
}
defer shallowFile.Close()

var commitCount int
scanner := bufio.NewScanner(shallowFile)
for scanner.Scan() {
commitCount++
}
if commitCount != int(tt.spec.Depth) {
t.Errorf("Expected %d commits in shallow file, got %d", tt.spec.Depth, commitCount)
}

// Verify remote.origin.fetch was unset
_, err = run(logger, "", "config", "--get", "remote.origin.fetch")
if err == nil {
t.Error("git fetch config should be unset for a shallow clone")
}
}

if tt.spec.Submodules {
submoduleDirs, err := filepath.Glob(".git/modules/*")
if err != nil {
t.Fatalf("Error finding submodule directories: %v", err)
}

if len(submoduleDirs) == 0 {
t.Error("No cloned submodules found")
}
logLine = 3
}

checkLogMessage(t, tt.logMessage, log, logLine)
})
}
Expand Down Expand Up @@ -510,6 +560,48 @@ func (f *SucceedAfter) Run() (string, error) {
return "", fmt.Errorf("temporary error")
}

func TestBuildSubmoduleUpdateArgs(t *testing.T) {
tests := []struct {
name string
spec FetchSpec
expected []string
}{
{
name: "no depth, no submodule paths",
spec: FetchSpec{
Depth: 0,
SubmodulePaths: nil,
},
expected: []string{"submodule", "update", "--recursive", "--init", "--force"},
},
{
name: "with depth, no submodule paths",
spec: FetchSpec{
Depth: 5,
SubmodulePaths: nil,
},
expected: []string{"submodule", "update", "--recursive", "--init", "--force", "--depth=5"},
},
{
name: "no depth, with submodule paths",
spec: FetchSpec{
Depth: 0,
SubmodulePaths: []string{"path/to/submod1", "path/to/submod2"},
},
expected: []string{"submodule", "update", "--recursive", "--init", "--force", "path/to/submod1", "path/to/submod2"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := buildSubmoduleUpdateArgs(tt.spec)
if diff := cmp.Diff(tt.expected, got); diff != "" {
t.Errorf("buildSubmoduleUpdateArgs() mismatch (-want +got):\n%s", diff)
}
})
}
}

func TestRetryWithBackoff(t *testing.T) {
withTemporaryGitConfig(t)
tests := []struct {
Expand Down