Rework history importing to ensure we read from HISTFILE exactly once and still always read the standard bash/zsh hist files

This commit is contained in:
David Dworken 2022-11-12 16:39:21 -08:00
parent 15abcd8d13
commit 0268554903
No known key found for this signature in database
2 changed files with 12 additions and 19 deletions

View File

@ -1503,7 +1503,7 @@ echo UUID-fishcommand
} }
// Compare the rest of the hishtory export // Compare the rest of the hishtory export
out = tester.RunInteractiveShell(t, `hishtory export -pipefail -`+randomCmdUuid[:5]) out = tester.RunInteractiveShell(t, `hishtory export -pipefail -/tmp/client -`+randomCmdUuid[:5])
if out != "" { if out != "" {
t.Fatalf("expected hishtory export to be empty, was=%v", out) t.Fatalf("expected hishtory export to be empty, was=%v", out)
} }

View File

@ -529,11 +529,13 @@ func ImportHistory(ctx *context.Context, shouldReadStdin, force bool) (int, erro
return 0, nil return 0, nil
} }
homedir := hctx.GetHome(ctx) homedir := hctx.GetHome(ctx)
historyEntries, err := parseBashHistory(homedir) bashHistPath := filepath.Join(homedir, ".bash_history")
historyEntries, err := readFileToArray(bashHistPath)
if err != nil { if err != nil {
return 0, fmt.Errorf("failed to parse bash history: %v", err) return 0, fmt.Errorf("failed to parse bash history: %v", err)
} }
extraEntries, err := parseZshHistory(homedir) zshHistPath := filepath.Join(homedir, ".zsh_history")
extraEntries, err := readFileToArray(zshHistPath)
if err != nil { if err != nil {
return 0, fmt.Errorf("failed to parse zsh history: %v", err) return 0, fmt.Errorf("failed to parse zsh history: %v", err)
} }
@ -543,6 +545,13 @@ func ImportHistory(ctx *context.Context, shouldReadStdin, force bool) (int, erro
return 0, fmt.Errorf("failed to parse fish history: %v", err) return 0, fmt.Errorf("failed to parse fish history: %v", err)
} }
historyEntries = append(historyEntries, extraEntries...) historyEntries = append(historyEntries, extraEntries...)
if histfile := os.Getenv("HISTFILE"); histfile != "" && histfile != zshHistPath && histfile != bashHistPath {
extraEntries, err := readFileToArray(histfile)
if err != nil {
return 0, fmt.Errorf("failed to parse histfile: %v", err)
}
historyEntries = append(historyEntries, extraEntries...)
}
if shouldReadStdin { if shouldReadStdin {
extraEntries, err = readStdin() extraEntries, err = readStdin()
if err != nil { if err != nil {
@ -629,14 +638,6 @@ func parseFishHistory(homedir string) ([]string, error) {
return ret, nil return ret, nil
} }
func parseBashHistory(homedir string) ([]string, error) {
histfile := os.Getenv("HISTFILE")
if histfile == "" || !strings.Contains(os.Getenv("SHELL"), "bash") {
histfile = filepath.Join(homedir, ".bash_history")
}
return readFileToArray(histfile)
}
func readFileToArray(path string) ([]string, error) { func readFileToArray(path string) ([]string, error) {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return []string{}, nil return []string{}, nil
@ -662,14 +663,6 @@ func readFileToArray(path string) ([]string, error) {
return lines, nil return lines, nil
} }
func parseZshHistory(homedir string) ([]string, error) {
histfile := os.Getenv("HISTFILE")
if histfile == "" || !strings.Contains(os.Getenv("SHELL"), "zsh") {
histfile = filepath.Join(homedir, ".zsh_history")
}
return readFileToArray(histfile)
}
func Install() error { func Install() error {
homedir, err := os.UserHomeDir() homedir, err := os.UserHomeDir()
if err != nil { if err != nil {