-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshrug-commit
More file actions
executable file
·180 lines (155 loc) · 4.49 KB
/
shrug-commit
File metadata and controls
executable file
·180 lines (155 loc) · 4.49 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/dash
#TODO: handle -a and -m together?
export PATH="$PATH:/tmp_amd/reed/export/reed/1/z5232937/2041/ass1" # change to pwd?
shrug=".shrug"
stage=".stage"
commit=".commit"
index=".index"
cfile=".shrug/commit_num.txt"
# check if .shrug exists
if [ -d "$shrug" ]
then
add_all=0
a_flag=0
m_flag=0
# f is an internal, hidden option to force commit
while getopts "afm:" options; do
case "$options" in
a) add_all=1
ignore=1
a_flag=1
;;
f) add_all=0
ignore=1
;;
m) commit_msg="$OPTARG"
ignore=0
add_all=0
m_flag=1
;;
*) add_all=0
ignore=0
;;
esac
done
# if -a and -m
if [ $a_flag -eq 1 ] && [ $m_flag -eq 1 ]
then
# -a values takes precedence over -m values
add_all=1
ignore=1
fi
if [ $add_all -eq 1 ]
then
# -a option present
# shrug-add all files in current dir, then shrug-commit
dir=$(pwd)
for file in "$dir"/*
do
shrug-add "$file"
done
# commit regardless of no change in files
shrug-commit -f "$commit_msg"
else
if [ -d "$shrug/$commit" ]
then
# copy contents of file into index
:
else
# mkdir and then copy contents into index
mkdir "$shrug/$commit"
fi
if [ -f $cfile ]
then
# overrite index number to existing value +1
cval=$(cat "$cfile")
cval=$(( cval + 1 ))
echo $cval > $cfile
else
# commit is 0
touch $cfile
echo "0" > $cfile
cval=0
fi
commit_subdir="${shrug}/${commit}/${cval}"
mkdir $commit_subdir
cmsgfile="${commit_subdir}/commit_msg.txt"
# save a copy of ALL files in .index/.stage to .commit repo
flag=0
stage_files="${shrug}/${index}/${stage}"
for file in "$stage_files"/*
do
cp "$file" $commit_subdir
touch $cmsgfile
echo "$commit_msg" > $cmsgfile
flag=1
done
# check if there is a need to commit or files have been unchanged from stage (or last commit?) or dont exist
if ([ $cval -ge 1 ] && [ $ignore -eq 0 ])
then
old_cval=$(( cval - 1))
diff_res=$(diff -b "$commit_subdir/list.txt" "$shrug/$commit/$old_cval/list.txt")
if [ -z "$diff_res" ]
then
echo "nothing to commit" >& 2
# remove latest commit and rst cval
rm -r $commit_subdir
if [ -f $cfile ]
then
# overrite index number to existing value -1
o_cval=$(cat "$cfile")
if [ $o_cval -gt 0 ]
then
o_cval=$(( o_cval - 1 ))
echo $o_cval > $cfile
fi
else
# commit is 0
touch $cfile
echo "0" > $cfile
cval=0
fi
flag=0
exit 0
fi
# if nothing is staged
elif ([ ! -d $stage_files ] && [ $ignore -eq 0 ])
then
echo "nothing to commit" >& 2
# remove latest commit
rm -r $commit_subdir
if [ -f $cfile ]
then
# overrite index number to existing value -1
o_cval=$(cat "$cfile")
if [ $o_cval -gt 0 ]
then
o_cval=$(( o_cval - 1 ))
echo $cval > $cfile
fi
else
# commit is 0
touch $cfile
echo "0" > $cfile
cval=0
fi
flag=0
exit 0
else
:
fi
if [ $flag -eq 1 ]
then
echo "Committed as commit ${cval}"
if [ -f "$shrug/to_commit.txt" ]
then
rm -f "$shrug/to_commit.txt"
fi
else
:
fi
fi
else
echo "shrug-commit: error: no .shrug directory containing shrug repository exists" >&2
exit 1
fi