-
Notifications
You must be signed in to change notification settings - Fork 207
/
clean-git.sh
89 lines (76 loc) · 1.92 KB
/
clean-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
#!/bin/bash
if [ ! -d '.git' ];then
echo "$(pwd) not a git repository"
exit -1
fi
git status | grep 'nothing to commit' > /dev/null
if [ $? != 0 ];then
echo "Cannot clean: You have unstaged changes."
exit -1
fi
IFS=$'\n'
#prepare delete directory
PRE_DEL_DIR_ARR=('DevSample' 'Sample' 'fastdex-build' 'buildSrc' 'app' 'sample')
#return: 1: yes 0: no
is_mapping() {
if [ "$1" == '' ];then
return 0
fi
for pattern in ${PRE_DEL_DIR_ARR[@]}
do
echo $1 | grep "^${pattern}" > /dev/null
if [ $? == 0 ];then
return 1
fi
done
return 0
}
processed_count=0
for row in $(git rev-list --objects --all)
do
obj_hash=$(echo ${row} | awk '{print $1}')
relative_path=$(echo ${row} | awk '{print $2}')
is_mapping ${relative_path}
if [ $? == 1 ];then
echo "process: ${relative_path}"
#remove object
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch ${relative_path}" --prune-empty --tag-name-filter cat -- --all
if [ $? == 0 ];then
let processed_count=processed_count+1
fi
else
if [ "${relative_path}" == '' ];then
echo "relative path is empty,just ignore. hash value: ${obj_hash}"
else
echo "skip ${relative_path}"
fi
if [ "${relative_path}" != '' ];then
echo "skip ${relative_path}"
fi
fi
done
echo "processed count: ${processed_count}"
if [ $processed_count > 0 ];then
if [ ! -f '.gitignore' ];then
touch .gitignore
fi
echo '' >> .gitignore
for pattern in ${PRE_DEL_DIR_ARR[@]}
do
echo .gitignore | grep "^${pattern}$" > /dev/null
if [ $? != 0 ];then
echo $pattern >> .gitignore
fi
echo .gitignore | grep "^${pattern}/$" > /dev/null
if [ $? != 0 ];then
echo "${pattern}/" >> .gitignore
fi
done
echo ''
echo '=========Execute the following command to complete the cleanup========='
echo "git add ."
echo "git commit -m 'clean git repository'"
echo "git push origin --force --all"
echo "git push origin --force --tags"
echo '=================='
fi