-
Notifications
You must be signed in to change notification settings - Fork 1
/
hg2git.sh
executable file
·316 lines (297 loc) · 11.9 KB
/
hg2git.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/bash
# Author: K. Gofron
# Date: 2020-2-12
# Source: https://github.com/kgofron/hg2git.git
# EXAMPLE: Convert local hg repository, with autosave for pmac motion controller,
# with gitlab repo existing (overwrite)
# hg2git.sh -r . -as pAS --force
# hg2git.sh -r . -as pAS -url "https://gitlab.nsls2.bnl.gov/xf/10id/iocs/xf10idd-ioc1/" --force
# hg2git.sh -r . -D7 -as pAS -u softioc -url https://github.com/kgofron/ --force
# hg2git/hg2git.sh -r . -D7 -as cAS -u softioc -url https://gitlab.nsls2.bnl.gov/xf/10id/iocs/xf10idd-ioc1/ --force
# hg2git/hg2git.sh -r . -b -D7 -as cAS -u softioc -url https://gitlab.nsls2.bnl.gov/xf/10id/iocs/xf10idd-ioc1/ --force
FST_EXPRT="/tmp/fast-export" # fast-export clone directory
REPO="" # Repository name
#PFX="hg2git"
#SFX_STATE="state"
GFI_OPTS="" # Git flags such as --force, ...
C_OPT="" # fast-export release, Deb7 requires v160914, but v180317 works as well.
AS_OPTS="" # AutoSave options
B_OPTS="--all" # Branch push options {active| --all}
# Get directory/folder
DIR_S=$(dirname "$PWD")
REPO_S=$(basename "$PWD")
BRANCH=$(hg branch)
FE_OPTS="" # Clone fast-export
echo "Current directory and repo=$DIR_S, $REPO_S"
#GIT_URL="https://github.com/kgofron/"
#GIT_URL="https://gitlab.nsls2.bnl.gov/xf/10id/iocs/xf10idd-ioc1/"
GIT_URL="https://gitlab.nsls2.bnl.gov/xf/11id/iocs/xf11idb-ioc1/"
GIT_REPO="$GIT_URL$REPO_S.git"
echo "Git repo defaults to=$GIT_REPO"
echo "Hg repo branch=$BRANCH"
USAGE="[--quiet] [-r <repo>] [-b <branch>] [--force] [-D7] [-as <pAS>] [-u <user>] [-A <file>] [-M <name>]"
LONG_USAGE="Import hg repository <repo> up to either tip or <max>
If <repo> is omitted, use last hg repository as obtained from state file,
GIT_DIR/$PFX-$SFX_STATE by default.
Note: The argument order matters.
Options:
--quiet Quiet option passed to git push
-r <repo> Mercurial repository to import (InPlace='.')
-b Push only active branch
-c Do not clone fast-export repo
--force Force push to git repository if it exists.
-D7 Debian 7 version to import
-as <AS> Autosave files for pmac, camera, ... (pmac->pAS, camera->cAS)
-u <user> IOC Owner account (e.g. softioc)
-url <url> URL of the git repo to push to
-A <file> Read author map from file (generated: /tmp/authors)
(Same as in git-svnimport(1) and git-cvsimport(1))
-M <name> Set the default branch name (defaults to 'master')
"
case "$1" in
-h|--help)
echo "usage: $(basename "$0") $USAGE"
echo ""
echo "$LONG_USAGE"
exit 0
esac
while case "$#" in 0) break ;; esac
do
case "$1" in
-r|--r|--re|--rep|--repo)
shift
REPO="$1"
echo "Repository=$REPO"
;;
-b|--branch) # activeBranch
B_OPTS="$BRANCH"
echo "Only push active $BRANCH branch"
;;
-c|--notClone) # do not clone fast-export
FE_OPTS="DoNotClone"
echo "Not cloning fast-export"
;;
--q|--qu|--qui|--quie|--quiet)
GFI_OPTS="$GFI_OPTS --quiet"
;;
--force)
# pass --force to git-fast-import and hg-fast-export.py
GFI_OPTS="$GFI_OPTS --force"
IGNORECASEWARN="";
break
;;
-D7|--Deb7|--Debian7)
# Debian 7 requires v160914, or maybe v180610
# C_OPTS="-b v160914" # {Debian7: v160914, support for git >= 2.10}
C_OPTS="-b v180317"
# C_OPTS="-b v200213"
;;
-u|--usr|--user)
# Owner of the ioc directory
shift
IOC_OWNER="$1"
;;
-as|--autoS|--autoSave) # PMAC AutoSave as/req, as/save
shift
AS_OPTS="$1"
echo "Autosave=$AS_OPTS"
;;
-url|--URL|--gitURL)
shift
GIT_URL="$1"
GIT_REPO="$GIT_URL$REPO_S.git"
echo "Git repo->$GIT_REPO"
;;
*)
echo "#### Invalid option entered =\"$1\""
exit
;;
esac
shift
done
# exit if IOC_OWNER does not exists on this system
if [[ ! IOC_OWNER = "" ]] && [[ $(getent passwd $IOC_OWNER) = "" ]]; then
echo "#### User $IOC_OWNER does not exist on this system"
exit
fi
echo "Preparing environment"
rm /tmp/authors
if [[ $FE_OPTS = "" ]]; then # Clone fast-export if no -c flag
rm -rf /tmp/fast-export
git clone $C_OPTS https://github.com/frej/fast-export.git $FST_EXPRT
echo "Cloning fast-export repository"
else
echo "Not cloning fast-export repository"
fi
# Authors cleanup might be needed, then hg-fast-export with -A flag
echo "Getting authors informations"
if [ "$IOC_OWNER" = "" ]
then
hg log | grep user: | sort | uniq | sed 's/user: *//' > /tmp/tmp_authors
else
sudo -Eu $IOC_OWNER bash -c "hg log" | grep user: | sort | uniq | sed 's/user: *//' > /tmp/tmp_authors
fi
while read -r line
do
echo "\"$line\"=\"$line\"" >> /tmp/authors
done < /tmp/tmp_authors
# Test if it is a git directory
if [ -d .git ]; then
echo ".git repo exists, perform manual migration";
exit; # exit since it is a git repo.
else
echo "Not a git repository, preceed with hg->git conversion";
fi
# Test if it is a mercurial repo "hg root"
#hg --cwd the/path/you/want/to/test root
if [ ! -d .hg ]; then
echo "Not hg repository, no conversion";
exit;
else # Hg->git migration InPlace (inside hg repository))
echo "Starting hg-> git mibration, since .hg exists";
# Create .gitignore file if does not exist
if [ -f ".gitignore" ]; then
echo ".gitignore exists"
else
if [ "$IOC_OWNER" = "" ]
then
# cp .hgignore .gitignore
echo "syntax: glob" > .gitignore
echo "*~" >> .gitignore
echo "*.o" >> .gitignore
echo "*.d" >> .gitignore
echo "*.log" >> .gitignore
echo "*.dbl" >> .gitignore
echo "db/" >> .gitignore
echo "dbd/" >> .gitignore
echo "O.*" >> .gitignore
echo "envPaths" >> .gitignore
echo "bin/" >> .gitignore
echo "lib/" >> .gitignore
echo "as/" >> .gitignore
echo "autosave/" >> .gitignore
echo "/pmc/" >> .gitignore
echo ".hgignore" >> .gitignore
echo ".hg/" >> .gitignore
else
# sudo -Eu $IOC_OWNER bash -c "cp .hgignore .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo 'syntax: glob' > .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo '*~' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo '*.o' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo '*.d' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo '*.log' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo '*.dbl' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo 'db/' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo 'dbd/' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo 'O.*' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo 'envPaths' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo 'bin/' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo 'lib/' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo 'as/' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo 'autosave/' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo '/pmc/' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo '.hgignore' >> .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo '.hg/' >> .gitignore"
fi # IOC_OWNER
fi # .gitignore created
if [ "$IOC_OWNER" = "" ]
then
git init
/tmp/fast-export/hg-fast-export.sh -r . --force
if [ ! "$BRANCH" = "default" ]; then # not on the default branch
git checkout --force $BRANCH
fi
git reset --hard HEAD # the files shows as deleted in 'git status'
git add -f .gitignore
echo '.gitignore' >> .hgignore # hg should ignore internal git repo files
echo '.git/' >> .hgignore
echo 'records.dbl' >> .hgignore
else
sudo -Eu $IOC_OWNER bash -c "git init"
sudo -Eu $IOC_OWNER bash -c "/tmp/fast-export/hg-fast-export.sh -r . --force"
if [ ! "$BRANCH" = "default" ]; then # not on the default branch
sudo -Eu $IOC_OWNER bash -c "git checkout --force $BRANCH"
fi
sudo -Eu $IOC_OWNER bash -c "git reset --hard HEAD" # the files shows as deleted in 'git status'
sudo -Eu $IOC_OWNER bash -c "git add -f .gitignore"
sudo -Eu $IOC_OWNER bash -c "echo '.gitignore' >> .hgignore" # hg should ignore internal git repo files
sudo -Eu $IOC_OWNER bash -c "echo '.git/' >> .hgignore"
sudo -Eu $IOC_OWNER bash -c "echo 'records.dbl' >> .hgignore"
fi
case $AS_OPTS in # autosave
pAS|pmacAS|pmacAutoSave) # pmac as/req, as/save
if [ "$IOC_OWNER" = "" ]
then
git add -f as/req/info_positions.req
git add -f as/req/info_settings.req
git add -f as/save/info_positions.sav
git add -f as/save/info_settings.sav
else
sudo -Eu $IOC_OWNER bash -c "git add -f as/req/info_positions.req"
sudo -Eu $IOC_OWNER bash -c "git add -f as/req/info_settings.req"
sudo -Eu $IOC_OWNER bash -c "git add -f as/save/info_positions.sav"
sudo -Eu $IOC_OWNER bash -c "git add -f as/save/info_settings.sav"
fi
echo "pmac as files"
;;
cAS|cameraAS|cameraAutoSave) # areaDetector autosave
if [ "$IOC_OWNER" = "" ]
then
git add -f autosave/auto_settings.sav
else
sudo -Eu $IOC_OWNER bash -c "git add -f autosave/auto_settings.sav"
fi
echo "camera autosave files"
;;
picoAS|picoMotorAS|picoMotorAutoSave) # pico Motor autosave
if [ "$IOC_OWNER" = "" ]
then
git add -f as/req/motorNewFocus_positions.req
git add -f as/req/motorNewFocus_settings.req
git add -f as/save/motorNewFocus_positions.sav
git add -f as/save/motorNewFocus_settings.sav
else
sudo -Eu $IOC_OWNER bash -c "git add -f as/req/motorNewFocus_positions.req"
sudo -Eu $IOC_OWNER bash -c "git add -f as/req/motorNewFocus_settings.req"
sudo -Eu $IOC_OWNER bash -c "git add -f as/save/motorNewFocus_positions.sav"
sudo -Eu $IOC_OWNER bash -c "git add -f as/save/motorNewFocus_settings.sav"
fi
echo "pico motor autosave files"
;;
ez4AS|ez4axisAS|ez4axisMotorAutoSave) # pico Motor autosave
if [ "$IOC_OWNER" = "" ]
then
git add -f as/req/allmotion_pass0.req
git add -f as/req/allmotion_pass1.req
git add -f as/save/allmotion_pass0.sav
git add -f as/save/allmotion_pass1.sav
else
sudo -Eu $IOC_OWNER bash -c "git add -f as/req/allmotion_pass0.req"
sudo -Eu $IOC_OWNER bash -c "git add -f as/req/allmotion_pass1.req"
sudo -Eu $IOC_OWNER bash -c "git add -f as/save/allmotion_pass0.sav"
sudo -Eu $IOC_OWNER bash -c "git add -f as/save/allmotion_pass1.sav"
fi
echo "allMotion autosave files"
;;
esac
if [ "$IOC_OWNER" = "" ]
then
git commit -m ".gitignore tracked"
git remote add origin $GIT_REPO
# git push -u origin master $GFI_OPTS # push only master branch
# git push --set-upstream origin $BRANCH $GFI_OPTS # push feature branch
# git push -u origin --all $GFI_OPTS # push all branches
# git push -u origin --tags $GFI_OPTS
git push -u origin $B_OPTS $GFI_OPTS # push all branches
git push -u origin --tags $GFI_OPTS
else
sudo -Eu $IOC_OWNER bash -c "git commit -m '.gitignore tracked'"
sudo -Eu $IOC_OWNER bash -c "git remote add origin $GIT_REPO"
# sudo -Eu $IOC_OWNER bash -c "git push -u origin master $GFI_OPTS" # push only master branch
# sudo -Eu $IOC_OWNER bash -c "git push --set-upstream origin $BRANCH $GFI_OPTS" # push feature branch
# sudo -Eu $IOC_OWNER bash -c "git push -u origin --all $GFI_OPTS" # all branches
# sudo -Eu $IOC_OWNER bash -c "git push -u origin --tags $GFI_OPTS" # all branches
sudo -Eu $IOC_OWNER bash -c "git push -u origin $B_OPTS $GFI_OPTS" # all branches
sudo -Eu $IOC_OWNER bash -c "git push -u origin --tags $GFI_OPTS" # all branches
fi
fi # .hg exists, .git does not exist; Conversion hg-> git is automatic