Skip to content
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
22 changes: 18 additions & 4 deletions gitcmds/gitcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -3013,9 +3013,9 @@ func GetCurrentBranchName(wd string) (string, error) {

// createRemote creates a remote in the cloned repository
func CreateRemote(wd, remote, account, token, repoName string, isUpstream bool) error {
repo, err := goGitPkg.PlainOpen(wd)
repo, err := OpenGitRepository(wd)
if err != nil {
return fmt.Errorf("failed to open cloned repository: %w", err)
return err
}

if err = repo.DeleteRemote(remote); err != nil {
Expand Down Expand Up @@ -3064,9 +3064,9 @@ func IamInMainBranch(wd string) (string, bool, error) {

// GetRemoteUrlByName retrieves the URL of a specified remote by its name
func GetRemoteUrlByName(wd string, remoteName string) (string, error) {
repo, err := goGitPkg.PlainOpen(wd)
repo, err := OpenGitRepository(wd)
if err != nil {
return "", fmt.Errorf("failed to open repository: %w", err)
return "", err
}

remotes, err := repo.Remotes()
Expand Down Expand Up @@ -3125,3 +3125,17 @@ func GetIssueDescription(notes []string) (string, error) {

return description, nil
}

// OpenGitRepository opens a git repository at the specified directory
func OpenGitRepository(dir string) (*goGitPkg.Repository, error) {
repo, err := goGitPkg.PlainOpen(dir)
if err != nil {
if errors.Is(err, goGitPkg.ErrRepositoryNotExists) {
return nil, errors.New("no .git directory found; please run this command inside a git repository")
}

return nil, fmt.Errorf("failed to open git repository: %w", err)
}

return repo, nil
}
5 changes: 2 additions & 3 deletions internal/commands/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/atotto/clipboard"
"github.com/fatih/color"
gitPkg "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/spf13/cobra"
"github.com/untillpro/goutils/logger"
Expand Down Expand Up @@ -193,9 +192,9 @@ func Dev(cmd *cobra.Command, wd string, args []string) error {

// branchExists checks if a branch with the given name already exists in the current git repository.
func branchExists(wd, branchName string) (bool, error) {
repo, err := gitPkg.PlainOpen(wd)
repo, err := gitcmds.OpenGitRepository(wd)
if err != nil {
return false, fmt.Errorf("failed to open cloned repository: %w", err)
return false, err
}

branches, err := repo.Branches()
Expand Down
12 changes: 6 additions & 6 deletions internal/systrun/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"regexp"
"strings"

gitPkg "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/untillpro/qs/gitcmds"
)

func findBranchNameWithPrefix(repoPath, prefix string) (string, error) {
repo, err := gitPkg.PlainOpen(repoPath)
repo, err := gitcmds.OpenGitRepository(repoPath)
if err != nil {
return "", fmt.Errorf("failed to open cloned repository: %w", err)
return "", err
}

branches, err := repo.Branches()
Expand Down Expand Up @@ -47,9 +47,9 @@ func parseGithubIssueURL(issueURL string) (string, string, string, error) {
return "", "", "", fmt.Errorf("invalid GitHub issue URL format: %s", issueURL)
}

repoOwner := matches[1]//nolint:revive
repoName := matches[2]//nolint:revive
issueNum := matches[3]//nolint:revive
repoOwner := matches[1] //nolint:revive
repoName := matches[2] //nolint:revive
issueNum := matches[3] //nolint:revive

return repoOwner, repoName, issueNum, nil
}
17 changes: 8 additions & 9 deletions internal/systrun/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strconv"
"strings"

gitPkg "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
goUtilsExec "github.com/untillpro/goutils/exec"
"github.com/untillpro/qs/gitcmds"
Expand Down Expand Up @@ -101,9 +100,9 @@ func ExpectationCustomBranchIsCurrentBranch(ctx context.Context) error {
// ExpectationCloneIsSyncedWithFork checks if the clone is synchronized with the fork
func ExpectationCloneIsSyncedWithFork(ctx context.Context) error {
// Compare local and remote branches to ensure they're in sync
repo, err := gitPkg.PlainOpen(ctx.Value(contextCfg.CtxKeyCloneRepoPath).(string))
repo, err := gitcmds.OpenGitRepository(ctx.Value(contextCfg.CtxKeyCloneRepoPath).(string))
if err != nil {
return fmt.Errorf(errFormatFailedToCloneRepos, err)
return err
}

// Get the current branch
Expand Down Expand Up @@ -139,9 +138,9 @@ func ExpectationCloneIsSyncedWithFork(ctx context.Context) error {
func ExpectationForkExists(ctx context.Context) error {
// Implement the logic to check if the fork exists
// get remotes of the local repo and check if remote, called origin, exists
repo, err := gitPkg.PlainOpen(ctx.Value(contextCfg.CtxKeyCloneRepoPath).(string))
repo, err := gitcmds.OpenGitRepository(ctx.Value(contextCfg.CtxKeyCloneRepoPath).(string))
if err != nil {
return fmt.Errorf(errFormatFailedToCloneRepos, err)
return err
}

// Check if the remote named "origin" exists
Expand Down Expand Up @@ -284,9 +283,9 @@ func ExpectationLargeFileHookFunctional(ctx context.Context) error {
// ExpectationCurrentBranchHasPrefix checks if the current branch has the expected prefix
func ExpectationCurrentBranchHasPrefix(ctx context.Context) error {
// Open the repository
repo, err := gitPkg.PlainOpen(ctx.Value(contextCfg.CtxKeyCloneRepoPath).(string))
repo, err := gitcmds.OpenGitRepository(ctx.Value(contextCfg.CtxKeyCloneRepoPath).(string))
if err != nil {
return fmt.Errorf(errFormatFailedToCloneRepos, err)
return err
}

// Get the current branch
Expand Down Expand Up @@ -335,9 +334,9 @@ func ExpectationPRCreated(ctx context.Context) error {
}

// Open the repository
repo, err := gitPkg.PlainOpen(cloneRepoPath)
repo, err := gitcmds.OpenGitRepository(cloneRepoPath)
if err != nil {
return fmt.Errorf(errFormatFailedToCloneRepos, err)
return err
}

// Check if PR branch exists locally
Expand Down
Loading