From 989f3a48f226c993dcf57166447206534043734b Mon Sep 17 00:00:00 2001 From: Alisher Nurmanov Date: Thu, 6 Nov 2025 11:38:26 +0500 Subject: [PATCH] [AIR-1901] qs: [main.main:13]: error checking branch existence: failed to open cloned repository: repository does not exist --- gitcmds/gitcmds.go | 22 ++++++++++++++++++---- internal/commands/dev.go | 5 ++--- internal/systrun/helper.go | 12 ++++++------ internal/systrun/types.go | 17 ++++++++--------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/gitcmds/gitcmds.go b/gitcmds/gitcmds.go index 980ebfa..ae95f8c 100644 --- a/gitcmds/gitcmds.go +++ b/gitcmds/gitcmds.go @@ -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 { @@ -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() @@ -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 +} diff --git a/internal/commands/dev.go b/internal/commands/dev.go index b3d7dd9..adfac23 100644 --- a/internal/commands/dev.go +++ b/internal/commands/dev.go @@ -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" @@ -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() diff --git a/internal/systrun/helper.go b/internal/systrun/helper.go index 5acb3ab..ed234a1 100644 --- a/internal/systrun/helper.go +++ b/internal/systrun/helper.go @@ -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() @@ -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 } diff --git a/internal/systrun/types.go b/internal/systrun/types.go index be24dc9..e809f88 100644 --- a/internal/systrun/types.go +++ b/internal/systrun/types.go @@ -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" @@ -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 @@ -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 @@ -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 @@ -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