Skip to content

Commit 88a7414

Browse files
github-actions[bot]Warashihiep-tk
authored
Cherry-pick to release-v0.51.x (#5772)
* Do treeless clone for git clone to improve performance (#5722) Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]> Signed-off-by: pipecd-bot <[email protected]> * remove temp sshKeyFile after use(#2215) (#5769) Signed-off-by: hiep-tk <[email protected]> Signed-off-by: pipecd-bot <[email protected]> --------- Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]> Signed-off-by: pipecd-bot <[email protected]> Signed-off-by: hiep-tk <[email protected]> Co-authored-by: Shinnosuke Sawada-Dazai <[email protected]> Co-authored-by: Hiep Trinh <[email protected]>
1 parent ca03e30 commit 88a7414

File tree

5 files changed

+32
-27
lines changed

5 files changed

+32
-27
lines changed

pkg/app/piped/cmd/piped/piped.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,12 @@ func (p *piped) run(ctx context.Context, input cli.Input) (runErr error) {
175175

176176
// Configure SSH config if needed.
177177
if cfg.Git.ShouldConfigureSSHConfig() {
178-
if err := git.AddSSHConfig(cfg.Git); err != nil {
178+
tempFile, err := git.AddSSHConfig(cfg.Git)
179+
if err != nil {
179180
input.Logger.Error("failed to configure ssh-config", zap.Error(err))
180181
return err
181182
}
183+
defer os.Remove(tempFile)
182184
input.Logger.Info("successfully configured ssh-config")
183185
}
184186

pkg/git/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (c *client) Clone(ctx context.Context, repoID, remote, branch, destination
166166
return nil, err
167167
}
168168
out, err := retryCommand(3, time.Second, logger, func() ([]byte, error) {
169-
args := []string{"clone", "--mirror", remote, repoCachePath}
169+
args := []string{"clone", "--mirror", "--filter=tree:0", remote, repoCachePath}
170170
args = append(authArgs, args...)
171171
return runGitCommand(ctx, c.gitPath, "", c.envsForRepo(remote), args...)
172172
})

pkg/git/repo.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,12 @@ func (r *repo) Copy(dest string) (Worktree, error) {
164164
}
165165

166166
// CopyToModify does cloning the repository to the given destination.
167+
// This method clones the repository from remote origin to the given destination, not from local repository.
167168
// The repository is cloned to the given destination with the .
168169
// NOTE: the given “dest” must be a path that doesn’t exist yet.
169170
// If you don't, you will get an error.
170171
func (r *repo) CopyToModify(dest string) (Repo, error) {
171-
cmd := exec.Command(r.gitPath, "clone", r.dir, dest)
172+
cmd := exec.Command(r.gitPath, "clone", "--filter=tree:0", "--branch", r.clonedBranch, r.remote, dest)
172173
if out, err := cmd.CombinedOutput(); err != nil {
173174
return nil, formatCommandError(err, out)
174175
}
@@ -187,16 +188,6 @@ func (r *repo) CopyToModify(dest string) (Repo, error) {
187188
}
188189
}
189190

190-
// because we did a local cloning so set the remote url of origin
191-
if err := cloned.setRemote(context.Background(), r.remote); err != nil {
192-
return nil, err
193-
}
194-
195-
// fetch the latest changes which doesn't exist in the local repository
196-
if out, err := cloned.runGitCommand(context.Background(), "fetch"); err != nil {
197-
return nil, formatCommandError(err, out)
198-
}
199-
200191
return cloned, nil
201192
}
202193

pkg/git/repo_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,12 @@ func TestCopyToModify(t *testing.T) {
288288
err = faker.makeRepo(org, repoName)
289289
require.NoError(t, err)
290290
r := &repo{
291-
dir: faker.repoDir(org, repoName),
292-
gitPath: faker.gitPath,
293-
remote: faker.repoDir(org, repoName), // use the same directory as remote, it's not a real remote. it's strange but it's ok for testing.
291+
dir: faker.repoDir(org, repoName),
292+
gitPath: faker.gitPath,
293+
remote: faker.repoDir(org, repoName), // use the same directory as remote, it's not a real remote. it's strange but it's ok for testing.
294+
username: "test-user",
295+
email: "test-email",
296+
clonedBranch: "master",
294297
}
295298

296299
commits, err := r.ListCommits(ctx, "")

pkg/git/ssh_config.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,52 +48,61 @@ type sshConfig struct {
4848
IdentityFile string
4949
}
5050

51-
func AddSSHConfig(cfg config.PipedGit) error {
51+
func AddSSHConfig(cfg config.PipedGit) (string, error) {
5252
cfgPath := cfg.SSHConfigFilePath
5353
if cfgPath == "" {
5454
home, err := os.UserHomeDir()
5555
if err != nil {
56-
return fmt.Errorf("failed to detect the current user's home directory: %w", err)
56+
return "", fmt.Errorf("failed to detect the current user's home directory: %w", err)
5757
}
5858
cfgPath = path.Join(home, ".ssh", "config")
5959
}
6060
sshDir := filepath.Dir(cfgPath)
6161

6262
if err := os.MkdirAll(sshDir, 0700); err != nil {
63-
return fmt.Errorf("failed to create a directory %s: %v", sshDir, err)
63+
return "", fmt.Errorf("failed to create a directory %s: %v", sshDir, err)
6464
}
6565

6666
sshKey, err := cfg.LoadSSHKey()
6767
if err != nil {
68-
return err
68+
return "", err
6969
}
7070

7171
sshKeyFile, err := os.CreateTemp(sshDir, "piped-ssh-key-*")
7272
if err != nil {
73-
return err
73+
return "", err
7474
}
75+
needCleanUp := false
76+
defer func() {
77+
if needCleanUp {
78+
os.Remove(sshKeyFile.Name())
79+
}
80+
}()
7581

76-
// TODO: Remove this key file when Piped terminating.
7782
if _, err := sshKeyFile.Write(sshKey); err != nil {
78-
return err
83+
needCleanUp = true
84+
return "", err
7985
}
8086

8187
configData, err := generateSSHConfig(cfg, sshKeyFile.Name())
8288
if err != nil {
83-
return err
89+
needCleanUp = true
90+
return "", err
8491
}
8592

8693
f, err := os.OpenFile(cfgPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
8794
if err != nil {
88-
return fmt.Errorf("could not create/append to %s: %v", cfgPath, err)
95+
needCleanUp = true
96+
return "", fmt.Errorf("could not create/append to %s: %v", cfgPath, err)
8997
}
9098
defer f.Close()
9199

92100
if _, err := f.Write([]byte(configData)); err != nil {
93-
return fmt.Errorf("failed to write sshConfig to %s: %v", cfgPath, err)
101+
needCleanUp = true
102+
return "", fmt.Errorf("failed to write sshConfig to %s: %v", cfgPath, err)
94103
}
95104

96-
return nil
105+
return sshKeyFile.Name(), nil
97106
}
98107

99108
func generateSSHConfig(cfg config.PipedGit, sshKeyFile string) (string, error) {

0 commit comments

Comments
 (0)