This repository has been archived by the owner on Jan 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-me
executable file
·161 lines (130 loc) · 4.22 KB
/
merge-me
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
#!/usr/bin/env bash
function exit_with_err_msg() {
echo >&2 -e "\\033[41m\\033[1mError\\033[0m $1"
exit 1
}
function debug_msg() {
if [[ "$DEBUG" = true ]]; then
if [ -t 0 ]; then
if [ $# -gt 0 ]; then
echo >&2 -ne "\\033[42m\\033[1mDebug\\033[0m "
echo >&2 "$*"
fi
else
local input
read -r input
if [[ -n "$input" ]]; then
echo >&2 -ne "\\033[42m\\033[1mDebug\\033[0m "
echo >&2 "$input"
fi
fi
fi
}
function info_msg() {
if [ -t 0 ]; then
if [ $# -gt 0 ]; then
echo >&2 -ne "\\033[44m\\033[1mInfo\\033[0m "
echo >&2 "$*"
fi
else
local input
read -r input
if [[ -n "$input" ]]; then
echo >&2 -ne "\\033[44m\\033[1mInfo\\033[0m "
echo >&2 "$input"
fi
fi
}
function cmd_exists_or_fail() {
debug_msg "Checking for command: $1"
command -v "$1" >/dev/null 2>&1 || exit_with_err_msg "Could not find command: $1"
}
function dir_exists_or_fail() {
debug_msg "Checking for directory: $1"
[[ -d "$1" ]] || exit_with_err_msg "Could not find directory: $1"
}
function dir_not_exists_or_fail() {
debug_msg "Checking for directory: $1"
[[ -d "$1" ]] && exit_with_err_msg "Directory already exists: $1"
}
function clean_up() {
debug_msg "Cleaning up"
rm -rf "$tmp" || exit_with_err_msg "Could not remove directory: $tmp"
cd "$initial_wd" || exit_with_err_msg "Could not navigate to initial pwd"
}
trap "clean_up" EXIT
function extract_info_from_col_path() {
local col_dir_path col_dir_basename col_legacy_id col_version
col_dir_path=$1
col_dir_basename=$(basename "$1")
col_legacy_id=${col_dir_basename%%_*}
col_version=${col_dir_basename##*_}
debug_msg "Got basename $col_dir_basename from $col_dir_path"
debug_msg "Detected collection legacy id: $col_legacy_id"
debug_msg "Detected collection version: $col_version"
echo "$col_legacy_id" "$col_version"
}
function make_temp() {
info_msg "Making temp directory: $tmp"
mkdir "$tmp" || exit_with_err_msg "Could not create directory: $tmp"
}
function populate_temp() {
debug_msg "Populating temp directory"
local params params_common col_info col_legacy_id col_version
col_info="$(extract_info_from_col_path "$col_path")"
read -r col_legacy_id col_version <<< "$col_info"
info_msg "Copying collection with user changes into temp"
cp -r "$col_path" "${tmp}/mine"
[[ "$HUMAN" = true ]] && params=("-t")
params_common=("$neb_env" "$col_legacy_id")
params=(${params[@]} ${params_common[@]})
info_msg "Fetching latest into temp"
neb get ${params[@]} latest -d "${tmp}/theirs" || exit_with_err_msg "Fetch failed."
info_msg "Fetching parent version into temp"
neb get ${params[@]} "$col_version" -d "${tmp}/parent" <<< "y" || exit_with_err_msg "Fetch failed."
}
function merge() {
cd "$tmp" || exit_with_err_msg "Could not navigate to temp directory"
cd parent || exit_with_err_msg "Navigation error: $(pwd)/parent"
info_msg "Initializing git in parent"
git init 2>&1 | debug_msg
git add . 2>&1 | debug_msg
git commit -m "parent" 2>&1 | debug_msg
git checkout -b mine 2>&1 | debug_msg
git checkout -b theirs 2>&1 | debug_msg
info_msg "Resolving head changes"
rm -rf ./* 2>&1 | debug_msg
cp -r ../theirs/* . 2>&1 | debug_msg
git add :/ 2>&1 | debug_msg
git commit -m "theirs" 2>&1 | debug_msg
git checkout mine 2>&1 | debug_msg
info_msg "Resolving local changes"
rm -rf ./* 2>&1 | debug_msg
cp -r ../mine/* . 2>&1 | debug_msg
git add :/ 2>&1 | debug_msg
git commit -m "mine" 2>&1 | debug_msg
info_msg "Merging"
git rebase --strategy-option=ours theirs 2>/dev/null
debug_msg "Trying to force the rebase through. A failure is ok"
git rebase --continue 2>&1 | debug_msg
cd "$initial_wd" || exit_with_err_msg "Could not navigate to initial pwd"
}
function copy_merge_to_out() {
debug_msg "Copying merged directory as desired output directory"
cp -r "${tmp}/parent" "$out_dir"
}
col_path=$1
neb_env=$2
out_dir=$3
initial_wd=$(pwd)
tmp="${initial_wd}/.merge-me-tmp"
[[ "$DEBUG" = true ]] && export DEBUG=true
cmd_exists_or_fail neb
cmd_exists_or_fail git
dir_exists_or_fail "$col_path"
dir_not_exists_or_fail "$out_dir"
make_temp
populate_temp
merge
copy_merge_to_out
info_msg "Done - Output in directory: $out_dir"