Mention Origin and X-Client headers in commit messages.
authorJelmer Vernooij <jelmer@jelmer.uk>
Sun, 10 Apr 2016 09:21:59 +0000 (09:21 +0000)
committerJelmer Vernooij <jelmer@jelmer.uk>
Sun, 10 Apr 2016 11:43:19 +0000 (11:43 +0000)
These are useful when e.g. web clients are used.

calypso/__init__.py
calypso/webdav.py

index da02962..28e29f4 100644 (file)
@@ -83,10 +83,15 @@ def _check(request, function):
         elif negotiate.enabled():
             user, negotiate_success = negotiate.try_aaa(authorization, request, owner)
 
+    client_info = dict([
+        (name, request.headers.get(name)) for name in
+            ("user-agent", "x-client", "origin")
+        if name in request.headers])
+
     # Also send UNAUTHORIZED if there's no collection. Otherwise one
     # could probe the server for (non-)existing collections.
     if request.server.acl.has_right(owner, user, password) or negotiate_success:
-        function(request, context={"user": user, "user-agent": request.headers.get("User-Agent", None)})
+        function(request, context={"user": user, "client_info": client_info})
     else:
         request.send_calypso_response(client.UNAUTHORIZED, 0)
         if negotiate.enabled():
index 0299c7a..3983b6c 100644 (file)
@@ -351,52 +351,55 @@ class Collection(object):
 
     def __repr__(self):
         return "<Calendar %s>" % (self.name)
-        
+
     def has_git(self):
         return True
 
-    def git_commit(self, context):
+    def git_commit(self, action, context):
         args = ["git", "commit", "--allow-empty"]
         env = {}
 
-        message = context.get('action', 'other action')
+        message = action
 
         if "user" in context:
             # use environment variables instead of --author to avoid git
             # looking it up in previous commits if it doesn't seem well-formed
-            env['GIT_AUTHOR_NAME'] = context['user'] or "unknown"
-            env['GIT_AUTHOR_EMAIL'] = "%s@webdav"%context['user']
+            env['GIT_AUTHOR_NAME'] = context["user"] or "unknown"
+            env['GIT_AUTHOR_EMAIL'] = "%s@webdav" % context["user"]
             # supress a chatty message that we could configure author
             # information explicitly in the config file. (slicing it in after
             # the git command as position is important with git arguments)
             args[1:1] = ["-c", "advice.implicitIdentity=false"]
-        if "user-agent" in context:
-            message += u"\n\nUser-Agent: %r"%context['user-agent']
+
+        if context["client_info"]:
+            message += u"\n"
+            for key, value in context["client_info"].iteritems():
+                message += u"%s: %s\n" % (key, value)
 
         args.extend(["-m", message.encode('utf8')])
 
         subprocess.check_call(args, cwd=self.path, env=env)
 
-    def git_add(self, path, context):
+    def git_add(self, path, action, context):
         if self.has_git():
             subprocess.check_call(["git", "add", os.path.basename(path)], cwd=self.path)
-            self.git_commit(context=context)
-    
-    def git_rm(self, path, context):
+            self.git_commit(action, context)
+
+    def git_rm(self, path, action, context):
         if self.has_git():
             subprocess.check_call(["git", "rm", os.path.basename(path)], cwd=self.path)
-            self.git_commit(context=context)
+            self.git_commit(action, context)
 
-    def git_change(self, path, context):
+    def git_change(self, path, action, context):
         if self.has_git():
             subprocess.check_call(["git", "add", os.path.basename(path)], cwd=self.path)
-            self.git_commit(context=context)
+            self.git_commit(action, context)
             # Touch directory so that another running instance will update
             try:
                 os.utime(self.path, None)
             except Exception, ex:
                 self.log.exception("Failed to set directory mtime")
-            
+
     def write_file(self, item):
         fd, path = tempfile.mkstemp(item.file_extension, item.file_prefix, dir=self.path)
         self.log.debug('Trying to write to %s', path)
@@ -416,11 +419,9 @@ class Collection(object):
                 self.log.exception("Failed to make collection directory %s: %s", self.path, ose)
                 raise
 
-        context['action'] = u'Add %s'%item
-
         try:
             path = self.write_file(item)
-            self.git_add(path, context=context)
+            self.git_add(path, 'Add %s' % item, context)
             self.scan_dir(True)
         except OSError, ex:
             self.log.exception("Error writing file")
@@ -433,11 +434,9 @@ class Collection(object):
     def destroy_file(self, item, context):
         self.log.debug("Remove %s", item.name)
 
-        context['action'] = u'Remove %s'%item
-
         try:
             os.unlink(item.path)
-            self.git_rm(item.path, context=context)
+            self.git_rm(item.path, 'Remove %s' % item, context)
             self.scan_dir(True)
         except Exception, ex:
             self.log.exception("Failed to remove %s", item.path)
@@ -446,18 +445,16 @@ class Collection(object):
     def rewrite_file(self, item, context):
         self.log.debug("Change %s", item.name)
 
-        context['action'] = u'Modify %s'%item
-
         try:
             new_path = self.write_file(item)
             os.rename(new_path, item.path)
             self.scan_file(item.path)
-            self.git_change(item.path, context=context)
+            self.git_change(item.path, u'Modify %s' % item, context)
             self.scan_dir(True)
         except Exception, ex:
             self.log.exception("Failed to rewrite %s", item.path)
             raise
-        
+
     def get_item(self, name):
         """Get collection item called ``name``."""
         for item in self.my_items: