-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab2github.sh
More file actions
executable file
·91 lines (72 loc) · 2.7 KB
/
Copy pathgitlab2github.sh
File metadata and controls
executable file
·91 lines (72 loc) · 2.7 KB
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
#!/bin/bash
set -euo pipefail
umask 077
install_gh() {
(type -p wget >/dev/null || (sudo apt update && sudo apt install wget -y)) \
&& sudo mkdir -p -m 755 /etc/apt/keyrings \
&& out=$(mktemp) && wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg \
&& cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
&& sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
&& sudo mkdir -p -m 755 /etc/apt/sources.list.d \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y
}
install_git() {
sudo apt update && sudo apt install git
}
check_tool() {
# Source - https://stackoverflow.com/a/677212
# Posted by lhunath, modified by community. See post 'Timeline' for change history
# Retrieved 2026-08-31, License - CC BY-SA 4.0
command -v $1 >/dev/null 2>&1 || { echo >&2 "I require $1 but it's not installed. I will install it for the user"; install_$1; }
}
# Check if user is root, if not ask him to run the script with sudo
if ! [ $(id -u) = 0 ]; then
echo "You must be root to run this script, use 'sudo ./gitlab2github.sh'"
exit 1
fi
# Check if all tools necesarry are available, if not install them for the user.
check_tool git
check_tool gh
# Source - https://stackoverflow.com/q/638975
# Posted by Bill the Lizard, modified by community. See post 'Timeline' for change history
# Retrieved 2026-08-31, License - CC BY-SA 4.0
# Check if environment file with credentials exists
ENV_FILE=./.env
if [ -f $ENV_FILE ]; then
source $ENV_FILE
else
echo "File $ENV_FILE does not exist. Please create it using the instructions"
exit 1
fi
# Check if csv file with repos exists
CSV_FILE=repos.csv
if [ ! -f $CSV_FILE ]; then
echo "File $CSV_FILE does not exist. Please create it using the instructions"
exit 1
fi
gh auth setup-git
# Loop over the projects
while IFS="," read -r OLD_REPO NEW_REPO
do
REPO_NO_SCHEME="${OLD_REPO#https://}"
CLONE_URL="https://${GITLAB_USERNAME}:${GITLAB_TOKEN}@${REPO_NO_SCHEME}"
REPO_NAME="$(basename "$REPO_NO_SCHEME" .git)"
echo "Cloning $OLD_REPO ..."
if ! git clone "$CLONE_URL"; then
echo "Failed to clone $OLD_REPO, skipping."
continue
fi
cd "$REPO_NAME"
echo "Creating GitHub repo $NEW_REPO and pushing $REPO_NAME into it..."
if ! gh repo create "$NEW_REPO" --private --source=. --remote=github --push; then
echo "Failed to create/push $NEW_REPO, skipping."
cd "$BASE_DIR"
continue
fi
git push github --all
git push github --tags
cd ..
sudo rm -rf $REPO_NAME
done < <(tail -n +2 $CSV_FILE)