Skip to content

Commit

Permalink
Improve merge function
Browse files Browse the repository at this point in the history
Fix some data loss when redirecting a large merged history file directly
to ZSH_HISTORY_FILE, use a temporary file instead.
Remove blank lines in ZSH_HISTORY_FILE with sed.
  • Loading branch information
awerebea authored and wulfgarpro committed May 1, 2022
1 parent 74ef179 commit 851add8
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion history-sync.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ZSH_HISTORY_FILE="${ZSH_HISTORY_FILE:-${HOME}/${ZSH_HISTORY_FILE_NAME}}"
ZSH_HISTORY_FILE_ENC_NAME="${ZSH_HISTORY_FILE_ENC_NAME:-zsh_history}"
ZSH_HISTORY_FILE_ENC="${ZSH_HISTORY_FILE_ENC:-${ZSH_HISTORY_PROJ}/${ZSH_HISTORY_FILE_ENC_NAME}}"
ZSH_HISTORY_FILE_DECRYPT_NAME="${ZSH_HISTORY_FILE_DECRYPT_NAME:-zsh_history_decrypted}"
ZSH_HISTORY_FILE_MERGED_NAME="${ZSH_HISTORY_FILE_MERGED_NAME:-zsh_history_merged}"
ZSH_HISTORY_COMMIT_MSG="${ZSH_HISTORY_COMMIT_MSG:-latest $(date)}"

function _print_git_error_msg() {
Expand Down Expand Up @@ -159,13 +160,15 @@ function history_sync_pull() {
[[ -o extendedhistory ]] && _squash_multiline_commands_in_files

# Merge
cat "$ZSH_HISTORY_FILE" "$ZSH_HISTORY_FILE_DECRYPT_NAME" | awk '/:[0-9]/ { if(s) { print s } s=$0 } !/:[0-9]/ { s=s"\n"$0 } END { print s }' | LC_ALL=C sort -u > "$ZSH_HISTORY_FILE"
cat "$ZSH_HISTORY_FILE" "$ZSH_HISTORY_FILE_DECRYPT_NAME" | awk '/:[0-9]/ { if(s) { print s } s=$0 } !/:[0-9]/ { s=s"\n"$0 } END { print s }' | LC_ALL=C sort -u > "$ZSH_HISTORY_FILE_MERGED_NAME"
mv "$ZSH_HISTORY_FILE_MERGED_NAME" "$ZSH_HISTORY_FILE"
rm "$ZSH_HISTORY_FILE_DECRYPT_NAME"
cd "$DIR"

# Check if EXTENDED_HISTORY is enabled, and if so, restore multi-line
# commands in the local history file
[[ -o extendedhistory ]] && _restore_multiline_commands_in_file
sed -i '/^$/d' "$ZSH_HISTORY_FILE"
}

# Encrypt and push current history to master
Expand Down

0 comments on commit 851add8

Please sign in to comment.