Allow 409 error from Gerrit when creating the branch
Gerrit may return 409 error to us even if the branch was created.
We should ignore this error if the branch head is the commit we
requested.
Also pre-check the branch status before creating it.
If an existing branch is expected by the caller, simply return
the branch info without sending the creation request.
BUG=1433047
TEST=local run
Change-Id: Ic6184705579f1410cb9c54843cc0bbb4c4ad61b1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4436317
Reviewed-by: Gavin Mak <gavinmak@google.com>
Reviewed-by: Garrett Beaty <gbeaty@google.com>
Commit-Queue: Xinan Lin <linxinan@chromium.org>
diff --git a/gerrit_util.py b/gerrit_util.py
index eb30807..2ccc685 100644
--- a/gerrit_util.py
+++ b/gerrit_util.py
@@ -1077,7 +1077,7 @@
path = 'projects/%s/branches/%s' % (project, branch)
body = {'revision': commit}
conn = CreateHttpConn(host, path, reqtype='PUT', body=body)
- response = ReadHttpJsonResponse(conn, accept_statuses=[201])
+ response = ReadHttpJsonResponse(conn, accept_statuses=[201, 409])
if response:
return response
raise GerritError(200, 'Unable to create gerrit branch')