forked from adam000/tilde-slash-bin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-snapshot
More file actions
executable file
·74 lines (59 loc) · 1.75 KB
/
Copy pathgit-snapshot
File metadata and controls
executable file
·74 lines (59 loc) · 1.75 KB
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
#!/bin/bash
# Author: Adam Hintz <adamh.zero@gmail.com>
# Git Snapshot: make a stash of your current status and then immediately apply
# it so you can continue work
# Takes parameters -u, -p, and an optional message (which defaults to 'Snapshot:
# <branchname> <datetime>')
if [[ $# == 1 && ($1 == -h || $1 == --help || $1 == help) ]]; then
echo "Usage:"
echo " git snapshot [-u|-p] [message]"
echo
echo "Options:"
echo " -u Use '--untracked' when stashing"
echo " -p Use '--patch' when stashing"
echo
echo "'Message' is prepended by \"Snapshot:\" or defaults to:"
echo "Snapshot: <branchname> <datetime>"
exit 1
fi
UNTRACKED=0
PATCH=0
if [[ $1 == -u ]]; then
UNTRACKED=1
shift 1
elif [[ $1 == -p ]]; then
PATCH=1
shift 1
fi
SNAPSHOT=""
# If there's something left, it's what we use in the snapshot name
if [[ ! -z "$@" ]]; then
SNAPSHOT="Snapshot: $@"
else
BRANCHNAME=`git rev-parse --abbrev-ref HEAD`
if [[ $BRANCHNAME == HEAD ]]; then
# HEAD is not a suitable differentiator; we're not on a branch. So use
# the short commit hash
BRANCHNAME=`git rev-parse --short HEAD`
fi
# Default to 'Snapshot @ <datetime>'
SNAPSHOT="Snapshot @ `date`"
fi
COMMAND="git stash save"
if [[ $UNTRACKED == 1 ]]; then
COMMAND="$COMMAND -u"
elif [[ $PATCH == 1 ]]; then
COMMAND="$COMMAND -p"
fi
COMMAND="$COMMAND \"$SNAPSHOT\""
OUTPUT=`$COMMAND`
# Unfortunately `git stash save` doesn't return an error if there are no local
# changes to save, so you have to look through the output for a failure and
# fail it yourself.
echo "$OUTPUT" | grep "No local changes to save" > /dev/null 2>&1
if [[ $? != 0 ]]; then
git stash apply
else
echo "No local changes to save; snapshot not saved"
exit 1
fi