~breatheoutbreathein/hledger-import-new-xacts

1e5a0015a312a33c264a2e93506716ae40b96592 — Joseph Turner 1 year, 10 months ago e409006
Handle importing files which are not yet tracked by git
1 files changed, 9 insertions(+), 0 deletions(-)

M hledger-import-new-xact.sh
M hledger-import-new-xact.sh => hledger-import-new-xact.sh +9 -0
@@ 6,6 6,7 @@ RULES="$3"
CSV_LENGTH_OFFSET="${4:-0}" # Number of lines in the CSV before/after data

CSV_BASENAME="${CSV##*/}"
CSV_DIRNAME="$(dirname $CSV)"

TMP_DIR="${HOME}/.tmp"
TMP_CSV="$TMP_DIR/$CSV_BASENAME.csv"


@@ 23,6 24,14 @@ duplicates_p () { test ! $(("$JOURNAL_LENGTH" + "$LINES_ADDED_LENGTH" + "$CSV_LE

# Body

# If csv is a new file, not yet tracked by git, import the whole file.
if ! $(git cat-file -e HEAD:$CSV 2> /dev/null); then
    echo -e "$CSV not tracked by git; importing all transactions.\n\n"
    hledger import -f "$JOURNAL" --rules-file="$RULES" "$CSV"
    rm -f "$CSV_DIRNAME/.latest.$CSV_BASENAME" # Clean up any .latest files
    exit 0
fi

# For some reason, hledger import would not import entries from a CSV stored inside /tmp/ on my Debian 11 machine.
# Instead of using a tmp file, it might be preferable if hledger import accepted input from stdin.
mkdir -p "$TMP_DIR"