forked from thxmasj/git-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-refactor-repo
executable file
·113 lines (105 loc) · 2.85 KB
/
git-refactor-repo
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
#!/usr/bin/env bash
sourceRepository=
sourceBranch=
sourceDirectory=
targetRepository=
targetBranch=
workDirectory=$(mktemp -d "${TMPDIR:-/tmp}/XXXXXXXXXXXX")
removeDirectory=
forkRepo() {
__readArgs "${@}"
test -z $sourceRepository && { __usage; exit 1; }
test -z $sourceBranch && { __usage; exit 1; }
test -z $targetRepository && { __usage; exit 1; }
test -z $targetBranch && { __usage; exit 1; }
git clone -b ${sourceBranch} --single-branch ${sourceRepository} ${workDirectory}
echo "Cloned to ${workDirectory}"
cd ${workDirectory}
modifyRepo ${sourceDirectory:+--sourceDirectory ${sourceDirectory}} --sourceBranch ${sourceBranch} ${removeDirectory:+--removeDirectory "${removeDirectory}"}
git remote set-url origin ${targetRepository}
git push origin -f --follow-tags ${sourceBranch}:${targetBranch}
}
modifyRepo() {
__readArgs "${@}"
test -z $sourceBranch && { __usage; exit 1; }
git filter-branch \
${sourceDirectory:+--subdirectory-filter ${sourceDirectory}} \
--prune-empty \
${removeDirectory:+--index-filter "git rm -r --cached --ignore-unmatch ${removeDirectory}"} \
--tag-name-filter cat \
${sourceBranch}
}
removeFiles() {
__readArgs "${@}"
test -z $sourceBranch && { >&2 echo "Missing required argument sourceBranch"; __usage; exit 1; }
test -z $removeDirectory && { >&2 echo "Missing required argument removeDirectory"; __usage; exit 1; }
git filter-branch \
--prune-empty \
${removeDirectory:+--index-filter "git rm -r --cached --ignore-unmatch ${removeDirectory}"} \
--tag-name-filter cat \
${sourceBranch}
}
__usage() {
echo "Usage:
$(basename $0) [COMMAND] \\
--sourceRepository SOURCE_REPOSITORY \\
--sourceBranch SOURCE_BRANCH \\
[--sourceDirectory SOURCE_DIRECTORY] \\
--targetRepository TARGET_REPOSITORY \\
--targetBranch TARGET_BRANCH \\
[--workDirectory WORK_DIRECTORY] \\
[--removeDirectory REMOVE_DIRECTORY]"
}
__readArgs() {
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
--sourceRepository)
sourceRepository="$2"
shift
;;
--sourceBranch)
sourceBranch="$2"
shift
;;
--sourceDirectory)
sourceDirectory="$2"
shift
;;
--targetRepository)
targetRepository="$2"
shift
;;
--targetBranch)
targetBranch="$2"
shift
;;
--workDirectory)
workDirectory="$2"
shift
;;
--removeDirectory)
removeDirectory="${2}"
shift
;;
*)
# fail on unknown argument
>&2 echo "Unknown argument ${key}"
exit 1
;;
esac
shift # past argument or value
done
}
case $1 in *)
function=$1
if [ "function" = "$(type -t ${function})" ]; then
shift
${function} "${@}"
else
>&2 echo "Unknown command ${function}"
usage
fi
;;
esac