ddworken / hishtory

Your shell history: synced, queryable, and in context
https://hishtory.dev
MIT License
2.43k stars 41 forks source link

The same command will be added to `hishtory`'s log file twice. #215

Open hongyi-zhao opened 2 months ago

hongyi-zhao commented 2 months ago

I'm using the latest GitHub version of hishtory on Ubuntu 22.04.4 LTS.

I noticed that after I issued the following command:

werner@x13dai-t:~$ hishtory config-set displayed-columns CWD Command ExitCode timestamp

There will be two duplicate entries with the same timestamp be added to the history log file of hishtory, which seems unreasonable, as shown below:

image

Regards, Zhao

ddworken commented 1 month ago

Ah interesting, thank you for opening this issue! Does this reproduce reliably for you? Or only sometimes? I can't seem to reproduce this, so I'm wondering if this is some sort of race condition.

hongyi-zhao commented 1 month ago

It seems that I can reproduce this reliably, as shown below:

image

I wonder if there is any way to debug this problem conveniently.

BTW, as you can see, the recorded two timestamps for the same operation are inconsistent. Therefore, I suspect there is something wrong with this timestamp-recording logic.

ddworken commented 1 month ago

Ah perfect, thank you for that! I can now reproduce this and am working on a fix in #217. I'll keep you updated as I look into this. :)

hongyi-zhao commented 1 month ago

The above PR doesn't work, see below for my test and comment: https://github.com/ddworken/hishtory/pull/218#issuecomment-2156400014

ddworken commented 1 month ago

Ah interesting, thank you for testing this and letting me know! Just to confirm, this is with the latest version of hishtory installed, right? Can you share with me:

  1. The output of hishtory status (with your secret key redacted)
  2. What version of bash you're using?
hongyi-zhao commented 1 month ago

Ah interesting, thank you for testing this and letting me know! Just to confirm, this is with the latest version of hishtory installed, right?

Yes.

Can you share with me:

1. The output of `hishtory status` (with your secret key redacted)
$ hishtory status
hiSHtory: v0.Unknown
Enabled: true
Secret Key: xxx
Commit Hash: Unknown
2. What version of bash you're using?
$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
ddworken commented 1 month ago

Hmm, two follow-up questions as I continue to look into this:

  1. It seems like you installed hishtory via building it yourself (hence the v0.Unknown). Is that correct?
  2. Have you restarted your terminal since updating hishtory, and are still running into this issue after the restart?
hongyi-zhao commented 1 month ago
  1. Yes.
  2. Yes.
ddworken commented 1 month ago

Hmm, I'm a bit stumped by this and am still struggling to reproduce this. I'm running Ubuntu 22.04 with bash 5.1.16(1)-release (the same as you) and with the latest version of hishtory I'm unable to reproduce this. My best guess is that something has gone wrong with how you're updating/installing hishtory. Can you check whether you're running with the latest shell config via declare -f __hishtory_precommand, it should show something like:

# declare -f __hishtory_precommand
__hishtory_precommand ()
{
    if [ -z "${HISHTORY_AT_PROMPT:-}" ]; then
        return;
    fi;
    unset HISHTORY_AT_PROMPT;
    HISHTORY_START_TIME=`hishtory getTimestamp`;
    CMD=`history 1`;
    if ! [ -z "CMD " ]; then
        if [[ "$CMD" != "$LAST_PRESAVED_COMMAND" ]] && [[ "$CMD" != "$LAST_SAVED_COMMAND" ]]; then
            ( hishtory presaveHistoryEntry bash "$CMD" $HISHTORY_START_TIME & );
        fi;
    fi;
    LAST_PRESAVED_COMMAND=$CMD
}
hongyi-zhao commented 1 month ago

See below:

werner@x13dai-t:~$ declare -f __hishtory_precommand
__hishtory_precommand () 
{ 
    if [ -z "${HISHTORY_AT_PROMPT:-}" ]; then
        return;
    fi;
    unset HISHTORY_AT_PROMPT;
    HISHTORY_START_TIME=`hishtory getTimestamp`;
    CMD=`history 1`;
    if ! [ -z "CMD " ]; then
        ( hishtory presaveHistoryEntry bash "$CMD" $HISHTORY_START_TIME & );
    fi
}
ddworken commented 1 month ago

Ah, that does seem to show that you don't have the latest version of hishtory install. You're missing the updated bit here.

How are you building it locally? go build; ./hishtory install should be enough to update the file at ~/.hishtory/config.sh to have the updated definition of __hishtory_precommand. As long as that is true, then restarting your terminal/shell should work fine.

hongyi-zhao commented 1 month ago

This time, the problem has been fixed and I see the same __hishtory_precommand as yours:

werner@x13dai-t:~$ declare -f __hishtory_precommand
__hishtory_precommand () 
{ 
    if [ -z "${HISHTORY_AT_PROMPT:-}" ]; then
        return;
    fi;
    unset HISHTORY_AT_PROMPT;
    HISHTORY_START_TIME=`hishtory getTimestamp`;
    CMD=`history 1`;
    if ! [ -z "CMD " ]; then
        if [[ "$CMD" != "$LAST_PRESAVED_COMMAND" ]] && [[ "$CMD" != "$LAST_SAVED_COMMAND" ]]; then
            ( hishtory presaveHistoryEntry bash "$CMD" $HISHTORY_START_TIME & );
        fi;
    fi;
    LAST_PRESAVED_COMMAND=$CMD
}

go build; ./hishtory install should be enough to update the file at ~/.hishtory/config.sh to have the updated definition of __hishtory_precommand.

Though ./hishtory install works, but there is no such a subcommand in the help:

werner@x13dai-t:~/Public/repo/github.com/ddworken$ hishtory -h |grep install
  uninstall               Completely uninstall hiSHtory and remove your shell history