upload: Automatically --cc folks in review.URL.autocopy

The upload command will read review.URL.autocopy from the project's
configuration and append the list of e-mails specified to the
--cc argument of the upload command if a non-empty --re argument
was provided.

Change-Id: I2424517d17dd3444b20f0e6a003be6e70b8904f6
Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/subcmds/upload.py b/subcmds/upload.py
index 4fa5b43..ba53246 100644
--- a/subcmds/upload.py
+++ b/subcmds/upload.py
@@ -13,6 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import copy
 import re
 import sys
 
@@ -83,6 +84,14 @@
 and will not prompt you further.  If it is set to "false" then repo
 will assume you always answer "n", and will abort.
 
+review.URL.autocopy:
+
+To automatically copy a user or mailing list to all uploaded reviews,
+you can set a per-project or global Git option to do so. Specifically,
+review.URL.autocopy can be set to a comma separated list of reviewers
+who you always want copied on all uploads with a non-empty --re
+argument.
+
 The URL must match the review URL listed in the manifest XML file,
 or in the .git/config within the project.  For example:
 
@@ -92,6 +101,7 @@
 
   [review "http://review.example.com/"]
     autoupload = true
+    autocopy = johndoe@company.com,my-team-alias@company.com
 
 References
 ----------
@@ -219,6 +229,19 @@
 
     self._UploadAndReport(todo, people)
 
+  def _AppendAutoCcList(self, branch, people):
+    """
+    Appends the list of users in the CC list in the git project's config if a
+    non-empty reviewer list was found.
+    """
+
+    name = branch.name
+    project = branch.project
+    key = 'review.%s.autocopy' % project.GetBranch(name).remote.review
+    raw_list = project.config.GetString(key)
+    if not raw_list is None and len(people[0]) > 0:
+      people[1].extend([entry.strip() for entry in raw_list.split(',')])
+
   def _FindGerritChange(self, branch):
     last_pub = branch.project.WasPublished(branch.name)
     if last_pub is None:
@@ -290,10 +313,13 @@
     branch.replace_changes = to_replace
     self._UploadAndReport([branch], people)
 
-  def _UploadAndReport(self, todo, people):
+  def _UploadAndReport(self, todo, original_people):
     have_errors = False
     for branch in todo:
       try:
+        people = copy.deepcopy(original_people)
+        self._AppendAutoCcList(branch, people)
+
         branch.UploadForReview(people)
         branch.uploaded = True
       except UploadError, e: