1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-22 05:29:44 +02:00

throw error when git username not configured

This commit is contained in:
Jesse Duffield 2018-08-09 15:24:12 +10:00
parent f33b2b2277
commit 5f70b2d9cd
2 changed files with 19 additions and 13 deletions

View File

@ -15,8 +15,8 @@ import (
) )
var ( var (
// ErrNoFiles : when there are no modified files in the repo errNoFiles = errors.New("No changed files")
ErrNoFiles = errors.New("No changed files") errNoUsername = errors.New(`No username set. Please do: git config --global user.name "Your Name"`)
) )
func stagedFiles(files []GitFile) []GitFile { func stagedFiles(files []GitFile) []GitFile {
@ -40,7 +40,7 @@ func stageSelectedFile(g *gocui.Gui) error {
func handleFilePress(g *gocui.Gui, v *gocui.View) error { func handleFilePress(g *gocui.Gui, v *gocui.View) error {
file, err := getSelectedFile(g) file, err := getSelectedFile(g)
if err != nil { if err != nil {
if err == ErrNoFiles { if err == errNoFiles {
return nil return nil
} }
return err return err
@ -66,7 +66,7 @@ func handleFilePress(g *gocui.Gui, v *gocui.View) error {
func handleAddPatch(g *gocui.Gui, v *gocui.View) error { func handleAddPatch(g *gocui.Gui, v *gocui.View) error {
file, err := getSelectedFile(g) file, err := getSelectedFile(g)
if err != nil { if err != nil {
if err == ErrNoFiles { if err == errNoFiles {
return nil return nil
} }
return err return err
@ -83,7 +83,7 @@ func handleAddPatch(g *gocui.Gui, v *gocui.View) error {
func getSelectedFile(g *gocui.Gui) (GitFile, error) { func getSelectedFile(g *gocui.Gui) (GitFile, error) {
if len(state.GitFiles) == 0 { if len(state.GitFiles) == 0 {
return GitFile{}, ErrNoFiles return GitFile{}, errNoFiles
} }
filesView, err := g.View("files") filesView, err := g.View("files")
if err != nil { if err != nil {
@ -96,7 +96,7 @@ func getSelectedFile(g *gocui.Gui) (GitFile, error) {
func handleFileRemove(g *gocui.Gui, v *gocui.View) error { func handleFileRemove(g *gocui.Gui, v *gocui.View) error {
file, err := getSelectedFile(g) file, err := getSelectedFile(g)
if err != nil { if err != nil {
if err == ErrNoFiles { if err == errNoFiles {
return nil return nil
} }
return err return err
@ -156,7 +156,7 @@ func renderfilesOptions(g *gocui.Gui, gitFile *GitFile) error {
func handleFileSelect(g *gocui.Gui, v *gocui.View) error { func handleFileSelect(g *gocui.Gui, v *gocui.View) error {
gitFile, err := getSelectedFile(g) gitFile, err := getSelectedFile(g)
if err != nil { if err != nil {
if err != ErrNoFiles { if err != errNoFiles {
return err return err
} }
renderString(g, "main", "No changed files") renderString(g, "main", "No changed files")
@ -182,6 +182,9 @@ func handleCommitPress(g *gocui.Gui, filesView *gocui.View) error {
return createErrorPanel(g, "You cannot commit without a commit message") return createErrorPanel(g, "You cannot commit without a commit message")
} }
if output, err := gitCommit(g, message); err != nil { if output, err := gitCommit(g, message); err != nil {
if err == errNoUsername {
return createErrorPanel(g, err.Error())
}
return createErrorPanel(g, output) return createErrorPanel(g, output)
} }
refreshFiles(g) refreshFiles(g)
@ -193,7 +196,7 @@ func handleCommitPress(g *gocui.Gui, filesView *gocui.View) error {
func genericFileOpen(g *gocui.Gui, v *gocui.View, open func(*gocui.Gui, string) (string, error)) error { func genericFileOpen(g *gocui.Gui, v *gocui.View, open func(*gocui.Gui, string) (string, error)) error {
file, err := getSelectedFile(g) file, err := getSelectedFile(g)
if err != nil { if err != nil {
if err != ErrNoFiles { if err != errNoFiles {
return err return err
} }
return nil return nil
@ -261,7 +264,7 @@ func renderGitFile(gitFile GitFile, filesView *gocui.View) {
func catSelectedFile(g *gocui.Gui) (string, error) { func catSelectedFile(g *gocui.Gui) (string, error) {
item, err := getSelectedFile(g) item, err := getSelectedFile(g)
if err != nil { if err != nil {
if err != ErrNoFiles { if err != errNoFiles {
return "", err return "", err
} }
return "", renderString(g, "main", "No file to display") return "", renderString(g, "main", "No file to display")
@ -330,7 +333,7 @@ func handleSwitchToMerge(g *gocui.Gui, v *gocui.View) error {
} }
file, err := getSelectedFile(g) file, err := getSelectedFile(g)
if err != nil { if err != nil {
if err != ErrNoFiles { if err != errNoFiles {
return err return err
} }
return nil return nil

View File

@ -457,9 +457,12 @@ func gitCommit(g *gocui.Gui, message string) (string, error) {
runSubProcess(g, "bash", "-c", "git commit -m \""+message+"\"") runSubProcess(g, "bash", "-c", "git commit -m \""+message+"\"")
return "", nil return "", nil
} }
userName, _ := gitconfig.Global("user.name") userName, err := gitconfig.Username()
userEmail, _ := gitconfig.Global("user.email") if userName == "" {
_, err := w.Commit(message, &git.CommitOptions{ return "", errNoUsername
}
userEmail, err := gitconfig.Email()
_, err = w.Commit(message, &git.CommitOptions{
Author: &object.Signature{ Author: &object.Signature{
Name: userName, Name: userName,
Email: userEmail, Email: userEmail,