mirror of
https://github.com/ddworken/hishtory.git
synced 2024-11-22 08:14:02 +01:00
Strip history entries with zsh weirdness rather than skip them + ensure the hishtory import command runs a full re-import
This commit is contained in:
parent
e6fc09cc5d
commit
15abcd8d13
@ -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)()
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user