From 15abcd8d137cf3e6402d21931e1d6e3ac4ed197c Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sat, 12 Nov 2022 16:30:59 -0800 Subject: [PATCH] Strip history entries with zsh weirdness rather than skip them + ensure the hishtory import command runs a full re-import --- client/client_test.go | 2 ++ client/lib/lib.go | 30 ++++++++++++++++++------------ client/lib/lib_test.go | 18 +++++++++--------- hishtory.go | 6 +++--- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index b6f4b04..757ef82 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1474,6 +1474,8 @@ func testHishtoryOffline(t *testing.T, tester shellTester) { } } +// TODO: tests for hishtory import + func testInitialHistoryImport(t *testing.T, tester shellTester) { // Setup defer testutils.BackupAndRestore(t)() diff --git a/client/lib/lib.go b/client/lib/lib.go index d769294..876ca8c 100644 --- a/client/lib/lib.go +++ b/client/lib/lib.go @@ -190,22 +190,27 @@ func buildCustomColumns(ctx *context.Context) (data.CustomColumns, error) { return ccs, nil } -func isZshWeirdness(cmd string) bool { - // Zsh has this weird behavior where the currently running command is persisted to - // the history file with a weird prefix. This only matters to us when running - // an import, in which case we want to just skip it. - // For example, if the running command was echo foo the command would - // show up in the history file as `: 1663823053:0;echo foo` +func stripZshWeirdness(cmd string) string { + // Zsh has this weird behavior where sometimes commands are saved in the hishtory file + // with a weird prefix. I've never been able to figure out why this happens, but we + // can at least strip it. firstCommandBugRegex := regexp.MustCompile(`: \d+:\d;(.*)`) matches := firstCommandBugRegex.FindStringSubmatch(cmd) - return len(matches) == 2 + if len(matches) == 2 { + return matches[1] + } + return cmd } func isBashWeirdness(cmd string) bool { // Bash has this weird behavior where the it has entries like `#1664342754` in the // history file. We want to skip these. - firstCommandBugRegex := regexp.MustCompile(`#\d+`) - return firstCommandBugRegex.MatchString(cmd) + firstCommandBugRegex := regexp.MustCompile(`^#\d+\s+$`) + result := firstCommandBugRegex.MatchString(cmd) + if result { + fmt.Println("BASH: " + cmd) + } + return result } func buildRegexFromTimeFormat(timeFormat string) string { @@ -517,9 +522,9 @@ func CheckFatalError(err error) { } } -func ImportHistory(ctx *context.Context, shouldReadStdin bool) (int, error) { +func ImportHistory(ctx *context.Context, shouldReadStdin, force bool) (int, error) { config := hctx.GetConf(ctx) - if config.HaveCompletedInitialImport { + if config.HaveCompletedInitialImport && !force { // Don't run an import if we already have run one. This avoids importing the same entry multiple times. return 0, nil } @@ -555,7 +560,8 @@ func ImportHistory(ctx *context.Context, shouldReadStdin bool) (int, error) { return 0, err } for _, cmd := range historyEntries { - if isZshWeirdness(cmd) || isBashWeirdness(cmd) || strings.HasPrefix(cmd, " ") { + cmd := stripZshWeirdness(cmd) + if isBashWeirdness(cmd) || strings.HasPrefix(cmd, " ") { // Skip it continue } diff --git a/client/lib/lib_test.go b/client/lib/lib_test.go index 8ab17d8..4348480 100644 --- a/client/lib/lib_test.go +++ b/client/lib/lib_test.go @@ -369,18 +369,18 @@ func TestChunks(t *testing.T) { func TestZshWeirdness(t *testing.T) { testcases := []struct { - input string - isWeird bool + input string + output string }{ - {": 1666062975:0;bash", true}, - {": 16660:0;ls", true}, - {"ls", false}, - {"0", false}, - {"hgffddxsdsrzsz xddfgdxfdv gdfc ghcvhgfcfg vgv", false}, + {": 1666062975:0;bash", "bash"}, + {": 16660:0;ls", "ls"}, + {"ls", "ls"}, + {"0", "0"}, + {"hgffddxsdsrzsz xddfgdxfdv gdfc ghcvhgfcfg vgv", "hgffddxsdsrzsz xddfgdxfdv gdfc ghcvhgfcfg vgv"}, } for _, tc := range testcases { - actual := isZshWeirdness(tc.input) - if !reflect.DeepEqual(actual, tc.isWeird) { + actual := stripZshWeirdness(tc.input) + if !reflect.DeepEqual(actual, tc.output) { t.Fatalf("weirdness failure for %#v", tc.input) } } diff --git a/hishtory.go b/hishtory.go index f9a9434..e689d59 100644 --- a/hishtory.go +++ b/hishtory.go @@ -71,7 +71,7 @@ func main() { if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" { fmt.Println("Importing existing shell history...") ctx := hctx.MakeContext() - numImported, err := lib.ImportHistory(ctx, false) + numImported, err := lib.ImportHistory(ctx, false, false) lib.CheckFatalError(err) if numImported > 0 { fmt.Printf("Imported %v history entries from your existing shell history\n", numImported) @@ -87,7 +87,7 @@ func main() { if len(data) < 10 { fmt.Println("Importing existing shell history...") ctx := hctx.MakeContext() - numImported, err := lib.ImportHistory(ctx, false) + numImported, err := lib.ImportHistory(ctx, false, false) lib.CheckFatalError(err) if numImported > 0 { fmt.Printf("Imported %v history entries from your existing shell history\n", numImported) @@ -106,7 +106,7 @@ func main() { lib.CheckFatalError(lib.Uninstall(hctx.MakeContext())) case "import": ctx := hctx.MakeContext() - numImported, err := lib.ImportHistory(ctx, true) + numImported, err := lib.ImportHistory(ctx, true, true) lib.CheckFatalError(err) if numImported > 0 { fmt.Printf("Imported %v history entries from your existing shell history\n", numImported)