Skip to content

Commit

Permalink
fix: fix kbcli addon index update always latest (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
1aal authored Nov 21, 2023
1 parent 365cf62 commit 756333e
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions pkg/util/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ package util

import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -144,12 +146,33 @@ func ExecGitCommand(pwd string, args ...string) (string, error) {
}

func IsRepoLatest(destinationPath string) (bool, error) {
if _, err := ExecGitCommand(destinationPath, "fetch", "-v"); err != nil {
var (
currentBranch string
commits string
err error
)
if currentBranch, err = GetCurrentBranch(destinationPath); err != nil {
return false, err
}
output, err := ExecGitCommand(destinationPath, "status", "-uno", "--porcelain")
if err != nil {
if _, err = ExecGitCommand(destinationPath, "fetch", "origin", "-v", "-n"); err != nil {
return false, err
}
return output == "", nil

if commits, err = ExecGitCommand(destinationPath, "rev-list", fmt.Sprintf("%s..origin/%s", currentBranch, currentBranch), "--count"); err == nil {
commits, err := strconv.Atoi(commits)
return err == nil && commits == 0, err
}
return false, err
}

func GetCurrentBranch(destinationPath string) (string, error) {
var (
output string
err error
)

if output, err = ExecGitCommand(destinationPath, "rev-parse", "--abbrev-ref", "HEAD"); err != nil {
return "", err
}
return output, nil
}

0 comments on commit 756333e

Please sign in to comment.