scmprompt: output only one remote bookmark

Summary:
Recently I had this scm prompt:
{F64910275}

This is not user friendly. I suggest to output at most one remote bookmark

Also this diff copies `seq.py` from upstream hg repo

Test Plan: Run scm prompt tests on devserver and mac and with '--shell=zsh'

Reviewers: rmcelroy

Reviewed By: rmcelroy

Subscribers: simonfar, wez, mjpieters, #sourcecontrol

Differential Revision: https://phabricator.intern.facebook.com/D4197242

Signature: t1:4197242:1483466446:cd1fe943dc99fde0600a4e20994fa4b0f971f72a
This commit is contained in:
Stanislau Hlebik 2017-01-04 00:23:16 -08:00
parent bdb60ea581
commit 6f3cfe5684
3 changed files with 67 additions and 6 deletions

View File

@ -51,6 +51,24 @@
# =========================================================================
#
_find_most_relevant_remotebookmark()
{
# We don't want to output all remote bookmarks because there can be many
# of them. This function finds the most relevant remote bookmark using this
# algorithm:
# 1. If 'master' or '@' bookmark is available then output it
# 2. Sort remote bookmarks and output the first in reverse sorted order (
# it's a heuristic that tries to find the newest bookmark. It will work well
# with bookmarks like 'release20160926' and 'release20161010').
relevantbook=$(command grep -m1 -E -o "^[^/]+/(master|@)$" <<< "$1")
if [[ -n $relevantbook ]]; then
command echo $relevantbook
return 0
fi
command echo "$(command sort -r <<< "$1" | command head -n 1)"
}
_scm_prompt()
{
local dir git hg fmt
@ -115,10 +133,14 @@ _scm_prompt()
fi
local remote="$hg/.hg/remotenames"
if [[ -f "$remote" ]]; then
local marks=$(command grep "^$dirstate bookmarks" "$remote" | \
command cut -f 3 -d ' ' | command tr '\n' '|' | command sed -e 's/|$//')
if [[ -n "$marks" ]]; then
br="$br|$marks"
local allremotemarks="$(command grep "^$dirstate bookmarks" "$remote" | \
command cut -f 3 -d ' ')"
if [[ -n "$allremotemarks" ]]; then
local remotemark="$(_find_most_relevant_remotebookmark "$allremotemarks")"
if [[ -n "$remotemark" ]]; then
br="$br|$remotemark..."
fi
fi
fi
local branch

24
tests/seq.py Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python
#
# A portable replacement for 'seq'
#
# Usage:
# seq STOP [1, STOP] stepping by 1
# seq START STOP [START, STOP] stepping by 1
# seq START STEP STOP [START, STOP] stepping by STEP
from __future__ import absolute_import, print_function
import sys
start = 1
if len(sys.argv) > 2:
start = int(sys.argv[1])
step = 1
if len(sys.argv) > 3:
step = int(sys.argv[2])
stop = int(sys.argv[-1]) + 1
for i in xrange(start, stop, step):
print(i)

View File

@ -156,7 +156,7 @@ Test remotenames
4b6cc7d5194bd5dbf63970015ec75f8fd1de6dba
$ echo 4b6cc7d5194bd5dbf63970015ec75f8fd1de6dba bookmarks remote/@ > .hg/remotenames
$ cmd
(4b6cc7d|remote/@)
(4b6cc7d|remote/@...)
Test shared bookmarks
$ cmd cd ..
@ -190,7 +190,7 @@ Test branches
$ cmd hg branch blah
marked working directory as branch blah
(branches are permanent and global, did you want a bookmark?)
(4b6cc7d|remote/@|blah)
(4b6cc7d|remote/@...|blah)
$ cmd hg commit -m blah
(a742469|blah)
$ cmd hg up -q default
@ -230,3 +230,18 @@ Test formatting options
97af35b
$ _scm_prompt ':%s:'
:97af35b: (no-eol)
Test many remotenames
$ hg log -r . -T '{node}\n'
97af35b3648c0098cbd8114ae1b1bafab997ac20
$ for i in `python $TESTDIR/seq.py 1 10`; do
> echo 97af35b3648c0098cbd8114ae1b1bafab997ac20 bookmarks remote/remote$i >> .hg/remotenames
> done
$ cmd
(97af35b|remote/remote9...)
$ echo 97af35b3648c0098cbd8114ae1b1bafab997ac20 bookmarks remote/abc/master >> .hg/remotenames
$ cmd
(97af35b|remote/remote9...)
$ echo 97af35b3648c0098cbd8114ae1b1bafab997ac20 bookmarks remote/@ >> .hg/remotenames
$ cmd
(97af35b|remote/@...)