Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/history.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ main() {
done

history_load_operations "$(history_operations_log_file)"
history_load_deletions "$(history_deletions_log_file)"
history_load_deletions "$(history_deletions_log_file)" "$HISTORY_LIMIT"

if [[ "$HISTORY_JSON" == "true" ]]; then
history_render_json "$HISTORY_LIMIT"
Expand Down
28 changes: 22 additions & 6 deletions lib/core/history.sh
Original file line number Diff line number Diff line change
Expand Up @@ -628,21 +628,37 @@ history_load_operations() {

history_load_deletions() {
local log_file="$1"
local limit="${2:-0}" next=0
local timestamp mode size_kb status path

history_reset_deletions

[[ -f "$log_file" ]] || return 0

# Split each row directly, keeping the last row even without a newline.
# Keep the newest valid rows in a ring when the caller supplies a limit.
# An unterminated final row still counts, and malformed rows consume no slot.
while IFS=$'\t' read -r timestamp mode size_kb status path || [[ -n "$timestamp" ]]; do
[[ -n "${timestamp:-}" && -n "${mode:-}" && -n "${status:-}" ]] || continue
HISTORY_DELETE_TIMESTAMPS+=("$timestamp")
HISTORY_DELETE_MODES+=("$mode")
HISTORY_DELETE_SIZE_KB+=("${size_kb:-unknown}")
HISTORY_DELETE_STATUSES+=("$status")
HISTORY_DELETE_PATHS+=("${path:-}")
HISTORY_DELETE_TIMESTAMPS[next]="$timestamp"
HISTORY_DELETE_MODES[next]="$mode"
HISTORY_DELETE_SIZE_KB[next]="${size_kb:-unknown}"
HISTORY_DELETE_STATUSES[next]="$status"
HISTORY_DELETE_PATHS[next]="${path:-}"
next=$((next + 1))
if [[ "$next" -eq "$limit" ]]; then
next=0
fi
done < "$log_file"

# A partially overwritten ring starts at the next write slot. Callers and
# both renderers expect the retained rows in their original file order.
if [[ "$next" -gt 0 && "${#HISTORY_DELETE_TIMESTAMPS[@]}" -eq "$limit" ]]; then
HISTORY_DELETE_TIMESTAMPS=("${HISTORY_DELETE_TIMESTAMPS[@]:next}" "${HISTORY_DELETE_TIMESTAMPS[@]:0:next}")
HISTORY_DELETE_MODES=("${HISTORY_DELETE_MODES[@]:next}" "${HISTORY_DELETE_MODES[@]:0:next}")
HISTORY_DELETE_SIZE_KB=("${HISTORY_DELETE_SIZE_KB[@]:next}" "${HISTORY_DELETE_SIZE_KB[@]:0:next}")
HISTORY_DELETE_STATUSES=("${HISTORY_DELETE_STATUSES[@]:next}" "${HISTORY_DELETE_STATUSES[@]:0:next}")
HISTORY_DELETE_PATHS=("${HISTORY_DELETE_PATHS[@]:next}" "${HISTORY_DELETE_PATHS[@]:0:next}")
fi
}

history_join_counts() {
Expand Down
29 changes: 29 additions & 0 deletions tests/history.bats
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,35 @@ assert rows == [
' "$last_path"
}

@test "bounded deletion history keeps the newest valid rows in file order" {
local audit_log="$HOME/Library/Logs/mole/deletions.log"
local i
for i in 1 2 3 4 5 6 7; do
printf 't%s\tm%s\t%s\ts%s\tp%s\n' "$i" "$i" "$i" "$i" "$i"
printf 'ignored\tshort\n'
done > "$audit_log"
printf 't8\tm8\t8\ts8\tp8' >> "$audit_log"

run env PROJECT_ROOT="$PROJECT_ROOT" AUDIT_LOG="$audit_log" /bin/bash --noprofile --norc <<'BASH'
source "$PROJECT_ROOT/lib/core/history.sh"
for limit in 1 3 4 8 20; do
history_load_deletions "$AUDIT_LOG" "$limit"
count=$limit
[[ $count -le 8 ]] || count=8
[[ ${#HISTORY_DELETE_TIMESTAMPS[@]} -eq $count ]] || exit 1
for ((i = 0; i < count; i++)); do
n=$((9 - count + i))
[[ ${HISTORY_DELETE_TIMESTAMPS[i]} == "t$n" && ${HISTORY_DELETE_MODES[i]} == "m$n" &&
${HISTORY_DELETE_SIZE_KB[i]} == "$n" && ${HISTORY_DELETE_STATUSES[i]} == "s$n" &&
${HISTORY_DELETE_PATHS[i]} == "p$n" ]] || exit 1
done
done
history_load_deletions "$AUDIT_LOG"
[[ ${#HISTORY_DELETE_PATHS[@]} -eq 8 ]]
BASH
[ "$status" -eq 0 ]
}

@test "mo history --json escapes unusual path characters" {
: > "$HOME/Library/Logs/mole/operations.log"
weird_path=$'/tmp/unicode-\xe9\x9b\xaa-quote"slash\\tab\tbackspace\bformfeed\fend'
Expand Down
Loading