Skip to content

feat: version control awareness; --commit flag for commands which change files - #2861

Closed
simonmichael wants to merge 1 commit into
mainfrom
vcs-commit
Closed

simonmichael wants to merge 1 commit into
mainfrom
vcs-commit

Conversation

@simonmichael

Copy link
Copy Markdown
Member

(This will need some testing and discussion. Is it a good idea ? Or feature creep, better left out so there's a clean separation of concerns ?)

hledger now notices when the journal's files are in a jj or git repository, and can record its changes there.

  • Detection: the repository is the nearest one at or above the main journal file's directory. jj is preferred when a directory has both (a colocated repo).
  • setup: shows whether the journal is version controlled, whether all of its files are tracked (catching a new included file that was never added), and whether they have unrecorded changes.
  • --commit on add, import, get, init and hledger-web: after changing files, commit just the files changed, with a descriptive message, eg:
    $ hledger import --commit
    imported 12 new transactions from bank.csv.rules to /home/me/finance/main.journal
    committed to git: hledger import: 12 new transactions from bank.csv.rules
    
    • Other changes in the repository, staged or not, are left alone. Earlier unrecorded edits in the same files are included, and hledger says so.
    • Ignored files are never committed, so downloaded data can be kept out of version control.
    • Without a repository (or its tool installed), --commit is an error and nothing is changed. With --dry-run, the commit message is shown.
    • add commits once at the end of the session; get commits per phase (data, prices); hledger-web commits each add, edit or upload, checking for a repository at startup.
    • To commit by default, add --commit to a config file.

The tools are run as external programs (no new dependencies). The manual has a new Version control section; the design and implementation notes are in doc/PLAN-vcs.md.

Tests: new hledger/test/vcs.test (git, and jj when installed), isolated from user git/jj configuration. setup's checks and hledger-web's commits were tested manually.

AI usage: Claude Fable 5.2/Opus 5.5, ~84k output tokens

…nge files

hledger now notices when the journal is in a jj or git repository
(the nearest one at or above the main journal file's directory;
jj is preferred when a directory has both).

setup shows whether the journal is version controlled, whether all of
its files are tracked, and whether they have unrecorded changes.

add, import, get, init and hledger-web have a new --commit flag.
After changing files, they commit just the files they changed, with a
descriptive message, like "hledger import: 12 new transactions from
bank.csv.rules", leaving other changes in the repository alone.
If a changed file already had unrecorded changes, those are included,
and hledger says so. With --dry-run, the message is shown.
Without a repository (or its tool), --commit is an error, and nothing
is changed. To commit by default, add --commit to a config file.

The manual has a new Version control section. The design and
implementation notes are in doc/PLAN-vcs.md.

AI usage: Claude Fable 5.2/Opus 5.5, ~84k output tokens
@simonmichael simonmichael added A-WISH Some kind of improvement request or proposal. add import web The hledger-web tool. get setup needs-discussion To unblock: needs more discussion/review/exploration needs-value-proposition To unblock: needs clearer justification, review of benefits vs costs needs-tests To unblock: needs more automated tests or test updates labels Nov 19, 2026
@simonmichael

simonmichael commented Nov 20, 2026 •

Copy link
Copy Markdown
Member Author

Everybody's workflow is different; but I thought more about my own usage. I import most days, but that's not when I commit. I almost always do additional followup or unrelated manual edits first. Such as:

  • filling in unknown accounts
  • adding new account declarations
  • moving imported entries into date order, if I manually recorded newer entries
  • merging imported entries with duplicatory entries recorded manually
  • dropping duplicate transfer entries that my rules don't handle
  • marking entries cleared
  • retroactive edits to older entries
  • updating csv rules
  • updating forecast.journal
  • updating scripts (these are non-hledger files)

So I'm adding entries manually in emacs, and/or doing an import, then I'm doing additional edits in emacs, and once or more per day I'm committing, usually with magit, occasionally with vscode or jj. And for vague reasons of wanting to avoid conflicts/keep history clear and compartmentalised, I usually do multiple separate commits - txns, accounts, rules, scripts, etc.

So it feels rather repetitive and tedious, even though commits are quick with magit. (With a notable exception: if I strengthen precommit hooks to catch more breakages, including in the all-years journal, as I did recently, it adds a delay to each commit, which even if small makes committing noticeably slower and more bothersome.)

So import --commit wouldn't cover all of my edits, even if I did it last in the session, or made changes to my workflow. It doesn't seem that useful for my workflow. What could I do instead ?

I could

  • reduce work by making larger, less fussy commits, at the cost of less detailed history.
  • Or there are hledger-git and hledger-jj addons that make it easy to commit all the journal's files (everything above except the scripts).
  • Or I have a (forgotten) just commit script that commits everything with a standard message.
  • Or I could revive my automated nightly commit with cron, etckeeper-style (hard to make reliable on a laptop), and never think about commits except when I want to.
  • Or give up version control and rely on backups (no thank you).

Or there could be a hledger commit command that commits all journal files to the active VCS, when you're ready for that. Like hledger-git/jj, but simpler and built in.

Or, nothing: don't try to make hledger do this job. At least, not as a builtin.

Here are the hledger-git and hledger-jj CLIs, for reference:

hledger-git - easyish version control for your hledger journal, using git.

An experimental prototype, currently works for the default journal only.
A git repo in the main file's directory will be autocreated if needed.

Subcommands:

hledger git [-h|--help]  - show this help
hledger git record [MSG] - record changes to journal's files (as listed by 'files')
hledger git status       - show unrecorded changes in journal's files (after first record)
hledger git log          - list recorded changes in journal's files (after first record)
hledger git GITARGS      - run another git command in this repo, on all files

The shorter r, s, l command aliases may be used instead.
Extra/unrecognised arguments are passed to git. Git-specific flags should be preceded by --.

You can install this as more convenient top-level commands by creating
hledger-record, hledger-status, hledger-log scripts like:

    #!/bin/sh
    hledger-git record "\$@"

Examples:

$ hledger git s               # briefly show status of journal's files
$ hledger git l -10           # briefly list last 10 commits to journal's files
$ hledger git l -- --stat     # list commits to journal's files, with summaries
$ hledger git r 'txns' -- -n  # commit changes, ignoring any pre-commit hooks
hledger-jj [COMMAND [OPTS]] - easy version control for hledger journals

An easy CLI for keeping your data in version control, using jj and a git repo.
Works for $LEDGER_FILE and its subfiles only (-f is not yet supported).
A repo will be created if needed, in $LEDGER_FILE's directory.
You can run this tool from any directory. Commands may be abbreviated.
Options are passed to jj; you may need to write -- first.

Examples:
$ hledger jj status
$ hledger jj diff
$ hledger jj commit
$ hledger jj c "new txns"
$ hledger jj log -- -n5
$ hledger-jj l -n5 --stat

@simonmichael

Copy link
Copy Markdown
Member Author

What are some workflows where automatic committing might be more useful ?

  • It would probably feel natural for most changes made with a UI - hledger-web, hledger-ui, hledger add, hledger-edit, etc - to be committed immediately.

  • Nightly scheduled committing (etckeeper style). And/or, a manual equivalent, triggered by a simple command, for when cron is not available. These would be nice to have as a fallback.

I think that's all ?

@simonmichael

Copy link
Copy Markdown
Member Author

I think these parts of the PR might be worth keeping:

  • VCS auto-detection; which is useful for

  • the setup command, which shows

  the journal is version controlled ?      yes  jj repo at /Users/simon/finance
  all journal files are tracked ?          yes  12 files
  journal files have unrecorded changes ?   no  
  • hledger-web's --commit flag (making it autocommit after each add or edit through the web interface).

@simonmichael

simonmichael commented Nov 20, 2026 •

Copy link
Copy Markdown
Member Author

And this might be worth adding:

  • a commit command.
    1. Its main advantage over the VCS's commit command would be that it knows the files belonging to the specified journal, so it would automatically include all of those, and only those, in the commit. (It should probably exclude CSV files.)
    2. Secondarily, it could generate a more informative commit message if you don't provide one.
    3. Thirdly, it would provide a standard command, abstracting which VCS this journal is using (git, jj, maybe others in future). This is probably not very important. In future it seems likely we'd focus on jj for the "easy version control" use case.

@simonmichael
simonmichael marked this pull request as draft November 20, 2026 17:48
@simonmichael
simonmichael marked this pull request as ready for review November 20, 2026 17:49
@adept

adept commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

My two cents is that in my experience VCSes typically have lots of commands, switches, features, and if you wrap them expecting a handful of specific workflows, you would inevitable find yourself in a position where you cut users off of specific command, or a feature, or a way to use the VCS, and now you need to think about how to make your wrapper more open and more flexible...

Like, if you have "hledger commit", then "hledger vcs status", and "hledger vcs commit --rebase-push" might be a reasonable ask as well. And so the slippery slope begins

@simonmichael

Copy link
Copy Markdown
Member Author

hledger-git/jj do indeed provide more commands -

  • easy log, status, record commands (hiding VCS complexity)
  • pass through of other commands to the VCS

They aimed to make version-controlling your hledger files practical for non-VCS people.

But as for this PR, yes I think I'll just close it; we could revive it in future if we find more of a value proposition.

@simonmichael simonmichael removed needs-discussion To unblock: needs more discussion/review/exploration needs-value-proposition To unblock: needs clearer justification, review of benefits vs costs needs-tests To unblock: needs more automated tests or test updates labels Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-WISH Some kind of improvement request or proposal. add get import setup web The hledger-web tool.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants