1#!/usr/bin/env python2.7
2
3# Script to migrate a Gerrit Change from memcached or ep-engine repos
4# to the new kv_enigne repo.
5
6from __future__ import print_function
7import json
8import subprocess
9import sys
10import urllib2
11
12
13class bcolors:
14    HEADER = '\033[35m'
15    OKBLUE = '\033[34m'
16    OKGREEN = '\033[32m'
17    WARNING = '\033[33m'
18    FAIL = '\033[31m'
19    ENDC = '\033[0m'
20    BOLD = '\033[1m'
21    UNDERLINE = '\033[4m'
22
23
24def header(*args):
25    print(bcolors.OKBLUE, end='')
26    print(*args, end='')
27    print(bcolors.ENDC)
28
29
30def success(*args):
31    print(bcolors.OKGREEN, end='')
32    print(*args, end='')
33    print(bcolors.ENDC)
34
35
36if len(sys.argv) != 3:
37    print("""Migrates existing Gerrit changes from
38memcached/ep-engine repo to kv_engine repo.
39Checks out the specifed change, rebases to kv_engine and re-uploads to
40gerrit as a kv_engine change.
41
42Usage: gerrit_move_to_kv_engine.py <username> <change>
43
44    username   Username for Gerrit - see
45               http://review.couchbase.org/#/settings/ under 'Profile'
46    change     Change number
47
48Example: gerrit_move_to_kv_engine.py drigby 78322
49    """, file=sys.stderr)
50    exit(1)
51
52user = sys.argv[1]
53change = sys.argv[2]
54
55info = dict()
56# Check this patch exists and determine which project it is from.
57try:
58    url = "http://review.couchbase.org/changes/{}".format(change)
59    f = urllib2.urlopen(url + "?o=CURRENT_REVISION&o=DOWNLOAD_COMMANDS")
60    data = f.read()[5:]  # The first 4 chars of response are ')]}'
61    info = json.loads(data)
62except urllib2.HTTPError as e:
63    print ("Error: failed to lookup change {} from {}: {}".format(change,
64                                                                  url, e),
65           file=sys.stderr)
66    print ("Check your Change is valid", file=sys.stderr)
67    exit(1)
68
69header("** Migrating change {} - '{}' to kv_engine".format(change,
70                                                           info['subject']))
71
72cur_rev_info = info['revisions'][info['current_revision']]
73fetch_info = cur_rev_info['fetch']['anonymous http']
74
75header(">> Fetching", fetch_info['ref'])
76subprocess.check_output(["git", "fetch", fetch_info['url'],
77                         fetch_info['ref']])
78
79header("-- Checking out change", bcolors.ENDC)
80subprocess.check_output(["git", "checkout", "FETCH_HEAD"])
81
82download_URL = "git@github.com:couchbase/kv_engine.git"
83header("-- Checking out latest kv_engine/master")
84subprocess.check_output(["git", "fetch", download_URL, "master"])
85
86header("-- Rebasing change onto kv_engine/master")
87if info['project'] == 'ep-engine':
88    subprocess.check_output(["git", "rebase",
89                             "--strategy=subtree",
90                             "--strategy-option=subtree=engines/ep",
91                             "FETCH_HEAD"])
92else:
93    subprocess.check_output(["git", "rebase", "FETCH_HEAD"])
94
95upload_URL = "ssh://{}@review.couchbase.org:29418/kv_engine".format(user)
96header("<< Uploading to Gerrit under kv_engine")
97subprocess.check_output(["git", "push", upload_URL,
98                         "HEAD:refs/for/master"])
99success("** Successfully moved change to kv_engine")
100