Tweak ImportHistory so that all entries are guaranteed to have sequential timestamps that are monotonically increasing

This commit is contained in:
David Dworken 2023-10-07 11:05:19 -07:00
parent 1593caa90f
commit f5264b6eff
No known key found for this signature in database
2 changed files with 7 additions and 3 deletions

View File

@ -2489,7 +2489,8 @@ func TestImportHistory(t *testing.T) {
require.Equal(t, "305", out)
out = tester.RunInteractiveShell(t, ` hishtory export -pipefail`)
require.Contains(t, out, "echo command-305")
out = tester.RunInteractiveShell(t, ` hishtory export -pipefail`)
testutils.CompareGoldens(t, out, "TestImportHistory-export")
// TODO: There is a bug here that will be evident if you check that the export is in the correct order on the remote device, because timestamps don't have sufficient granularity
}

View File

@ -290,6 +290,7 @@ func ImportHistory(ctx context.Context, shouldReadStdin, force bool) (int, error
numEntriesImported := 0
var iteratorError error = nil
var batch []data.HistoryEntry
importTimestamp := time.Now().UTC()
batchSize := 100
entriesIter(func(cmd string, err error) bool {
if err != nil {
@ -300,6 +301,8 @@ func ImportHistory(ctx context.Context, shouldReadStdin, force bool) (int, error
if isBashWeirdness(cmd) || strings.HasPrefix(cmd, " ") {
return true
}
startTime := importTimestamp.Add(time.Millisecond * time.Duration(numEntriesImported*2))
endTime := startTime.Add(time.Millisecond)
entry := normalizeEntryTimezone(data.HistoryEntry{
LocalUsername: currentUser.Name,
Hostname: hostname,
@ -307,8 +310,8 @@ func ImportHistory(ctx context.Context, shouldReadStdin, force bool) (int, error
CurrentWorkingDirectory: "Unknown",
HomeDirectory: homedir,
ExitCode: 0,
StartTime: time.Now().UTC(),
EndTime: time.Now().UTC(),
StartTime: startTime,
EndTime: endTime,
DeviceId: config.DeviceId,
EntryId: uuid.Must(uuid.NewRandom()).String(),
})