Commits

Fredrik Håård committed 5c351bf Merge

merge

Comments (0)

Files changed (3)

  hg status
  hg merge (fails on conflict)
  hg revert
+ hg branch
+ hg branches
 
 You also have access to the configuration (config, configbool,
 configlist) just as in the internal Mercurial API. The repository 
             for key in kwargs:
                 cmds += [key, kwargs[key]]
         return self.hg_command(*cmds)
-        
+
+    def hg_branch(self, branch_name=None):
+        """ Creates a branch of branch_name isn't None
+            If not, returns the current branch name.
+        """
+        args = []
+        if branch_name:
+            args.append(branch_name)
+        branch = self.hg_command("branch", *args)
+        return branch.strip()
+
+    def get_branches(self):
+        """ Returns a list of branches from the repo, including versions """
+        branches = self.hg_command("branches")
+        branch_list = branches.strip().split("\n")
+        values = []
+        for branch in branch_list:
+            b = branch.partition(" ")
+            if not b:
+                continue
+            name = b[0].strip()
+            version = b[-1].strip()
+            values.append({'name':name, 'version':version})
+        return values
+
+    def get_branch_names(self):
+        """ Returns a list of branch names from the repo. """
+        branches = self.hg_command("branches")
+        branch_list = branches.strip().split("\n")
+        values = []
+        for branch in branch_list:
+            b = branch.partition(" ")
+            if not b:
+                continue
+            name = b[0]
+            if name:
+                name = name.strip()
+                values.append(name)
+        return values
+
     def hg_status(self, empty=False):
         """Get repository status.
         Returns a dict containing a *change char* -> *file list* mapping, where 

hgapi/testhgapi.py

         self.assertEquals(all_revs[-1].desc, all_revs[-2].desc)
         self.assertNotEquals(all_revs[-2].desc, all_revs[-3].desc)
         
-        
+    def test_190_Branches(self):
+        # make sure there is only one branch and it is default
+        self.assertEquals(self.repo.hg_branch(), "default")
+        branches = self.repo.get_branches()
+        self.assertEquals(len(branches), 1)
+        branch_names = self.repo.get_branch_names()
+        self.assertEquals(len(branch_names), 1)
+        self.assertEquals(branch_names[0], "default")
+
+        # create a new branch, should still be default in branches until we commit
+        # but branch should return the new branch
+        self.assertEquals(self.repo.hg_branch('test_branch'),
+            "marked working directory as branch test_branch")
+        self.assertEquals(self.repo.hg_branch(), "test_branch")
+        branches = self.repo.get_branches()
+        self.assertEquals(len(branches), 1)
+        branch_names = self.repo.get_branch_names()
+        self.assertEquals(len(branch_names), 1)
+        self.assertEquals(branch_names[0], "default")
+
+        # now commit. branch and branches should change to test_branch
+        self.repo.hg_commit("commit test_branch")
+        self.assertEquals(self.repo.hg_branch(), "test_branch")
+        branches = self.repo.get_branches()
+        self.assertEquals(len(branches), 2)
+        branch_names = self.repo.get_branch_names()
+        self.assertEquals(len(branch_names), 2)
 
 def test_doc():
     #Prepare for doctest
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 ProjectModifiedEvent.java.
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.