Skip to content

Commit 7754cc5

Browse files
committed
🐛 fix double error display if called outside a git repo without the global flag
1 parent 8679073 commit 7754cc5

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

cmd/check.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"github.com/Shieldine/git-profile/internal"
2020
"github.com/spf13/cobra"
21+
"os"
2122
)
2223

2324
var checkCmd = &cobra.Command{
@@ -41,6 +42,11 @@ func runCheck(cmd *cobra.Command, _ []string) {
4142
email, emailErr = internal.GetGlobalUserEmail()
4243
fmt.Println("Global Git Configuration:")
4344
} else {
45+
if !internal.CheckGitRepo() {
46+
fmt.Println("error: not a git repository")
47+
os.Exit(1)
48+
}
49+
4450
name, nameErr = internal.GetUserName()
4551
email, emailErr = internal.GetUserEmail()
4652
fmt.Println("Local Git Configuration:")

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/spf13/cobra"
2121
)
2222

23-
var version = "1.2.1"
23+
var version = "1.2.2"
2424

2525
var rootCmd = &cobra.Command{
2626
Use: "git-profile",

cmd/set.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ var setCmd = &cobra.Command{
4141
// It sets the Git user configuration (name and email) based on the specified profile.
4242
// If --global flag is used, it sets the global Git configuration; otherwise, it sets the local repository configuration.
4343
func runSet(cmd *cobra.Command, args []string) {
44-
profileName := args[0]
45-
4644
global, _ := cmd.Flags().GetBool("global")
4745

46+
if !global && !internal.CheckGitRepo() {
47+
fmt.Println("error: not a git repository")
48+
os.Exit(1)
49+
}
50+
51+
profileName := args[0]
52+
4853
profile := internal.GetProfileByName(profileName)
4954

5055
if (models.ProfileConfig{}) == profile {

cmd/tempset.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,15 @@ If you don't pass them, you will be asked to provide a name and an email.
4242
// If --global flag is used, it sets the global Git configuration; otherwise, it sets the local repository configuration.
4343
// Credentials can be provided via flags or will be prompted interactively.
4444
func runTempSet(cmd *cobra.Command, _ []string) {
45-
reader := bufio.NewReader(os.Stdin)
4645
global, _ := cmd.Flags().GetBool("global")
4746

47+
if !global && !internal.CheckGitRepo() {
48+
fmt.Println("error: not a git repository")
49+
os.Exit(1)
50+
}
51+
52+
reader := bufio.NewReader(os.Stdin)
53+
4854
if name == "" {
4955
var currentName string
5056
var err error

cmd/unset.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"github.com/Shieldine/git-profile/internal"
2020
"github.com/spf13/cobra"
21+
"os"
2122
)
2223

2324
var unsetCmd = &cobra.Command{
@@ -38,6 +39,11 @@ func runUnset(cmd *cobra.Command, _ []string) {
3839
if global {
3940
fmt.Println("warning: removing global git credentials")
4041
} else {
42+
if !internal.CheckGitRepo() {
43+
fmt.Println("error: not a git repository")
44+
os.Exit(1)
45+
}
46+
4147
fmt.Println("warning: git will default to global credentials without local configuration")
4248
}
4349

@@ -50,8 +56,6 @@ func runUnset(cmd *cobra.Command, _ []string) {
5056
if err != nil {
5157
fmt.Println(err)
5258
}
53-
54-
fmt.Println("Process complete.")
5559
}
5660

5761
// init initializes the unset command and adds it to the root command.

0 commit comments

Comments
 (0)