-
Notifications
You must be signed in to change notification settings - Fork 1
/
git_move.sh
executable file
·82 lines (59 loc) · 1.66 KB
/
git_move.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
#!/usr/bin/env bash
# Usage:
# ./git_move.sh git@repo_site.com:/my_repo.git origin/folder/path/ /destination/repo/path/ new/folder/path/
repo=$1
folder=$2
dest_repo=$3
dest_folder=$4
clone_folder='__git_clone_repo__'
echo 'cloning repo...'
#clone repo
git clone $repo $clone_folder
#move to new folder and get path for later
cd $clone_folder
#get old branch with no trailing slash
old_branch_path=`pwd | sed "s,/$,,"`
echo 'filtering branch...'
#removes everything but the folder we need
git filter-branch --subdirectory-filter $folder -- -- all
echo 'making destination folder...'
#make destination folder recursive
base=''
base_dest=''
count=0
IFS='/' read -ra ADDR <<< "$dest_folder"
for i in "${ADDR[@]}"; do
if [["$count" = "0"]]
then
base_dest=$i
fi
count=$(($count+1))
base=$base''$i'/'
mkdir $base
done
echo 'moving files into destination folder in old repo...'
#move all one by one or it complains
for file in `ls`; do
#move it all
if [[ "$file" != "$base_dest" ]]
then
git mv $file $dest_folder
fi
done
#commit to make the history work
git commit -m "removed all data but folder to move"
#move to destination _repo_ folder
cd $dest_repo
echo 'adding local remote to new repo...'
git remote add _repo_1 $old_branch_path
#fetch the remote source, create a branch and merge it with the destination repository in usual way
git fetch _repo_1
git branch _repo_1 remotes/_repo_1/master
git merge _repo_1
echo 'removing remote and dummy branch...'
git remote rm _repo_1
git branch -d _repo_1
echo 'cleaning up temp repo...'
chmod 0777 $old_branch_path
rm -r -f $old_branch_path
echo 'Done. Remeber to commit and push the changes to the destination branch.'