Add change HEAD cli option to gerrit_client

This option modifies default branch of desired repository.

R=ehmaldonaldo@chromium.org, gavinmak@google.com

Change-Id: I1e20dc8d333c4301eacffff5049e3a98c3d59f75
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2575884
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
diff --git a/gerrit_util.py b/gerrit_util.py
index aa8a396..6312b16 100644
--- a/gerrit_util.py
+++ b/gerrit_util.py
@@ -75,6 +75,9 @@
     self.http_status = http_status
     self.message = '(%d) %s' % (self.http_status, message)
 
+  def __str__(self):
+    return self.message
+
 
 def _QueryString(params, first_param=None):
   """Encodes query parameters in the key:val[+key:val...] format specified here:
@@ -909,6 +912,39 @@
   raise GerritError(200, 'Unable to create gerrit branch')
 
 
+def GetHead(host, project):
+  """Retrieves current HEAD of Gerrit project
+
+  https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-head
+
+  Returns:
+    A JSON object with 'ref' key.
+  """
+  path = 'projects/%s/HEAD' % (project)
+  conn = CreateHttpConn(host, path, reqtype='GET')
+  response = ReadHttpJsonResponse(conn, accept_statuses=[200])
+  if response:
+    return response
+  raise GerritError(200, 'Unable to update gerrit HEAD')
+
+
+def UpdateHead(host, project, branch):
+  """Updates Gerrit HEAD to point to branch
+
+  https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#set-head
+
+  Returns:
+    A JSON object with 'ref' key.
+  """
+  path = 'projects/%s/HEAD' % (project)
+  body = {'ref': branch}
+  conn = CreateHttpConn(host, path, reqtype='PUT', body=body)
+  response = ReadHttpJsonResponse(conn, accept_statuses=[200])
+  if response:
+    return response
+  raise GerritError(200, 'Unable to update gerrit HEAD')
+
+
 def GetGerritBranch(host, project, branch):
   """Gets a branch from given project and commit.