Skip to content

Commit

Permalink
Merge pull request #2 from LrWm3/main
Browse files Browse the repository at this point in the history
allow exec 'i.sh' without sourcing, add command help, introduce optional post-commit hook
  • Loading branch information
kungfusheep authored Sep 10, 2023
2 parents 77b8372 + 5aa2333 commit ebe46f5
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .githooks/post-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
##
# post-commit hook
##

if which i.sh >/dev/null 2>&1; then
commit_message=$(git log -1 --pretty=format:%s)
repo_name=$(basename $(git rev-parse --show-toplevel))
branch_name=$(git rev-parse --abbrev-ref HEAD)

if [ "$repo_name" = "i" ]; then
exit 0
fi

i.sh "[repo:$repo_name] (branch:$branch_name) cmsg: '$commit_message'"
fi
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,26 @@ i git remote add origin git@my-git.lb.local:me/myi.git

```bash
i git push
```
```

## Log commits made in other repositories to i.sh

A post-commit hook provided in `.githooks/post-commit` which will then automatically log commits using `i.sh`.

Commits will then be logged in the following format as they are made:

```
34 minutes ago: [repo:i] (branch:main) cmsg: 'doc: Update README with post-commit hook'
```

You can install it either for a specific repository or globally for all your repositories.

### Log commits for a specific repository

1. Modify your PATH variable to include this repository, or place `i.sh` on your PATH.
2. Copy `.githooks/post-commit` into `.git/hooks`

### Log commits for all repositories

1. Modify your PATH variable to include this repository, or place `i.sh` on your PATH.
2. From the root of this repository, run `git config --global core.hooksPath $PWD/.githooks`
64 changes: 64 additions & 0 deletions i.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,79 @@ function i {
source $I_SOURCE_DIR/i.sh
return;;


"help") # Display help
__i_help
return;;

"--help") # Display help
__i_help
return;;

"-h") # Display help
__i_help
return;;

esac

if [ ! -n "${1}" ]; then
__i_help
return
fi

# add a journal entry
__i_write "$@"
}

# basic help function
function __i_help {
echo "Usage: i [COMMAND|MESSAGE]"
echo ""
echo "COMMANDS:"
echo " amend Overwrite the last message - useful in case of missing info or typos."
echo " list List out the journal."
echo " mentioned List out names mentioned or entries where a specific person is mentioned."
echo " tagged List out tags mentioned or entries where a specific tag is mentioned."
echo " find Generic find for anything."
echo " occurrences Count occurrences of anything."
echo " git Run arbitrary git commands on the 'i' repo."
echo " today View today's journal entries with a special date format, paginated."
echo " yesterday View yesterday's journal entries with a special date format, paginated."
echo " digest Use GPT to summarize the week's activity into a digest."
echo " remember Use GPT to generate a to-do list of tasks that sound outstanding from the previous week."
echo " analyse Run arbitrary GPT analysis commands on a specific time window from the journal."
echo " upgrade Upgrade the 'i' client."
echo " help(-h|--help) Display this help for the 'i' command."
echo ""
echo "By default, if none of the recognized commands are used, a new journal entry is created with the provided message."
echo ""
echo "For more detailed information on each command, look at the source of 'i.sh'."
}

# write a (potentially empty) commit with a message
function __i_write {
HOOKS_PATH="$(git config core.hooksPath)"
git config core.hooksPath .git/hooks
git -C $I_PATH/ commit --allow-empty -qam "$*"
git config core.hooksPath "$HOOKS_PATH"

# If we have a remote, push to it async
if [ -n "$(git -C $I_PATH/ remote show | grep origin)" ]; then
( git -C $I_PATH/ push -u origin main -q > /dev/null 2>&1 & );
fi
}

# amend the previous message
function __i_amend {
HOOKS_PATH="$(git config core.hooksPath)"
git config core.hooksPath .git/hooks
git -C $I_PATH/ commit --allow-empty --amend -qam "$*"
git config core.hooksPath "$HOOKS_PATH"

# If we have a remote, push to it async
if [ -n "$(git -C $I_PATH/ remote show | grep origin)" ]; then
( git -C $I_PATH/ push -u origin main -f -q > /dev/null 2>&1 & );
fi
}

# list the entries in readable format
Expand Down Expand Up @@ -244,3 +303,8 @@ if [ ! -e "$I_PATH" ]; then
git -C $I_PATH/ init -q
__i_write 'created a journal'
fi

# Check if the script is being executed directly. If so, we run i directly
if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
i "$@"
fi

0 comments on commit ebe46f5

Please sign in to comment.