Skip to content

Commit

Permalink
fix: git remote
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchnet committed Feb 5, 2024
1 parent 442bc58 commit edea16d
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions remote/git/git-remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (gr *GitRemote) Init(ctx context.Context) (string, error) {
utils.CreateDirIfNotExist(sshDir)

privateKeyPath = filepath.Join(sshDir, "pm")
sshCmd := exec.Command("ssh-keygen", "-t", "rsa", "-b", "4096", "-f", privateKeyPath, "-N", "")
sshCmd := exec.Command("ssh-keygen", "-t", "rsa", "-b", "4096", "-f", privateKeyPath, "-N", "", "-C", "")
sshCmd.Run()

pubKey, err := os.ReadFile(filepath.Join(sshDir, "pm.pub"))
Expand Down Expand Up @@ -187,33 +187,29 @@ func (gr *GitRemote) Push(ctx context.Context, msg ...string) error {
func (gr *GitRemote) Pull(ctx context.Context) error {
utils.CreateDirIfNotExist(gr.LocalPath)

// _, _, repo, err := utils.ExtractGitInfo(gr.Url)
// if err != nil {
// return err
// }

auth, err := auth(gr.PrivateKeyPath, "")
if err != nil {
return fmt.Errorf("failed to create git auth credentials: %w", err)
}

var r *git.Repository
if !utils.IfDirExist(gr.LocalPath) {

r, err = git.PlainOpen(gr.LocalPath)
if err != nil && !errors.Is(err, git.ErrRepositoryNotExists) {
return err
}

if errors.Is(err, git.ErrRepositoryNotExists) {
_, err = git.PlainClone(config.GetString("local.path"), false, &git.CloneOptions{
URL: config.GetString("remote.url"),
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
Auth: auth,
})
if err != nil {
if err != nil && !errors.Is(err, transport.ErrEmptyRemoteRepository) {
return fmt.Errorf("failed to clone git repository: %w", err)
}
}

r, err = git.PlainOpen(gr.LocalPath)
if err != nil {
return err
}

// Get the working directory for the repository
w, err := r.Worktree()
if err != nil {
Expand All @@ -224,7 +220,7 @@ func (gr *GitRemote) Pull(ctx context.Context) error {
if err := w.Pull(&git.PullOptions{
RemoteName: "origin",
Auth: auth,
}); err != nil && !errors.Is(git.NoErrAlreadyUpToDate, err) {
}); err != nil && !errors.Is(git.NoErrAlreadyUpToDate, err) && !errors.Is(err, transport.ErrEmptyRemoteRepository) {
return fmt.Errorf("failed to pull latest changes from origin remote: %w", err)
}

Expand Down

0 comments on commit edea16d

Please sign in to comment.