prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  mercurial project imported from bitbucket stuck in 'Triggered' state
  The docs for pylibftdi are set to be built (via a POST trigger) from https://bitbucket.org/codedstructure/pylibftdi, but builds (https://readthedocs.org/builds/pylibftdi/) are stuck at 'Triggered'.
  
  Based on comments in #435 I set the project up to build against a github mirror, and that worked successfully, so it seems (from #435) that this is likely an hg issue.
  
  
  </issue>
  <code>
  [start of readthedocs/vcs_support/backends/hg.py]
  1 import csv
  2 from StringIO import StringIO
  3 
  4 from projects.exceptions import ProjectImportError
  5 from vcs_support.base import BaseVCS, VCSVersion
  6 
  7 
  8 class Backend(BaseVCS):
  9     supports_tags = True
  10     supports_branches = True
  11     fallback_branch = 'default'
  12 
  13     def update(self):
  14         super(Backend, self).update()
  15         retcode = self.run('hg', 'status')[0]
  16         if retcode == 0:
  17             return self.pull()
  18         else:
  19             return self.clone()
  20 
  21     def pull(self):
  22         pull_output = self.run('hg', 'pull')
  23         if pull_output[0] != 0:
  24             raise ProjectImportError(
  25                 ("Failed to get code from '%s' (hg pull): %s"
  26                  % (self.repo_url, pull_output[0]))
  27             )
  28         update_output = self.run('hg', 'update', '-C')[0]
  29         if update_output[0] != 0:
  30             raise ProjectImportError(
  31                 ("Failed to get code from '%s' (hg update): %s"
  32                  % (self.repo_url, pull_output[0]))
  33             )
  34         return update_output
  35 
  36     def clone(self):
  37         output = self.run('hg', 'clone', self.repo_url, '.')
  38         if output[0] != 0:
  39             raise ProjectImportError(
  40                 ("Failed to get code from '%s' (hg clone): %s"
  41                  % (self.repo_url, output[0]))
  42             )
  43         return output
  44 
  45     @property
  46     def branches(self):
  47         retcode, stdout = self.run('hg', 'branches', '-q')[:2]
  48         # error (or no tags found)
  49         if retcode != 0:
  50             return []
  51         return self.parse_branches(stdout)
  52 
  53     def parse_branches(self, data):
  54         """
  55         stable
  56         default
  57         """
  58 
  59         names = [name.lstrip() for name in data.splitlines()]
  60         return [VCSVersion(self, name, name) for name in names if name]
  61 
  62     @property
  63     def tags(self):
  64         retcode, stdout = self.run('hg', 'tags')[:2]
  65         # error (or no tags found)
  66         if retcode != 0:
  67             return []
  68         return self.parse_tags(stdout)
  69 
  70     def parse_tags(self, data):
  71         """
  72         Parses output of show-ref --tags, eg:
  73 
  74         tip                              278:c4b2d21db51a
  75         0.2.2                            152:6b0364d98837
  76         0.2.1                            117:a14b7b6ffa03
  77         0.1                               50:30c2c6b3a055
  78         """
  79         # parse the lines into a list of tuples (commit-hash, tag ref name)
  80         raw_tags = csv.reader(StringIO(data), delimiter=' ')
  81         vcs_tags = []
  82         for row in raw_tags:
  83             row = filter(lambda f: f != '', row)
  84             if row == []:
  85                 continue
  86             name, commit = row
  87             if name == 'tip':
  88                 continue
  89             revision, commit_hash = commit.split(':')
  90             vcs_tags.append(VCSVersion(self, commit_hash, name))
  91         return vcs_tags
  92 
  93     def checkout(self, identifier=None):
  94         super(Backend, self).checkout()
  95         if not identifier:
  96             identifier = 'tip'
  97         retcode = self.run('hg', 'status')[0]
  98         if retcode == 0:
  99             self.run('hg', 'pull')
  100             return self.run('hg', 'update', '-C', identifier)
  101         else:
  102             self.clone()
  103             return self.run('hg', 'update', '-C', identifier)
  104
  [end of readthedocs/vcs_support/backends/hg.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/readthedocs/vcs_support/backends/hg.py b/readthedocs/vcs_support/backends/hg.py
  --- a/readthedocs/vcs_support/backends/hg.py
  +++ b/readthedocs/vcs_support/backends/hg.py
  @@ -1,6 +1,3 @@
  -import csv
  -from StringIO import StringIO
  -
   from projects.exceptions import ProjectImportError
   from vcs_support.base import BaseVCS, VCSVersion
   
  @@ -69,19 +66,24 @@
   
       def parse_tags(self, data):
           """
  -        Parses output of show-ref --tags, eg:
  +        Parses output of `hg tags`, eg:
  +
  +            tip                              278:c4b2d21db51a
  +            0.2.2                            152:6b0364d98837
  +            0.2.1                            117:a14b7b6ffa03
  +            0.1                               50:30c2c6b3a055
  +            maintenance release 1             10:f83c32fe8126
   
  -        tip                              278:c4b2d21db51a
  -        0.2.2                            152:6b0364d98837
  -        0.2.1                            117:a14b7b6ffa03
  -        0.1                               50:30c2c6b3a055
  +        Into VCSVersion objects with the tag name as verbose_name and the
  +        commit hash as identifier.
           """
  -        # parse the lines into a list of tuples (commit-hash, tag ref name)
  -        raw_tags = csv.reader(StringIO(data), delimiter=' ')
           vcs_tags = []
  -        for row in raw_tags:
  -            row = filter(lambda f: f != '', row)
  -            if row == []:
  +        tag_lines = [line.strip() for line in data.splitlines()]
  +        # starting from the rhs of each line, split a single value (changeset)
  +        # off at whitespace; the tag name is the string to the left of that
  +        tag_pairs = [line.rsplit(None, 1) for line in tag_lines]
  +        for row in tag_pairs:
  +            if len(row) != 2:
                   continue
               name, commit = row
               if name == 'tip':
  
verification_info: {"golden_diff": "diff --git a/readthedocs/vcs_support/backends/hg.py b/readthedocs/vcs_support/backends/hg.py\n--- a/readthedocs/vcs_support/backends/hg.py\n+++ b/readthedocs/vcs_support/backends/hg.py\n@@ -1,6 +1,3 @@\n-import csv\n-from StringIO import StringIO\n-\n from projects.exceptions import ProjectImportError\n from vcs_support.base import BaseVCS, VCSVersion\n \n@@ -69,19 +66,24 @@\n \n     def parse_tags(self, data):\n         \"\"\"\n-        Parses output of show-ref --tags, eg:\n+        Parses output of `hg tags`, eg:\n+\n+            tip                              278:c4b2d21db51a\n+            0.2.2                            152:6b0364d98837\n+            0.2.1                            117:a14b7b6ffa03\n+            0.1                               50:30c2c6b3a055\n+            maintenance release 1             10:f83c32fe8126\n \n-        tip                              278:c4b2d21db51a\n-        0.2.2                            152:6b0364d98837\n-        0.2.1                            117:a14b7b6ffa03\n-        0.1                               50:30c2c6b3a055\n+        Into VCSVersion objects with the tag name as verbose_name and the\n+        commit hash as identifier.\n         \"\"\"\n-        # parse the lines into a list of tuples (commit-hash, tag ref name)\n-        raw_tags = csv.reader(StringIO(data), delimiter=' ')\n         vcs_tags = []\n-        for row in raw_tags:\n-            row = filter(lambda f: f != '', row)\n-            if row == []:\n+        tag_lines = [line.strip() for line in data.splitlines()]\n+        # starting from the rhs of each line, split a single value (changeset)\n+        # off at whitespace; the tag name is the string to the left of that\n+        tag_pairs = [line.rsplit(None, 1) for line in tag_lines]\n+        for row in tag_pairs:\n+            if len(row) != 2:\n                 continue\n             name, commit = row\n             if name == 'tip':\n", "issue": "mercurial project imported from bitbucket stuck in 'Triggered' state\nThe docs for pylibftdi are set to be built (via a POST trigger) from https://bitbucket.org/codedstructure/pylibftdi, but builds (https://readthedocs.org/builds/pylibftdi/) are stuck at 'Triggered'.\n\nBased on comments in #435 I set the project up to build against a github mirror, and that worked successfully, so it seems (from #435) that this is likely an hg issue.\n\n", "before_files": [{"content": "import csv\nfrom StringIO import StringIO\n\nfrom projects.exceptions import ProjectImportError\nfrom vcs_support.base import BaseVCS, VCSVersion\n\n\nclass Backend(BaseVCS):\n    supports_tags = True\n    supports_branches = True\n    fallback_branch = 'default'\n\n    def update(self):\n        super(Backend, self).update()\n        retcode = self.run('hg', 'status')[0]\n        if retcode == 0:\n            return self.pull()\n        else:\n            return self.clone()\n\n    def pull(self):\n        pull_output = self.run('hg', 'pull')\n        if pull_output[0] != 0:\n            raise ProjectImportError(\n                (\"Failed to get code from '%s' (hg pull): %s\"\n                 % (self.repo_url, pull_output[0]))\n            )\n        update_output = self.run('hg', 'update', '-C')[0]\n        if update_output[0] != 0:\n            raise ProjectImportError(\n                (\"Failed to get code from '%s' (hg update): %s\"\n                 % (self.repo_url, pull_output[0]))\n            )\n        return update_output\n\n    def clone(self):\n        output = self.run('hg', 'clone', self.repo_url, '.')\n        if output[0] != 0:\n            raise ProjectImportError(\n                (\"Failed to get code from '%s' (hg clone): %s\"\n                 % (self.repo_url, output[0]))\n            )\n        return output\n\n    @property\n    def branches(self):\n        retcode, stdout = self.run('hg', 'branches', '-q')[:2]\n        # error (or no tags found)\n        if retcode != 0:\n            return []\n        return self.parse_branches(stdout)\n\n    def parse_branches(self, data):\n        \"\"\"\n        stable\n        default\n        \"\"\"\n\n        names = [name.lstrip() for name in data.splitlines()]\n        return [VCSVersion(self, name, name) for name in names if name]\n\n    @property\n    def tags(self):\n        retcode, stdout = self.run('hg', 'tags')[:2]\n        # error (or no tags found)\n        if retcode != 0:\n            return []\n        return self.parse_tags(stdout)\n\n    def parse_tags(self, data):\n        \"\"\"\n        Parses output of show-ref --tags, eg:\n\n        tip                              278:c4b2d21db51a\n        0.2.2                            152:6b0364d98837\n        0.2.1                            117:a14b7b6ffa03\n        0.1                               50:30c2c6b3a055\n        \"\"\"\n        # parse the lines into a list of tuples (commit-hash, tag ref name)\n        raw_tags = csv.reader(StringIO(data), delimiter=' ')\n        vcs_tags = []\n        for row in raw_tags:\n            row = filter(lambda f: f != '', row)\n            if row == []:\n                continue\n            name, commit = row\n            if name == 'tip':\n                continue\n            revision, commit_hash = commit.split(':')\n            vcs_tags.append(VCSVersion(self, commit_hash, name))\n        return vcs_tags\n\n    def checkout(self, identifier=None):\n        super(Backend, self).checkout()\n        if not identifier:\n            identifier = 'tip'\n        retcode = self.run('hg', 'status')[0]\n        if retcode == 0:\n            self.run('hg', 'pull')\n            return self.run('hg', 'update', '-C', identifier)\n        else:\n            self.clone()\n            return self.run('hg', 'update', '-C', identifier)\n", "path": "readthedocs/vcs_support/backends/hg.py"}]}