-
Notifications
You must be signed in to change notification settings - Fork 0
/
legit-commit
109 lines (91 loc) · 1.96 KB
/
legit-commit
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
#!/bin/dash
export PATH=$PATH:.
# usage error
if [ $# -lt 2 ]
then
echo "usage: legit-commit [-a] -m 'message'"
exit
fi
# if index is empty then print error then print 'nothing to commit'
if [ ! "$(ls -A .legit/index/)" ]
then
echo "nothing to commit"
exit
fi
# locating most recent commit
found=1
recent=0
while [ $((found)) -eq 1 ]
do
dir=.legit/commit.$recent
if [ -d "$dir" ]
then
recent=$(($recent + 1))
else
found=0
fi
done
recent=$(($recent - 1))
# checks if '-a' is the second argument
if [ "$1" = '-a' ]
then
if [ "$2" != '-m' ]
then
echo "usage: legit-commit [-a] -m 'message'"
exit
fi
# go through files in index
files=.legit/index/*
for file in $files;
do
#split
file_to_add=$(echo "$file" | cut -d'/' -f3)
# call init-add on each files
# ./legit-add "$file_to_add"
legit-add "$file_to_add"
done
# call init-commit: init-commit -m $3
#./legit-commit -m "$3"
legit-commit -m "$3"
exit
fi
# if there is no -a then normal commit will happen which is below
if [ "$1" != '-m' ]
then
echo "usage: legit-commit [-a] -m 'message'"
exit
fi
# 5 represents all items created when legit-init was run + commit.0
if [ "$(ls .legit | wc -l)" -ge 5 ]
then
dir1=.legit/index/
dir2=.legit/commit.$recent/
if [ ! "$(diff -qr $dir1 $dir2)" ]
then
echo "nothing to commit"
exit
fi
fi
# if no errors were detected then commit
message=$2
i=0
present=1
# loop until find next available commit number
while [ $((present)) -eq 1 ]
do
new_commit=.legit/commit.$i
if [ -d "$new_commit" ]
then
i=$(($i + 1))
else
mkdir $new_commit
present=0
fi
done
source=.legit/index/*
for file in $source;
do
cp $file $new_commit
done
echo "Committed as commit $i"
echo "$i $message\n$(cat .legit/log)" > .legit/log