aboutsummaryrefslogtreecommitdiffstats
path: root/future-me.sh
blob: 8da9406facb4b0991d79b150e7c80a5ce758ff38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
set -x

# First of all as we are using git update-index and don't want to mess arround with saving the current state
# We check if there is any staged files
check-git-status() {
    git status --porcelain | grep -e ^A -e ^M && echo "You first need to clean you git staging status to use mr-todo" && exit 1
}

create-new-bug() {
    readonly bug="$1"
    last_ref=$(git show-ref refs/notes/devtools/mr-todo | cut -d ' ' -f 1)
    echo $last_ref
    if [ -n "$last_ref" ]
    then
        git read-tree $last_ref
    else
	git read-tree --empty
    fi

    object_id=$(echo "$1" | git hash-object -w --stdin)
    echo $object_id
    file_path=${object_id:0:2}/${object_id:2}
    git update-index --add --cacheinfo 100644 $object_id $file_path
    tree_id=$(git write-tree)
    files_to_unstage=$(git update-index --refresh | cut -d ' ' -f 1 | cut -d ':' -f 1)
    git update-index --remove $files_to_unstage
    if [ -n "$last_ref" ]
    then
        add_parent="$last_ref"
    	commit_id=$(echo 'mr-todo: created a new bug for you' | git commit-tree $tree_id -p $add_parent)
    else
    	commit_id=$(echo 'mr-todo: created a new bug for you' | git commit-tree $tree_id)
    fi
    git update-ref refs/notes/devtools/mr-todo $commit_id
}

show-bugs() {
    #git ls-tree --full-tree -r  refs/notes/devtools/mr-todo
    for i in $(git ls-tree --full-tree -r  refs/notes/devtools/mr-todo | cut -d ' ' -f 3 | cut  -f 1 | tr '\n' ' '); do git cat-file -p $i ; done
}

main() {
    readonly command="$1"
    readonly payload="${*:2}"
    case $command in
        new)
            echo create new bug "$payload" #"${*:2}"
	    check-git-status
	    last_tree="$(git --no-pager log -1 --format="%H" | tr --delete '\n')"
	    echo $last_tree
	    create-new-bug "$payload"
	    git read-tree "$last_tree"
	;;
        show)
	    echo show all bugs
	    show-bugs
	;;
        *)
	    echo unknown command "$@"
	    ;;
    esac
}

main "$@"