aboutsummaryrefslogtreecommitdiff
path: root/task-git.sh
blob: f9c26affebe996db3dd3138db6031e29c44d25f4 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash

LOGFILE=/dev/null

# Get task command
TASK_COMMAND="task ${@}"

# Get data dir
DATA_RC=$(task _show | grep data.location)
DATA=(${DATA_RC//=/ })
DATA_DIR=${DATA[1]}

# Need to expand home dir ~
eval DATA_DIR=$DATA_DIR

pushd $DATA_DIR > $LOGFILE
# Function for make a commit
make_commit() {
   git add .  > $LOGFILE
   git commit -m "$TASK_COMMAND" > $LOGFILE
   echo "Committed"
}


# Exit if we don't have a tasks data directory
if [ ! -e "$DATA_DIR" ]; then
    echo "Could not load data directory $DATA_DIR."
    exit 1
fi

# Check if git repo exists
if ! [ -d "$DATA_DIR/.git" ]; then
    echo "Initializing git repo"
    pushd $DATA_DIR
    git init
    git add *
    git commit -m "Initial Commit"
    popd
fi

# Push by default
PUSH=0
PULL=0
COMMIT=0


# Check if we are passing something that doesn't do any modifications
for i in $1
do
    case $i in
	    add|append|completed|delete|done|due|duplicate|edit|end|modify|prepend|rm|start|stop)
	    make_commit
            ;;
        push)
            echo "Push"
	    PUSH=1
            ;;
        pull)
            echo "Pull"
            PULL=1
            ;;
        *)
            PUSH=0
            ;;
    esac
done



# Check if we have a place to push to
GIT_REMOTE=$(git remote -v | grep push | grep origin | awk '{print $2}')
if [ -z $GIT_REMOTE ]; then
    # No place to push to
    PUSH=0
fi

if [ "$PULL" == 1 ]; then
    echo "Fetching & Applying updates from $GIT_REMOTE"
    git fetch && git pull
    exit 0
fi


# Push
if [ "$PUSH" == 1 ]; then
    echo "Pushing updates to $GIT_REMOTE"
    git push origin master > $LOGFILE
fi

# Call task
/usr/bin/task $@

popd > $LOGFILE

exit 0