-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitdumper.sh
165 lines (134 loc) · 4.39 KB
/
gitdumper.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
#$1 : URL to download .git from (http://target.com/.git/)
#$2 : Folder where the .git-directory will be created
function init_header() {
cat <<EOF
#########################################################################
# Sebagian Tools Masih Tahap Pengembangan jika ada Bug Silakan laporkan #
# Developed and maintained by @Huda from @Hakka.id #
# Contant: +62 85335822427 #
#########################################################################
EOF
}
# get_git_dir "$@" for "--git-dir=asd"
# returns asd in GITDIR
function get_git_dir() {
local FLAG="--git-dir="
local ARGS=${@}
for arg in $ARGS
do
if [[ $arg == $FLAG* ]]; then
echo "${arg#$FLAG}"
return
fi
done
echo ".git"
}
init_header
QUEUE=();
DOWNLOADED=();
BASEURL="$1";
BASEDIR="$2";
GITDIR=$(get_git_dir "$@")
BASEGITDIR="$BASEDIR/$GITDIR/";
if [ $# -lt 2 ]; then
echo -e "\033[33m[*] USAGE: http://target.tld/.git/ dest-dir [--git-dir=otherdir]\033[0m";
echo -e "\t\t--git-dir=otherdir\t\tChange the git folder name. Default: .git"
exit 1;
fi
if [[ ! "$BASEURL" =~ /$GITDIR/$ ]]; then
echo -e "\033[31m[-] /$GITDIR/ missing in url\033[0m";
exit 0;
fi
if [ ! -d "$BASEGITDIR" ]; then
echo -e "\033[33m[*] Destination folder does not exist\033[0m";
echo -e "\033[32m[+] Creating $BASEGITDIR\033[0m";
mkdir -p "$BASEGITDIR";
fi
function start_download() {
#Add initial/static git files
QUEUE+=('HEAD')
QUEUE+=('objects/info/packs')
QUEUE+=('description')
QUEUE+=('config')
QUEUE+=('COMMIT_EDITMSG')
QUEUE+=('index')
QUEUE+=('packed-refs')
QUEUE+=('refs/heads/master')
QUEUE+=('refs/remotes/origin/HEAD')
QUEUE+=('refs/stash')
QUEUE+=('logs/HEAD')
QUEUE+=('logs/refs/heads/master')
QUEUE+=('logs/refs/remotes/origin/HEAD')
QUEUE+=('info/refs')
QUEUE+=('info/exclude')
QUEUE+=('/refs/wip/index/refs/heads/master')
QUEUE+=('/refs/wip/wtree/refs/heads/master')
#Iterate through QUEUE until there are no more files to download
while [ ${#QUEUE[*]} -gt 0 ]
do
download_item ${QUEUE[@]:0:1}
#Remove item from QUEUE
QUEUE=( "${QUEUE[@]:1}" )
done
}
function download_item() {
local objname=$1
local url="$BASEURL$objname"
local hashes=()
local packs=()
#Check if file has already been downloaded
if [[ " ${DOWNLOADED[@]} " =~ " ${objname} " ]]; then
return
fi
local target="$BASEGITDIR$objname"
#Create folder
if dir=$(echo "$objname" | grep -oE "^(.*)/"); then
mkdir -p "$BASEGITDIR/$dir"
fi
#Download file
curl -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" -f -k -s "$url" -o "$target"
#Mark as downloaded and remove it from the queue
DOWNLOADED+=("$objname")
if [ ! -f "$target" ]; then
echo -e "\033[31m[-] Downloaded: $objname\033[0m"
return
fi
echo -e "\033[32m[+] Downloaded: $objname\033[0m"
#Check if we have an object hash
if [[ "$objname" =~ /[a-f0-9]{2}/[a-f0-9]{38} ]]; then
#Switch into $BASEDIR and save current working directory
cwd=$(pwd)
cd "$BASEDIR"
#Restore hash from $objectname
hash=$(echo "$objname" | sed -e 's~objects~~g' | sed -e 's~/~~g')
#Check if it's valid git object
if ! type=$(git cat-file -t "$hash" 2> /dev/null); then
#Delete invalid file
cd "$cwd"
rm "$target"
return
fi
#Parse output of git cat-file -p $hash. Use strings for blobs
if [[ "$type" != "blob" ]]; then
hashes+=($(git cat-file -p "$hash" | grep -oE "([a-f0-9]{40})"))
else
hashes+=($(git cat-file -p "$hash" | strings -a | grep -oE "([a-f0-9]{40})"))
fi
cd "$cwd"
fi
#Parse file for other objects
hashes+=($(cat "$target" | strings -a | grep -oE "([a-f0-9]{40})"))
for hash in ${hashes[*]}
do
QUEUE+=("objects/${hash:0:2}/${hash:2}")
done
#Parse file for packs
packs+=($(cat "$target" | strings -a | grep -oE "(pack\-[a-f0-9]{40})"))
for pack in ${packs[*]}
do
QUEUE+=("objects/pack/$pack.pack")
QUEUE+=("objects/pack/$pack.idx")
done
}
start_download