-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-lfs-mirror
executable file
·146 lines (120 loc) · 4.17 KB
/
git-lfs-mirror
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
#!/bin/bash
#
# git-mirror
# https://github.com/justinethier/git-mirror
#
# Copyright (c) 2022, Justin Ethier
# All rights reserved.
#
#
# Create our initial mirror repo
#
init() {
REMOTE_REPO=`echo "$1" | cut -d"/" -f 5`
SERVER=`echo "$2" | cut -d"/" -f 1 | cut -d"@" -f 2 | cut -d":" -f 1`
ORG=`echo "$2" | cut -d"/" -f 1 | cut -d"@" -f 2 | cut -d":" -f 2`
REPO=`echo "$2" | cut -d"/" -f 2`
echo "mirror to: $SERVER $ORG $REPO"
git clone --bare $1
cd $REMOTE_REPO
git lfs fetch --all
git config lfs.https://$SERVER/$ORG/$REPO/info/lfs.locksverify true
git remote set-url origin $2
git lfs push --all
git lfs push --all git@$2
git lfs push --all origin
git push --mirror
}
#
# Create a clone of our mirrored repo
#
clone() {
REMOTE_REPO=`echo "$1" | cut -d"/" -f 5`
SERVER=`echo "$2" | cut -d"/" -f 1 | cut -d"@" -f 2 | cut -d":" -f 1`
ORG=`echo "$2" | cut -d"/" -f 1 | cut -d"@" -f 2 | cut -d":" -f 2`
REPO=`echo "$2" | cut -d"/" -f 2`
DIR=`echo "$REPO" | cut -d"." -f 1`
# Clone a local copy of our mirror and get assets from LFS
git clone $2
cd $DIR
git lfs fetch --all
# Fetch upstream changes including LFS
git remote add upstream $1
git remote update upstream
git lfs fetch upstream --all
# Setup our local git to track all branches so we can sync them
# Solution from: https://stackoverflow.com/a/4754797/101258
#for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
for branch in $(git for-each-ref --format='%(refname)' refs/remotes/upstream | cut -d'/' -f 4); do
git branch --track "${branch##*/}" "$branch"
done
git fetch --all
# Finally, push everything to our local git mirror
git push origin --all
git push origin --tags
}
#
# Update our mirrored repo with changes from upstream
#
update() {
# Clone a local copy of our mirror and get assets from LFS
git lfs fetch --all
## Fetch upstream changes including LFS
git remote update upstream
git lfs fetch upstream --all
# Setup our local git to track all branches so we can sync them
# Solution from: https://stackoverflow.com/a/4754797/101258
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
#for branch in $(git for-each-ref --format='%(refname)' refs/remotes/upstream | cut -d'/' -f 4); do
echo "Track $branch"
git branch --track "${branch##*/}" "$branch"
done
for branch in $(git for-each-ref --format='%(refname)' refs/heads/ | cut -d"/" -f 3); do
#for branch in $(git branch); do
# Attempt to pick up changes across all branches
echo "Merge $branch"
git checkout "$branch"
git pull
git merge upstream/"$branch"
done
git fetch --all
## Finally, push everything to our local git mirror
git push origin --all
git push origin --tags
}
case "$1" in
init)
init $2 $3
;;
clone)
clone $2 $3
;;
update)
update
;;
*)
cat << EOF
Usage: $0 command args
The following commands are provided:
init REMOTE_REPO SERVER
Run the init command to create the initial mirror of a repository, and push
that mirror to the specified server.
This command will create a bare repository on your local machine. This is
not suitable for check outs so once the repository is created you will
probably want to delete the directory and check our your mirror again
using the clone command.
For example:
git-lfs-mirror init https://github.com/justinethier/git-mirror.git git@github.com:your-account/test.git
clone REMOTE_REPO SERVER
Clone will create a local clone of your mirrored repository and setup the
local repository to track all upstream branches so we can sync them.
For example:
git-lfs-mirror clone https://github.com/justinethier/git-mirror.git git@github.com:your-account/test.git
update
Update our mirrored repository with changes from upstream.
This includes updates to all tracked branches.
For example:
git-lfs-mirror update
EOF
exit 1
esac