bin: hledger-git, hledger-pijul
This commit is contained in:
		
							parent
							
								
									b03bbc9184
								
							
						
					
					
						commit
						a7d4c3ad1e
					
				
							
								
								
									
										98
									
								
								bin/hledger-git
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										98
									
								
								bin/hledger-git
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,98 @@ | |||||||
|  | #!/usr/bin/env bash | ||||||
|  | set -e | ||||||
|  | 
 | ||||||
|  | # * About | ||||||
|  | usage() { line80; cat <<EOF # keep synced with Commands below | ||||||
|  | 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 record [MSG] - record the journal's files (as listed by 'files') | ||||||
|  | hledger git status       - show unrecorded changes (after first record) | ||||||
|  | hledger git log          - list the journal's change history (after record) | ||||||
|  | hledger git              - show this help | ||||||
|  | 
 | ||||||
|  | Extra arguments are passed to git (git-specific flags should be preceded by --). | ||||||
|  | You can install these as more convenient top-level commands by creating  | ||||||
|  | hledger-record, hledger-status, hledger-log scripts like: | ||||||
|  | 
 | ||||||
|  |     #!/bin/sh | ||||||
|  |     hledger-git record "\$@" | ||||||
|  | 
 | ||||||
|  | Examples: | ||||||
|  | 
 | ||||||
|  | hledger git status | ||||||
|  | hledger git log -10 | ||||||
|  | hledger git log -- -10 --stat | ||||||
|  | 
 | ||||||
|  | EOF | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # * Utils | ||||||
|  | 
 | ||||||
|  | line80() { cat <<EOF | ||||||
|  | -------------------------------------------------------------------------------- | ||||||
|  | EOF | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | no_repo_msg() { | ||||||
|  |     echo "Try this again after 'record'." | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | MAINFILE=$LEDGER_FILE | ||||||
|  | FILES=$(hledger -f "$MAINFILE" files) | ||||||
|  | DIR=$(dirname "$MAINFILE") | ||||||
|  | # executable name, just one word | ||||||
|  | GIT=git | ||||||
|  | 
 | ||||||
|  | ensure_git() { | ||||||
|  |     if ! hash $GIT 2>/dev/null; then | ||||||
|  |         cat >&2 <<EOF | ||||||
|  | This command requires '$GIT', but it's not installed in \$PATH. | ||||||
|  | Please install it (see https://git.org/downloads) and try again. | ||||||
|  | EOF | ||||||
|  |         exit 1 | ||||||
|  |     fi | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # TODO: also look in parent directories | ||||||
|  | ensure_git_repo() { | ||||||
|  |     if [[ ! -d "$DIR/.git" ]]; then | ||||||
|  |         $GIT init "$DIR" | ||||||
|  |         echo "Created git repo in $DIR" | ||||||
|  |     fi | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # * Commands | ||||||
|  | # keep synced with usage() above | ||||||
|  | 
 | ||||||
|  | record() { | ||||||
|  |     ensure_git | ||||||
|  |     ensure_git_repo | ||||||
|  |     cd "$DIR" | ||||||
|  |     for F in $FILES; do $GIT add -f "$F"; done | ||||||
|  |     MSG=${1:-$(date +'%Y-%m-%d %H:%M:%S %Z')}; shift | ||||||
|  |     $GIT record -m "$MSG" "$@" -- "$FILES" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | status() { | ||||||
|  |     ensure_git | ||||||
|  |     $GIT --work-tree "$DIR" status -sb "$@" -- "$FILES" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | log() { | ||||||
|  |     ensure_git | ||||||
|  |     # ensure_git_repo | ||||||
|  |     # $GIT --work-tree "$DIR" log --format='%C(yellow)%ad %Cred%h%Creset %s%C(bold blue)%d%Creset' --date=short -1000000 "$@" | ||||||
|  |     cd "$DIR" | ||||||
|  |     # TODO: limit to hledger files | ||||||
|  |     $GIT log --format='%ad %h %s' --date=short "$@" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # * Main | ||||||
|  | 
 | ||||||
|  | # NOTE intended to run Commands but will run any function above | ||||||
|  | if declare -f "$1" > /dev/null; then "$@"; else usage; fi | ||||||
							
								
								
									
										110
									
								
								bin/hledger-pijul
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										110
									
								
								bin/hledger-pijul
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,110 @@ | |||||||
|  | #!/usr/bin/env bash | ||||||
|  | set -e | ||||||
|  | 
 | ||||||
|  | # * About | ||||||
|  | usage() { line80; cat <<EOF # keep synced with Commands below | ||||||
|  | hledger-pijul - easy version control for your hledger journal. | ||||||
|  | 
 | ||||||
|  | An experimental prototype, currently works for the default journal only. | ||||||
|  | This uses pijul, the VCS most likely to support a simple just-works UI. | ||||||
|  | A pijul repo in the main file's directory, and a pijul commit-signing key, | ||||||
|  | will be autocreated if needed. | ||||||
|  | 
 | ||||||
|  | Subcommands:  | ||||||
|  | 
 | ||||||
|  | hledger pijul record [MSG] - record the journal's files (as listed by 'files') | ||||||
|  | hledger pijul status       - show unrecorded changes (after first record) | ||||||
|  | hledger pijul log          - list the journal's change history (after record) | ||||||
|  | hledger pijul              - show this help | ||||||
|  | 
 | ||||||
|  | Extra arguments are passed to pijul (pijul flags should be preceded by --). | ||||||
|  | You can install these as more convenient top-level commands by creating  | ||||||
|  | hledger-record, hledger-status, hledger-log scripts like: | ||||||
|  | 
 | ||||||
|  |     #!/bin/sh | ||||||
|  |     hledger-pijul record "\$@" | ||||||
|  | 
 | ||||||
|  | EOF | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # * Utils | ||||||
|  | 
 | ||||||
|  | line80() { cat <<EOF | ||||||
|  | -------------------------------------------------------------------------------- | ||||||
|  | EOF | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | no_repo_msg() { | ||||||
|  |     echo "Try this again after 'record'." | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | MAINFILE=$LEDGER_FILE | ||||||
|  | FILES=$(hledger -f "$MAINFILE" files) | ||||||
|  | DIR=$(dirname "$MAINFILE") | ||||||
|  | # pijul executable name, just one word | ||||||
|  | PIJUL=pijul | ||||||
|  | 
 | ||||||
|  | ensure_pijul() { | ||||||
|  |     if ! hash $PIJUL 2>/dev/null; then | ||||||
|  |         cat >&2 <<EOF | ||||||
|  | This command requires '$PIJUL', but it's not installed in \$PATH. | ||||||
|  | Please install it (see https://pijul.org/downloads) and try again. | ||||||
|  | EOF | ||||||
|  |         exit 1 | ||||||
|  |     fi | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # TODO: also look in parent directories | ||||||
|  | ensure_pijul_repo() { | ||||||
|  |     if [[ ! -d "$DIR/.pijul" ]]; then | ||||||
|  |         $PIJUL init "$DIR" | ||||||
|  |         echo "Created pijul repo in $DIR" | ||||||
|  |     fi | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | ensure_pijul_key() { | ||||||
|  |     PIJULDIR=$(pijul_config_dir) | ||||||
|  |     if [[ ! -e $PIJULDIR/publickey.json ]]; then | ||||||
|  |         echo "Creating a pijul commit signing key in $PIJULDIR" | ||||||
|  |         echo "If you set a password below, you'll have to enter it every time you record." | ||||||
|  |         #     -------------------------------------------------------------------------------- | ||||||
|  |         $PIJUL key generate "$USER" | ||||||
|  |     fi | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pijul_config_dir() { | ||||||
|  |     if [[ $(uname) == Darwin ]]; then | ||||||
|  |         echo "$HOME/Library/Application Support/pijul" | ||||||
|  |     else | ||||||
|  |         echo "$HOME/.config/pijul"  # guess | ||||||
|  |     fi | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # * Commands | ||||||
|  | # keep synced with usage() above | ||||||
|  | 
 | ||||||
|  | record() { | ||||||
|  |     ensure_pijul | ||||||
|  |     ensure_pijul_repo | ||||||
|  |     ensure_pijul_key | ||||||
|  |     cd "$DIR" | ||||||
|  |     for F in $FILES; do $PIJUL add -f "$F"; done | ||||||
|  |     MSG=${1:-$(date +'%Y-%m-%d %H:%M:%S %Z')}; shift || true | ||||||
|  |     $PIJUL record -m "$MSG" "$@" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | status() { | ||||||
|  |     ensure_pijul | ||||||
|  |     $PIJUL diff --repository "$DIR" "$@" #| sed -n '/^# Hunks/,$ p' | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | log() { | ||||||
|  |     ensure_pijul | ||||||
|  |     # ensure_pijul_repo | ||||||
|  |     $PIJUL log --repository "$DIR" "$@" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # * Main | ||||||
|  | 
 | ||||||
|  | # NOTE intended to run Commands but will run any function above | ||||||
|  | if declare -f "$1" > /dev/null; then "$@"; else usage; fi | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user