-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitrim.sh
executable file
·145 lines (126 loc) · 3.44 KB
/
gitrim.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
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
#!/bin/bash
# Trim unncecessary files from git repository
# Credits to various people over at stackoverflow
CWD=$PWD
# Functions
# Changes to repo provided by user
function sel_repo
{
echo -en " \u001b[32;1m\u001b[4m Enter full path to repository :\u001b[0m "
read -r repo_dir
cd "$repo_dir" || return
echo "$PWD"
}
# Makes sure all branches are up-to-date
function update_branch
{
for branch in $(git branch -a | grep remotes | grep -v HEAD | grep -v master); do
git branch --track "${branch##*/}" "$branch"
done
}
# Requires dust or du for functionality
function list_largest
{
echo -en " \u001b[32;1m\u001b[4mEnter directory depth :\u001b[0m "
read -r depth
if hash dust 2>/dev/null; then
dust -d depth
else
du -h --max-depth="$depth" path/to/directory
fi
}
# Trims file from git history
function trim_file
{
echo -en " \u001b[32;1m\u001b[4mEnter file path to trim :\u001b[0m "
read -r filepath
git filter-branch --tag-name-filter cat --index-filter \
"git rm -r --cached --ignore-unmatch $filepath" --prune-empty -f -- --all
}
# Remove trimmed files from local repo.
function clean_files
{
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
}
# Push changed repo to remote
function push_change
{
git push origin --force --all
git push origin --force --tag
echo -e "\u001b[31;1m Warning! Must use rebase or fresh clone on other local repos. \u001b[0m"
}
# Clean up and exit
function bye
{
cd "$CWD" || return
clear
exit
}
# Infinite loop for menu
while true
do
clear
# Draw UI, ANSI Escape sequences used. Edit carefully!
echo
echo -e " \e[7m Gitrim - Trim Git Repository \e[0m"
echo -e " \u001b[34;1m (1) Select Repository (2) Update Branches \u001b[0m"
echo -e " \u001b[34;1m (3) List Largest Files (4) Trim Files \u001b[0m"
echo -e " \u001b[34;1m (5) Clean Local Repo (6) Push Changes \u001b[0m"
echo -e " \u001b[31;1m (0) Quit \u001b[0m"
echo
echo -e "\e[1m For best results use the options sequentially per repository.\e[0m"
echo -en " \u001b[32;1m\u001b[4mSelect Option :\u001b[0m "
# Save entered number
read -r choice
# Run appropriate choice
case "$choice" in
1 )
sel_repo
echo
echo -e " \e[42m Changed repository. \e[0m"
# wait for input, e.g. by pressing ENTER:
read -r
;;
2 )
update_branch
echo
echo -e " \e[42m All branches up-to-date. \e[0m"
read -r
;;
3 )
list_largest
echo
echo -e " \e[42m Largest files listed. \e[0m"
read -r
;;
4 )
trim_file
echo
echo -e " \e[42m Selected files trimmed. \e[0m"
read -r
;;
5 )
clean_files
echo
echo -e " \e[42m Trimmed files cleaned from local repository. \e[0m"
read -r
;;
6 )
push_change
echo
echo -e " \e[42m Changes pushed to remote. \e[0m"
read -r
;;
0 )
bye
;;
* )
echo -e " \e[41m Wrong option \e[0m"
echo -e " Please try again... "
sleep 1
;;
esac
done