1
+ // Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
2
+ // Use of this source code is governed by the license in the LICENSE file.
3
+
1
4
package git
2
5
3
6
import (
4
- "os"
5
- "path/filepath"
6
7
"strings"
7
8
8
9
"github.com/pkg/errors"
10
+
9
11
"go.jetpack.io/devbox/internal/cmdutil"
10
12
"go.jetpack.io/devbox/internal/fileutil"
11
13
)
12
14
13
15
const nothingToCommitErrorText = "nothing to commit"
14
16
15
17
func Push (dir , url string ) error {
16
- tmpDir , err := CloneToTmp ( url )
18
+ tmpDir , err := fileutil . CreateDevboxTempDir ( )
17
19
if err != nil {
18
20
return err
19
21
}
20
- if err := removeNonGitFiles (tmpDir ); err != nil {
22
+
23
+ if err := cloneGitHistory (url , tmpDir ); err != nil {
21
24
return err
22
25
}
23
26
@@ -32,6 +35,13 @@ func Push(dir, url string) error {
32
35
return push (tmpDir )
33
36
}
34
37
38
+ func cloneGitHistory (url , dst string ) error {
39
+ // See https://stackoverflow.com/questions/38999901/clone-only-the-git-directory-of-a-git-repo
40
+ cmd := cmdutil .CommandTTY ("git" , "clone" , "--no-checkout" , url , dst )
41
+ cmd .Dir = dst
42
+ return errors .WithStack (cmd .Run ())
43
+ }
44
+
35
45
func createCommit (dir string ) error {
36
46
cmd := cmdutil .CommandTTY ("git" , "add" , "." )
37
47
cmd .Dir = dir
@@ -54,19 +64,3 @@ func push(dir string) error {
54
64
err := cmd .Run ()
55
65
return errors .WithStack (err )
56
66
}
57
-
58
- func removeNonGitFiles (dir string ) error {
59
- entries , err := os .ReadDir (dir )
60
- if err != nil {
61
- return err
62
- }
63
- for _ , entry := range entries {
64
- if entry .Name () == ".git" {
65
- continue
66
- }
67
- if err := os .RemoveAll (filepath .Join (dir , entry .Name ())); err != nil {
68
- return err
69
- }
70
- }
71
- return nil
72
- }
0 commit comments