-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdfm
121 lines (102 loc) · 3.48 KB
/
sdfm
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# simple dot files manager
[ -n "$SDFM_GIT_DIR" ] || SDFM_GIT_DIR="$HOME/.local/state/sdfm"
[ -n "$SDFM_GIT_WORKTREE" ] || SDFM_GIT_WORKTREE="$HOME"
[ -n "$SDFM_GIT_REMOTE" ] || SDFM_GIT_REMOTE='origin'
[ -n "$SDFM_GIT_BRANCH" ] || SDFM_GIT_BRANCH='master'
[ -n "$SDFM_GIT_COMMAND" ] || \
SDFM_GIT_COMMAND='git --git-dir="$SDFM_GIT_DIR" --work-tree="$SDFM_GIT_WORKTREE"'
alias sdfm="$SDFM_GIT_COMMAND"
alias sdfc="$SDFM_GIT_COMMAND commit"
alias sdfs="$SDFM_GIT_COMMAND stat"
alias sdfp="$SDFM_GIT_COMMAND push"
alias sdfpl="$SDFM_GIT_COMMAND pull --autostash"
alias sdfl="$SDFM_GIT_COMMAND log --date=human --abbrev-commit"
sdfi() {
[ -d "${SDFM_GIT_DIR%/*}" ] || {
mkdir -pv "${SDFM_GIT_DIR%/*}" || return "$?"
}
[ -d "${SDFM_GIT_WORKTREE%/*}" ] || {
mkdir -pv "${SDFM_GIT_WORKTREE%/*}" || return "$?"
}
local non_checkout_message='! echo "No tracked file, use -a or -p flag to checkout file"'
[ -d "$SDFM_GIT_DIR" ] || {
sdfm init -b "$SDFM_GIT_BRANCH" || return "$?"
sdfm config status.showUntrackedFiles no || return "$?"
sdfm config push.autoSetupRemote true || return "$?"
sdfm config alias.stat $non_checkout_message || return "$?"
}
local config_abbrev="$(sdfm config core.abbrev)"
while getopts :u:fap:r option; do
case $option in
u)
[ -n "$(sdfm remote show)" ] || {
sdfm remote add "$SDFM_GIT_REMOTE" "$OPTARG" || return "$?"
echo "Remote $SDFM_GIT_REMOTE added"
}
;;
f)
echo 'Fetching...'
sdfm fetch --depth=1 --set-upstream \
"$SDFM_GIT_REMOTE" "$SDFM_GIT_BRANCH" || return "$?"
sdfm reset -q "$SDFM_GIT_REMOTE"/"$SDFM_GIT_BRANCH" || return "$?"
sdfm diff --name-status --diff-filter=M HEAD || return "$?"
;;
a)
if [ -z "$config_abbrev" ]; then
sdfm config core.abbrev auto || return "$?"
config_abbrev="$(sdfm config core.abbrev)"
sdfm config alias.stat 'status -s' || return "$?"
sdfm checkout -- . || return "$?"
echo 'Last commit applied'
elif [ "$config_abbrev" = auto ]; then
echo 'Commit already applied'
return 1
else
printf '%s\n' \
'Already in partial checkout state.' \
'Use -r to reset state and remove tracked file.'
fi
;;
p)
# can checkout multiple files.
if [ -z "$config_abbrev" ]; then
sdfm config core.abbrev no
config_abbrev="$(sdfm config core.abbrev)"
sdfm config alias.stat 'diff --name-status --diff-filter=M HEAD'
fi
if [ "$config_abbrev" = no ]; then
sdfm checkout -- "$OPTARG" || return "$?"
echo "Checkout $OPTARG"
else
printf '%s\n' \
'Already in full checkout state.' \
'Use -r to reset state and remove tracked file.'
return 1
fi
;;
r)
echo 'Reset state and remove all tracked files'
sdfm read-tree -u -m "$(sdfm hash-object -t tree /dev/null)" || return "$1"
sdfm reset -q
sdfm config unset core.abbrev
config_abbrev="$(sdfm config core.abbrev)"
sdfm config alias.stat $non_checkout_message
;;
:)
echo "Option -$OPTARG requires an argument"
return 1
;;
?)
echo 'Usage: sdfi [-u <url>] [-f] [-a] [-p <path>] [-r]'
return 1
;;
esac
done
}
sdfa() {
sdfm add "$@"
}
sdfd() {
sdfm diff "$@"
}
# vim:ft=sh