Skip to content

Commit

Permalink
Merge pull request #2 from Genzer/dev/0.3.0
Browse files Browse the repository at this point in the history
Dev/0.3.0
  • Loading branch information
Genzer authored Jan 11, 2021
2 parents 03506d4 + 02875bf commit 7e8e1fe
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ The following commands can be used:
_since 0.2.0_
A hook is an executable (a script file or a binary file) which, this is important, that the current user is granted execute permission (e.g `chmod u+x`).
The hook will be called with exactly the parameters passed in from the original `focus.sh` command.
`focus.sh` supports hooks (like Git hooks) which will be called upon some commands get done. The commands at the moment support hooks are:
- `focus to`: the hook defined by an environment variable `FOCUS_TO__HOOK` or default to `$HOME/.focus/hooks/focus_to.sh`.
- `focus done`: the hook defined by an environment variable `FOCUS_DONE__HOOK` or default to `$HOME/.focus/hooks/focus_done.sh`.
| Command | Environment Variable | Default |
| ------------ | -------------------- | ---------------------------------- |
| `focus to` | `FOCUS_TO__HOOK` | `$HOME/.focus/hooks/focus_to.sh` |
| `focus done` | `FOCUS_DONE__HOOK` | `$HOME/.focus/hooks/focus_done.sh` |
| `focus stop` | `FOCUS_STOP__HOOK` | `$HOME/.focus/hooks/focus_stop.sh` |
A hook is an executable (a script file or a binary file) which, this is important, that the current user is granted execute permission (e.g `chmod u+x`).
The hook will be called with exactly the parameters passed in from the original `focus.sh` command.
61 changes: 51 additions & 10 deletions focus.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env bash

[ "$DEBUG" == "1" ] && set -x
set -euo pipefail

[ "${DEBUG:-0}" == "1" ] && set -x

FOCUS_TRACKING_REPO=${FOCUS_TRACKING_REPO:-$HOME/work/focus.sh-tracking}

Expand All @@ -10,8 +12,9 @@ function focus__version() {

function focus__init() {
mkdir -p ${FOCUS_TRACKING_REPO}
__git init
__git switch -c "nothing"
__git init --initial-branch=nothing
__git commit --allow-empty -m 'Empty'
__git branch -c "done"
}

function __git() {
Expand All @@ -28,6 +31,13 @@ function focus__before() {

function focus__done() {
local current_branch="$(focus__now)"

if [[ "${current_branch}" == 'nothing' ]]
then
echo "nothing is done"
return
fi

__git switch "done"
__git merge --no-ff --no-edit "$current_branch"
__git branch -D "${current_branch}"
Expand All @@ -48,26 +58,48 @@ function focus__jot() {
function focus__to() {
local new_topic="${*}"
new_topic=${new_topic// /-}

# This is important as it won't polute the branch /nothing
# with too many commits.
# Previous versions (<= 0.3.0) produced a very long history
# for any new branch.
local current_branch="$(focus__now)"
if [[ "${current_branch}" != 'nothing' ]]
then
focus__jot -m "Switch to [${new_topic}]"
fi

focus__jot -m "Switch to [${new_topic}]"

$(__git show-ref --quiet --verify -- "refs/heads/${new_topic}")
if [ "$?" == "0" ]
# See https://superuser.com/a/940542/627807.
# Bash allows to test a command and use the exit code for the `if`
# statement and bypassing the set -e.
if __git show-ref --quiet --verify -- "refs/heads/${new_topic}"
then
__git switch "${new_topic}"
else
__git switch -c "${new_topic}" "nothing"
__git switch -c "${new_topic}" 'nothing'
focus__jot -m "Started on ${new_topic}"
fi

local hook="${FOCUS_TO__HOOK:-$HOME/.focus/hooks/focus_to.sh}"
[ -f ${hook} ] && ${hook} "${new_topic}"

echo "previous topic $(focus__before)"
if focus__before
then
echo "previous topic $(focus__before)"
else
echo "previous topic: nothing"
fi
}

function focus__ls() {
__git ls
__git log --pretty=format:'%C(green)%h %C(yellow)[%ad] %Creset%s' --decorate --date=relative
}

function focus__stop() {
focus__to 'nothing'
local hook="${FOCUS_STOP__HOOK:-$HOME/.focus/hooks/focus_stop.sh}"
[ -f ${hook} ] && ${hook}

}

function usage() {
Expand All @@ -82,9 +114,15 @@ The following commands can be used:
to SUMMARY_OF_TOPIC
Switch the current focus to another topic.
Hook is supported.
stop
Stop the current focus. Switch back to 'nothing'.
Hook is supported.
done
Mark the current topic as done.
Hook is supported.
all
List all (undone) active focuses.
Expand Down Expand Up @@ -154,6 +192,9 @@ case "$option" in
version)
focus__version
;;
stop)
focus__stop
;;
*)
usage
;;
Expand Down

0 comments on commit 7e8e1fe

Please sign in to comment.