Skip to content

[push] Simplify git clone for push #1096

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

Merged
merged 2 commits into from
Jun 6, 2023
Merged
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
1 change: 1 addition & 0 deletions internal/boxcli/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package boxcli
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

"go.jetpack.io/devbox"
)

Expand Down
6 changes: 6 additions & 0 deletions internal/fileutil/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"

"github.com/pkg/errors"

"go.jetpack.io/devbox/internal/cmdutil"
)

Expand All @@ -31,3 +32,8 @@ func ClearDir(dir string) error {
}
return errors.WithStack(os.MkdirAll(dir, 0755))
}

func CreateDevboxTempDir() (string, error) {
tmpDir, err := os.MkdirTemp("", "devbox")
return tmpDir, errors.WithStack(err)
}
6 changes: 3 additions & 3 deletions internal/pullbox/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/cuecfg"
"go.jetpack.io/devbox/internal/devconfig"
"go.jetpack.io/devbox/internal/fileutil"
)

func (p *pullbox) IsTextDevboxConfig() bool {
Expand All @@ -33,9 +33,9 @@ func (p *pullbox) pullTextDevboxConfig() error {
return err
}

tmpDir, err := os.MkdirTemp("", "devbox")
tmpDir, err := fileutil.CreateDevboxTempDir()
if err != nil {
return errors.WithStack(err)
return err
}
if err = cfg.SaveTo(tmpDir); err != nil {
return err
Expand Down
10 changes: 6 additions & 4 deletions internal/pullbox/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
package git

import (
"os"
"strings"

"github.com/pkg/errors"

"go.jetpack.io/devbox/internal/cmdutil"
"go.jetpack.io/devbox/internal/fileutil"
)

func CloneToTmp(repo string) (string, error) {
tmpDir, err := os.MkdirTemp("", "devbox")
tmpDir, err := fileutil.CreateDevboxTempDir()
if err != nil {
return "", errors.WithStack(err)
return "", err
}

if err := clone(repo, tmpDir); err != nil {
return "", errors.WithStack(err)
return "", err
}
return tmpDir, nil
}
Expand Down
34 changes: 14 additions & 20 deletions internal/pullbox/git/push.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.

package git

import (
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"

"go.jetpack.io/devbox/internal/cmdutil"
"go.jetpack.io/devbox/internal/fileutil"
)

const nothingToCommitErrorText = "nothing to commit"

func Push(dir, url string) error {
tmpDir, err := CloneToTmp(url)
tmpDir, err := fileutil.CreateDevboxTempDir()
if err != nil {
return err
}
if err := removeNonGitFiles(tmpDir); err != nil {

if err := cloneGitHistory(url, tmpDir); err != nil {
return err
}

Expand All @@ -32,6 +35,13 @@ func Push(dir, url string) error {
return push(tmpDir)
}

func cloneGitHistory(url, dst string) error {
// See https://stackoverflow.com/questions/38999901/clone-only-the-git-directory-of-a-git-repo
cmd := cmdutil.CommandTTY("git", "clone", "--no-checkout", url, dst)
cmd.Dir = dst
return errors.WithStack(cmd.Run())
}

func createCommit(dir string) error {
cmd := cmdutil.CommandTTY("git", "add", ".")
cmd.Dir = dir
Expand All @@ -54,19 +64,3 @@ func push(dir string) error {
err := cmd.Run()
return errors.WithStack(err)
}

func removeNonGitFiles(dir string) error {
entries, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, entry := range entries {
if entry.Name() == ".git" {
continue
}
if err := os.RemoveAll(filepath.Join(dir, entry.Name())); err != nil {
return err
}
}
return nil
}