-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush-to-git.sh
executable file
·112 lines (88 loc) · 2.3 KB
/
push-to-git.sh
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
#!/bin/sh
#
# Copyright © 2023 Chee Bin HOH. All rights reserved.
#
# It combines the following steps into one shell script:
# - remove padding space and detect files with tab at start of lines
# - make all to catch build error
# - make clean to remove binary object and executable files
# - clang-format files to be committed (in directory and file extension
# configured via DIRS file)
# - git add changes
# - git commit changes
# - git push changes
oldpwd=$PWD
rootdir=`dirname $0`
if [ $rootdir != "" ]; then
cd $rootdir
fi
make trim-space.out >/dev/null
if [ ! -x trim-space.out ]; then
echo "trim-space.out fails to be built"
exit 1
fi
function source_files
{
IFS_PREV=$IFS
IFS=$'\n'
for l in `cat DIRS`; do
IFS=$IFS_PREV
dir=`echo $l | sed -e 's/\(.*\):.*/\1/g'`
exts=`echo $l | sed -e 's/.*://g'`
cd $dir
for file in $exts; do
echo $dir/$file
done
cd - >/dev/null
done
IFS=$IFS_PREV
}
# extra space following newline is never intended to be checked in, so we trim it.
#
# Tab at the beginning of lines are not consistent cross IDE, it is particular
# annoying for source files saved in visual studio kind of IDE and reopen in
# vi.
echo "Trim space and check tab at the start of lines..."
has_invalid_tab=""
for f in `source_files`; do
if [ ! -x $f ]; then
./trim-space.sh $f
fi
invalid_tab_lines=`sed -n -e '/^\t/=' $f`
if [ "$invalid_tab_lines" != "" ] ; then
has_invalid_tab="yes"
echo Error: $f has invalid tab at beginning of the lines: `echo $invalid_tab_lines | sed -e 's/ /, /g'`
fi
done
if [ "$has_invalid_tab" == "yes" ]; then
exit 1
fi
# test build, we do not want to check in things that break
echo "build check..."
if [ -f CMakeLists.txt ] ; then
cmake .
fi
make >/dev/null || exit 1
# clean up build, we do not want to check in binary
echo "clean up build..."
make clean >/dev/null
# clang-format the source files
if which clang-format &>/dev/null; then
echo "perform clang-format..."
for f in `source_files`; do
clang-format ${f} > ${f}_tmp
if ! diff $f ${f}_tmp &>/dev/null; then
cp ${f}_tmp ${f}
fi
rm ${f}_tmp
done
fi
# git activities
echo "git add, commit and push..."
echo
git add .
git commit -m "${1:-"no comment"}"
git push origin master --force
if [ $rootdir != "" ]; then
cd $oldpwd;
fi