From c96c53aa0215351f1ef170b7355e490e8cd8978e Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Fri, 9 Jun 2023 01:24:48 +0900 Subject: [PATCH] refactoring add command (#106) --- cmd/add.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index bb44d34..9385512 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -75,6 +75,19 @@ var addCmd = &cobra.Command{ for _, arg := range args { if _, err := os.Stat(arg); os.IsNotExist(err) { // If the file does not exist but is registered in the index, delete it from the index + // but not delete here, just check it + cleanedArg := filepath.Clean(arg) + cleanedArg = strings.ReplaceAll(cleanedArg, `\`, "/") + _, _, isEntryFound := client.Idx.GetEntry([]byte(cleanedArg)) + if !isEntryFound { + return fmt.Errorf(`path "%s" did not match any files`, arg) + } + } + } + + for _, arg := range args { + // If the file does not exist but is registered in the index, delete it from the index + if _, err := os.Stat(arg); os.IsNotExist(err) { cleanedArg := filepath.Clean(arg) cleanedArg = strings.ReplaceAll(cleanedArg, `\`, "/") _, entry, isEntryFound := client.Idx.GetEntry([]byte(cleanedArg)) @@ -82,12 +95,11 @@ var addCmd = &cobra.Command{ return fmt.Errorf(`path "%s" did not match any files`, arg) } if err := client.Idx.DeleteEntry(client.RootGoitPath, entry); err != nil { - return fmt.Errorf("fail to delete '%s' from index: %w", cleanedArg, err) + return fmt.Errorf("fail to delete untracked file %s: %w", cleanedArg, err) } + continue } - } - for _, arg := range args { path, err := filepath.Abs(arg) if err != nil { return fmt.Errorf("fail to convert abs path: %s", arg)