Skip to content

Commit

Permalink
Merge pull request #14 from matsuyoshi30/improve-errormessage
Browse files Browse the repository at this point in the history
Add function for detection under a git project directory
  • Loading branch information
matsuyoshi30 committed Jul 4, 2021
2 parents 75c6221 + a3e6847 commit 4806c48
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
28 changes: 27 additions & 1 deletion git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package main

import "os/exec"
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
)

func SetGitConfig(user User, scope Scope, users []User, selectedUserIndex int) error {
cmdName := exec.Command("git", "config", scope.String(), "user.name", user.Name)
Expand All @@ -26,3 +32,23 @@ func SetGitConfig(user User, scope Scope, users []User, selectedUserIndex int) e

return nil
}

func IsUnderGitDir() error {
cwd, err := os.Getwd()
if err != nil {
return err
}

if _, err := os.Stat(filepath.Join(cwd, ".git")); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("you need to be in a git project directory to use gitsu")
} else if err != nil {
return err
}

cmdRevParse := exec.Command("git", "rev-parse", "--is-inside-work-tree")
if err := cmdRevParse.Run(); err != nil {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func run() int {

switch actionType {
case sel:
if err := IsUnderGitDir(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to detect git project: %v\n", err)
return 1
}

if err := selectUser(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to select user: %v\n", err)
return 1
Expand Down

0 comments on commit 4806c48

Please sign in to comment.