# HG changeset patch
# User Jeff Hammel <jhammel@mozilla.com>
# Date 1274839341 25200
# Node ID 44ea39c3e98fd7e4529b7cec5240fd9d9fb34223
# Parent  8bc27dbf0214b6c71ca0be0c8ef5bf242b051fa5
add methods for dealing with the patch repositories

diff -r 8bc27dbf0214 -r 44ea39c3e98f hq/main.py
--- a/hq/main.py	Tue May 25 13:39:19 2010 -0700
+++ b/hq/main.py	Tue May 25 19:02:21 2010 -0700
@@ -39,11 +39,14 @@
         os.chdir(directory)
         call(['hg', 'qinit', '-c'])
         if queue:
-            _oldcwd = os.getcwd()
-            os.chdir(os.path.join('.hg', 'patches'))
-            call(['hg', 'pull', '--update', queue])
-            os.chdir(_oldcwd)
+            # pull from the given repository
+            self._patch_command(*['hg', 'pull', '--update', queue])
+        else:
+            # (optionally) setup a new repo
+            pass # TODO
+            
         if patch:
+            # create a new patch
             call(['hg', 'qnew', patch])
 
     def edit(self, patch=None):
@@ -58,9 +61,7 @@
         """
         call(['hg', 'qrefresh'])
         call(['hg', 'qcommit', '-m', message])
-        root = subprocess.Popen(['hg', 'root'], stdout=subprocess.PIPE).communicate()[0]
-        os.chdir(os.path.join(root, '.hg', 'patches'))
-        call(['hg', 'push'])
+        self._patch_command(*['hg', 'push'])
 
     def pull(self, repo=None):
         """
@@ -92,6 +93,17 @@
         stdout, stderr = process.communicate()
         return stdout
 
+    def _patch_repo(self):
+        """the location of the patch repository"""
+        root = subprocess.Popen(['hg', 'root'], stdout=subprocess.PIPE).communicate()[0]
+        return os.path.join(root, '.hg', 'patches')
+
+    def _patch_command(self, *command):
+        """perform a command in the patch repository"""
+        _oldpwd = os.getcwd()
+        os.chdir(self._patch_repo())
+        call(command)
+        os.chdir(_oldpwd)
 
 def main(args=sys.argv[1:]):
     parser = CommandParser(HQ)