Fredrik Håård avatar Fredrik Håård committed b5b9422

Basic hg_diff command implemented #9

Comments (0)

Files changed (5)

  hg branches
  hg tag
  hg tags
+ hg diff
 You also have access to the configuration (config, configbool,
 configlist) just as in the internal Mercurial API. The repository 
 # General information about the project.
 project = u'hgapi'
-copyright = u'2011,'
+copyright = u''
 # The version info for the project you're documenting, acts as replacement for
 # |version| and |release|, also used in various other places throughout the
 # built documents.
 # The short X.Y version.
-version = '1.3.1'
+version = '1.4.1'
 # The full version, including alpha/beta/rc tags.
-release = '1.3.1a1'
+release = '1.4.1'
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.
         return values
+    def hg_diff(self, rev_a=None, rev_b=None, filenames=None):
+        """Get a unified diff as returned by 'hg diff'
+        rev_a and rev_b are passed as -r <rev> arguments to the call,
+        and filenames are expected to be an iterable of file names.
+        """
+        cmds = ['diff']
+        for rev in (rev_a, rev_b):
+            if not rev is None:
+                cmds += ['-r', rev]
+        if not filenames is None:
+            cmds += list(filenames)
+        result =  self.hg_command(*cmds)
+        diffs = []
+        filere = re.compile("^diff .* (\S+)$")
+        for line in result.split('\n'):
+            match = filere.match(line)
+            if match:
+                diffs.append({'filename': match.groups()[0],  'diff': ''})
+            diffs[-1]['diff'] += line + '\n'
+        return diffs
     def hg_status(self, empty=False, clean=False):
         """Get repository status.
         Returns a dict containing a *change char* -> *file list* mapping, where 
         if value.count(","):
             return value.split(",")
-            return value.split()
+            return value.split()


         self.assertTrue("commit test_branch" in branch)
         self.assertFalse("commit test_branch" in default)
+    def test_230_BasicDiff(self):
+        diffs = self.repo.hg_diff('default', 'test_branch')
+        self.assertTrue('.hgtags' in [diff['filename'] for diff in diffs])
+        self.assertTrue('+even more stuff' in diffs[1]['diff'])
+    def test_240_DiffFile(self):
+        diffs = self.repo.hg_diff('default', 'test_branch', filenames=['file.txt'])
+        self.assertEquals(len(diffs), 1)
+        self.assertEquals(diffs[0]['filename'], 'file.txt')
+        self.assertTrue('+even more stuff' in diffs[0]['diff'])
 def test_doc():
     #Prepare for doctest
-        unittest.main()
+        unittest.main()
     name = "hgapi",
-    version = "1.3.1a3",
+    version = "1.4.1",
     packages = ['hgapi'],
     test_suite = "hgapi.testhgapi.TestHgAPI",
     author = "Fredrik Håård",
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.