-
Notifications
You must be signed in to change notification settings - Fork 21
/
odoo-repos-merge-pr.sh
executable file
·135 lines (115 loc) · 4.35 KB
/
odoo-repos-merge-pr.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
#!/bin/bash
if [ -z "$HOME" ]; then
echo "ERROR: Instalation error, HOME is not defined"
exit 1
fi
show_help() {
echo "ERROR: $2"
echo "Usage: $0 <repo> <pr> [pr, ...]"
exit $1
}
repo="$1"
if [ -z "$repo" ]; then
show_help 1 "No repo defined"
fi
show_error() {
local error=$1
local output=$2
echo "ERROR: $error";
if [ -n "$output" ] && [ -f $output ]; then
echo "----------------------------------------------------------------"
cat $output
echo "----------------------------------------------------------------"
rm -rf $output
fi
}
merge_pr() {
local name=$1
local prs=${@:2}
local error=0
if [ -z "$prs" ]; then
show_help 3 "No PRs defined"
fi
local temp=/tmp/odoo_repos_merge_pr.$$.tmp
git -C $(pwd)/$name symbolic-ref HEAD > $temp 2>&1
error=$?; if [ $error -ne 0 ]; then printf "%-28s - " "$name"; show_error "No HEAD ref"; return $error; fi
local head=$(cat $temp)
local branch=$(expr $head : 'refs/heads/\(.*\)')
local remote=$(git -C $(pwd)/$name config branch.$branch.remote)
local remote_branch=$(expr $(git -C $(pwd)/$name config branch.$branch.merge) : 'refs/heads/\(.*\)')
local status=''
git -C $(pwd)/$name status --porcelain > $temp 2>&1
if [ -n "$(cat $temp)" ]; then status='DIRTY'; fi
if [ "$status" == 'DIRTY' ]; then
show_error "$name is dirty" $temp
elif [ -z "$remote" ] || [ -z "$remote_branch" ]; then
show_error "$name has no tracking branch: remote = '$remote', remote_branch = '$remote_branch'"
else
printf "%-28s - Merging PRs: %s\n" "$name" "$prs"
local all_branch=
local new_branch='merge'
echo -n " - Fetch remote '$remote' ... "
git -C $(pwd)/$name fetch $remote > $temp 2>&1
error=$?; if [ $error -ne 0 ]; then show_error $error $temp; return $error; fi
echo "OK"
for id in $prs; do
echo -n " - Fetch PR '$id' ... "
git -C $(pwd)/$name fetch --force $remote pull/$id/head:pr$id > $temp 2>&1
error=$?; if [ $error -ne 0 ]; then show_error $error $temp; return $error; fi
new_branch=${new_branch}_$id
all_branch="$all_branch pr$id"
echo "OK"
done
echo -n " - Checkout to '$remote/$remote_branch' ... "
git -C $(pwd)/$name checkout $remote/$remote_branch > $temp 2>&1
error=$?; if [ $error -ne 0 ]; then show_error $error $temp; return $error; fi
echo "OK"
if git -C $(pwd)/$name branch -l --no-color | egrep -q " $new_branch\$"; then
echo -n " - Removing branch '$new_branch' ... "
git -C $(pwd)/$name branch -D $new_branch > $temp 2>&1
error=$?; if [ $error -ne 0 ]; then show_error $error $temp; return $error; fi
echo "OK"
fi
echo -n " - Creating branch '$new_branch' ... "
git -C $(pwd)/$name branch $new_branch $remote/$remote_branch > $temp 2>&1
error=$?; if [ $error -ne 0 ]; then show_error $error $temp; return $error; fi
echo "OK"
echo -n " - Checkout to '$new_branch' ... "
git -C $(pwd)/$name checkout --force $new_branch > $temp 2>&1
error=$?; if [ $error -ne 0 ]; then show_error $error $temp; return $error; fi
echo "OK"
echo -n " - Reseting hard to '$remote/$remote_branch' ... "
git -C $(pwd)/$name reset --hard $remote/$remote_branch > $temp 2>&1
error=$?; if [ $error -ne 0 ]; then show_error $error $temp; return $error; fi
echo "OK"
echo -n " - Merging '$all_branch' ... "
git -C $(pwd)/$name merge --no-edit $all_branch > $temp 2>&1
error=$?; if [ $error -ne 0 ]; then show_error $error $temp; return $error; fi
echo "OK"
fi
rm -rf $temp
}
odoo_merge_pr() {
cd $HOME
if [ -d $HOME/OCB/.git ]; then
merge_pr OCB ${@:2}
elif [ -d $HOME/odoo/.git ]; then
merge_pr odoo ${@:2}
elif [ -d $HOME/openerp/.git ]; then
merge_pr openerp ${@:2}
fi
}
if [ "$repo" == 'odoo' ]; then
odoo_merge_pr $@
elif [ -d $HOME/repos/$repo/.git ]; then
cd $HOME/repos
merge_pr $@
elif [ -d $HOME/$repo/.git ]; then
cd $HOME
merge_pr $@
elif [ -d $repo/.git ]; then
cd $(dirname $repo)
merge_pr $(basename $repo) ${@:2}
else
show_help 2 "Repo '$repo' not found"
fi