5334581e231a

Merge.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Wed, 05 Oct 2016 20:52:29 +0000
parents 4d7402d28abf (diff) 064a8c1d11be (current diff)
children 4dddc56c4c4b
branches/tags (none)
files prompt.py

Changes

--- a/.hgignore	Wed Oct 05 11:17:48 2016 +0200
+++ b/.hgignore	Wed Oct 05 20:52:29 2016 +0000
@@ -2,5 +2,7 @@
 
 .DS_Store
 *.pyc
+.idea/
 docs/.html
 docs/.tmp
+
--- a/prompt.py	Wed Oct 05 11:17:48 2016 +0200
+++ b/prompt.py	Wed Oct 05 20:52:29 2016 +0000
@@ -14,10 +14,15 @@
 import os
 import subprocess
 from datetime import datetime, timedelta
+from contextlib import closing
 from os import path
 from mercurial import extensions, commands, cmdutil, help
+from mercurial.i18n import _
 from mercurial.node import hex, short
 
+cmdtable = {}
+command = cmdutil.command(cmdtable)
+
 # `revrange' has been moved into module `scmutil' since v1.9.
 try :
     from mercurial import scmutil
@@ -30,17 +35,33 @@
 
 FILTER_ARG = re.compile(r'\|.+\((.*)\)')
 
+
 def _cache_remote(repo, kind):
     cache = path.join(repo.root, CACHE_PATH, kind)
     c_tmp = cache + '.temp'
 
-    # This is kind of a hack and I feel a little bit dirty for doing it.
-    IGNORE = open('NUL:','w') if subprocess.mswindows else open('/dev/null','w')
+    popenargs = ['hg', kind, '--quiet']
+    remote_path = repo.ui.config('prompt', 'remote')
+    if remote_path is not None:
+        popenargs.append(remote_path)
 
-    subprocess.call(['hg', kind, '--quiet'], stdout=file(c_tmp, 'w'), stderr=IGNORE)
+    null_path = 'NUL:' if subprocess.mswindows else '/dev/null'
+    with open(null_path, 'w') as null_fp:
+        with open(c_tmp, 'w') as stdout_fp:
+            exit_code = subprocess.call(popenargs, stdout=stdout_fp, stderr=null_fp)
+
+    if exit_code not in (0, 1): # (changesets_found, changesets_not_found)
+        msg = "hg-prompt error: "
+        if remote_path: # Failure likely due to bad remote. Is 255 a valid check?
+            msg += "Can't access remote '%s'" % remote_path
+        else:
+            msg += "Error attempting 'hg %s'" % kind
+        print msg
+
     os.rename(c_tmp, cache)
     return
 
+
 def _with_groups(groups, out):
     out_groups = [groups[0]] + [groups[-1]]
 
@@ -72,6 +93,11 @@
     else:
         return None
 
+@command('prompt',
+         [('', 'angle-brackets', None, 'use angle brackets (<>) for keywords'),
+          ('', 'cache-incoming', None, 'used internally by hg-prompt'),
+          ('', 'cache-outgoing', None, 'used internally by hg-prompt')],
+         'hg prompt STRING')
 def prompt(ui, repo, fs='', **opts):
     '''get repository information for use in a shell prompt
 
@@ -117,6 +143,8 @@
             book = getattr(repo, '_bookmarkcurrent', None)
         except KeyError:
             book = getattr(repo, '_bookmarkcurrent', None)
+        if book is None:
+            book = getattr(repo, '_activebookmark', None)
         if book:
             cur = repo['.'].node()
             if repo._bookmarks[book] == cur:
@@ -281,10 +309,12 @@
             if cache_exists:
                 with open(cache) as c:
                     count = len(c.readlines())
-                    if g[1]:
-                        return _with_groups(g, str(count)) if count else ''
+                    if g[1] and count > 0:
+                        return _with_groups(g, str(count))
+                    elif g[2]:
+                        return _with_groups(g, '0') if not count else ''
                     else:
-                        return _with_groups(g, '') if count else ''
+                        return _with_groups(g, '')
             else:
                 return ''
         return _r
@@ -367,7 +397,7 @@
                 tip = head
                 break
 
-        return _with_groups(m.groups(), '^') if current_rev != repo[tip] else ''
+        return _with_groups(m.groups(), '^') if current_rev.children() else ''
 
     if opts.get("angle_brackets"):
         tag_start = r'\<([^><]*?\<)?'
@@ -423,8 +453,14 @@
             ')*': _tip,
         'update': _update,
 
-        'incoming(\|count)?': _remote('incoming'),
-        'outgoing(\|count)?': _remote('outgoing'),
+        'incoming(?:'
+            '(\|count)'
+            '|(\|zero)'
+            ')*': _remote('incoming'),
+        'outgoing(?:'
+            '(\|count)'
+            '|(\|zero)'
+            ')*': _remote('outgoing')
     }
 
     if opts.get("cache_incoming"):
@@ -464,18 +500,9 @@
     except KeyError:
         pass
 
-cmdtable = {
-    "prompt":
-    (prompt, [
-        ('', 'angle-brackets', None, 'use angle brackets (<>) for keywords'),
-        ('', 'cache-incoming', None, 'used internally by hg-prompt'),
-        ('', 'cache-outgoing', None, 'used internally by hg-prompt'),
-    ],
-    'hg prompt STRING')
-}
 help.helptable += (
-    (['prompt-keywords', 'prompt-keywords'], ('Keywords supported by hg-prompt'),
-     (r'''hg-prompt currently supports a number of keywords.
+    (['prompt-keywords'], _('Keywords supported by hg-prompt'),
+     lambda _: r'''hg-prompt currently supports a number of keywords.
 
 Some keywords support filters.  Filters can be chained when it makes
 sense to do so.  When in doubt, try it!
@@ -515,6 +542,8 @@
 
      |count
          Display the number of incoming changesets (if greater than 0).
+     |zero
+         Display 0 if there are no incoming changesets.
 
 node
      Display the (full) changeset hash of the current parent.
@@ -538,6 +567,8 @@
 
      |count
          Display the number of outgoing changesets (if greater than 0).
+     |zero
+         Display 0 if there are no incoming changesets.
 
 patch
      Display the topmost currently-applied patch (requires the mq
@@ -654,5 +685,5 @@
      Display `^` if the current parent is not the tip of the current branch,
      otherwise nothing.  In effect, this lets you see if running `hg update`
      would do something.
-''')),
+'''),
 )
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_bookmark.py	Wed Oct 05 20:52:29 2016 +0000
@@ -0,0 +1,19 @@
+'''Test output of {bookmark}.'''
+
+from nose import *
+from util import *
+
+@with_setup(setup_sandbox, teardown_sandbox)
+def test_bookmark():
+    hg_bookmark('test')
+
+    output = prompt(fs='{bookmark}')
+    assert output == 'test'
+
+@with_setup(setup_sandbox, teardown_sandbox)
+def test_no_bookmark():
+    hg_bookmark('test')
+    hg_deactivate_bookmark()
+
+    output = prompt(fs='{bookmark}')
+    assert output == ''
--- a/tests/util.py	Wed Oct 05 11:17:48 2016 +0200
+++ b/tests/util.py	Wed Oct 05 20:52:29 2016 +0000
@@ -41,6 +41,12 @@
 def hg_branch(branch='test'):
     commands.branch(_ui, get_sandbox_repo(), branch)
 
+def hg_bookmark(bookmark='test'):
+    commands.bookmark(_ui, get_sandbox_repo(), bookmark)
+
+def hg_deactivate_bookmark():
+    commands.bookmark(_ui, get_sandbox_repo(), inactive=True)
+
 def hg_update(rev):
     opts = { 'rev': str(rev), }
     commands.update(_ui, get_sandbox_repo(), **opts)