06d5f75cfb55

Add path sanitization because Windows just *has* to use backslashes...
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Wed, 07 Oct 2009 18:23:37 -0400
parents ab4cc556087d
children b3bae1d649d7
branches/tags (none)
files review/api.py review/extension_ui.py

Changes

--- a/review/api.py	Tue Oct 06 19:45:29 2009 -0400
+++ b/review/api.py	Wed Oct 07 18:23:37 2009 -0400
@@ -26,6 +26,22 @@
     '''Raised when trying to delete an object that does not support deletion.'''
     pass
 
+class FileNotInChangeset(Exception):
+    '''Raised when trying to add a comment on a file not in the changeset.'''
+    def __init__(self, filename):
+        super(FileNotInChangeset, self).__init__()
+        self.filename = filename
+    
+
+
+def _split_path_dammit(p):
+    def _spd(p):
+        p, i = os.path.split(p)
+        while i:
+            yield i
+            p, i = os.path.split(p)
+    
+    return list(_spd(p))[::-1]
 
 def _parse_hgrf(repo):
     """Parse the .hgreview file and return the data inside."""
@@ -59,6 +75,12 @@
     return data
 
 
+def sanitize_path(p, repo=None):
+    '''Take a path specific to the current platform and convert it.'''
+    if repo:
+        p = os.path.relpath(p, start=repo.root)
+    return '/'.join(_split_path_dammit(p))
+
 class ReviewDatastore(object):
     '''The data store for all the reviews so far.'''
     def __init__(self, ui, repo, lpath=None, rpath=None, create=False):
@@ -146,6 +168,9 @@
     
     def add_comment(self, message, filename='', lines=[]):
         '''Add (and commit) a comment for the given file and lines.'''
+        if filename and filename not in self.target[self.node].files():
+            raise FileNotInChangeset(filename)
+        
         comment = ReviewComment(self.ui.username(), datetime.utcnow(),
             self.node, filename, lines, message)
         comment.commit(self.ui, self.repo)
--- a/review/extension_ui.py	Tue Oct 06 19:45:29 2009 -0400
+++ b/review/extension_ui.py	Wed Oct 07 18:23:37 2009 -0400
@@ -30,12 +30,7 @@
     rcset = rd[rev]
     
     if filename:
-        filename = os.path.relpath(filename, start=repo.root)
-        
-        if filename not in repo[rcset.node].files():
-            raise util.Abort(
-                messages.COMMENT_FILE_DOES_NOT_EXIST % (filename, repo[rev].rev())
-            )
+        filename = sanitize_path(filename, repo)
     
     if lines and not filename:
         raise util.Abort(messages.COMMENT_LINES_REQUIRE_FILE)
@@ -43,7 +38,15 @@
     if not message:
         raise util.Abort(messages.COMMENT_REQUIRES_MESSAGE)
     
-    rcset.add_comment(message=message, filename=filename, lines=lines.split(','))
+    if lines:
+        lines=lines.split(',')
+    
+    try:
+        rcset.add_comment(message=message, filename=filename, lines=lines)
+    except FileNotInChangeset:
+        raise util.Abort(
+            messages.COMMENT_FILE_DOES_NOT_EXIST % (filename, repo[rev].rev())
+        )
 
 def _signoff_command(ui, repo, **opts):
     rd = ReviewDatastore(ui, repo)
@@ -103,6 +106,7 @@
     for comment in review_level_comments:
         _print_comment(comment, before='\n')
     
+    fnames = [sanitize_path(fname, repo) for fname in fnames]
     diffs = rcset.full_diffs(fnames, opts)
     
     for filename in diffs: