-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepo-clone
More file actions
executable file
·134 lines (118 loc) · 3.73 KB
/
Copy pathrepo-clone
File metadata and controls
executable file
·134 lines (118 loc) · 3.73 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
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
#!/bin/bash
# **Repo Clone Tool**
#
# *Usage*: `./repo-clone <url>`
#
# This script can be *sourced* or *executed* directly.
#
# It is handy when often copy&paste links from e.g. github/gitlab,
# and cloning many repositories on harddrive,
# to clone them organized under $YOUR_REPOS_DIR/$USERNAME/$REPO name.
#
# Examples:
# repo-clone git@server:username/reponame
# repo-clone https://server/username/reponame.git
# repo-clone https://server/username/reponame
# repo-clone https://huggingface.co/TheBloke/wizard-mega-13B-GPTQ
#
# When sourced, call the function:
# repo_clone <url>
# Function to check if the url is from huggingface.co
is_it_huggingface() {
local url="$1"
[[ "$url" == "https://huggingface.co/"* ]]
}
# Function to clone the huggingface repository
clone_huggingface() {
local url="$1"
# Remove leading https://huggingface.co/
url="${url#https://huggingface.co/}"
IFS='/' read -r -a array <<< "$url"
local username="${array[0]}"
local repo="${array[1]%.git}"
set -x
mkdir -p "$HOME/huggingface.co/${username}" && cd "$HOME/huggingface.co/${username}" && git lfs install && \
git clone "https://huggingface.co/${username}/${repo}"
}
# Function to parse the git url and return hosting shortcode, git hosting, username, and repository name
parse_git_url() {
local url="$1"
local is_ssh=false
local git_hosting
local hosting_shortcode
local username
local repo
# Check if the URL has the SSH format
if [[ "$url" =~ "@" ]]; then
is_ssh=true
IFS=':' read -r -a array <<< "$url"
git_hosting="${array[0]#*@}"
IFS='/' read -r -a array <<< "${array[1]}"
else
url="${url#http://}"
url="${url#https://}"
IFS='/' read -r -a array <<< "$url"
git_hosting="${array[0]}"
array=("${array[@]:1}")
fi
hosting_shortcode="${git_hosting%%.*}"
username="${array[0]}"
repo="${array[1]%.git}"
echo "$hosting_shortcode" "$git_hosting" "$username" "$repo" "$is_ssh"
}
# Function to create/update the directory and clone or pull the repository
clone_repo() {
local hosting_shortcode="$1"
local username="$2"
local repo="$3"
local clone_url="$4"
set -x
local repo_path="$HOME/$hosting_shortcode/$username/$repo"
if [ -d "$repo_path" ]; then
# If repository already exists, let's fetch and optionally pull
cd "$repo_path"
git fetch
local changes
changes="$(git status --porcelain)"
if [ -z "$changes" ]; then
git pull
else
echo "Local changes found. Please commit or stash them before pulling."
fi
else
mkdir -p "$HOME/$hosting_shortcode/$username"
cd "$HOME/$hosting_shortcode/$username"
git clone "$clone_url"
fi
}
# The core function that can be called when the script is sourced
repo_clone() {
# Check if the url is from huggingface.co
if is_it_huggingface "$1"; then
clone_huggingface "$1"
return
fi
# Otherwise, parse the git url
read -r hosting_shortcode git_hosting username repo is_ssh <<< "$(parse_git_url "$1")"
# Create the clone url
local clone_url
if [[ "$is_ssh" = "true" ]]; then
clone_url="git@$git_hosting:$username/$repo.git"
else
clone_url="https://$git_hosting/$username/$repo.git"
fi
# Clone the repository
clone_repo "$hosting_shortcode" "$username" "$repo" "$clone_url"
}
# Main entry point (only invokes the function if executed directly)
main() {
if [[ -z "$1" ]]; then
echo "Usage: repo-clone <url>"
return 1
fi
repo_clone "$@"
}
# Check if the script is being sourced or executed
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi