1# aliases
2alias hga='hg add'
3alias hgc='hg commit'
4alias hgca='hg commit --amend'
5alias hgci='hg commit --interactive'
6alias hgb='hg branch'
7alias hgba='hg branches'
8alias hgbk='hg bookmarks'
9alias hgco='hg checkout'
10alias hgd='hg diff'
11alias hged='hg diffmerge'
12alias hgp='hg push'
13alias hgs='hg status'
14alias hgsl='hg log --limit 20 --template "{node|short} | {date|isodatesec} | {author|person}: {desc|strip|firstline}\n" '
15alias hgun='hg resolve --list'
16# pull and update
17alias hgi='hg incoming'
18alias hgl='hg pull -u'
19alias hglr='hg pull --rebase'
20alias hgo='hg outgoing'
21alias hglg='hg log --stat -v'
22alias hglgp='hg log --stat  -p -v'
23
24function hgic() {
25  hg incoming "$@" | grep "changeset" | wc -l
26}
27
28function hgoc() {
29  hg outgoing "$@" | grep "changeset" | wc -l
30}
31
32# functions
33function hg_root() {
34  local dir="$PWD"
35  while [[ "$dir" != "/" ]]; do
36    if [[ -d "$dir/.hg" ]]; then
37      echo "$dir"
38      return 0
39    fi
40    dir="${dir:h}"
41  done
42  return 1
43}
44
45function in_hg() {
46  hg_root >/dev/null
47}
48
49function hg_get_branch_name() {
50  local dir
51  if ! dir=$(hg_root); then
52    return
53  fi
54
55  if [[ ! -f "$dir/.hg/branch" ]]; then
56    echo default
57    return
58  fi
59
60  echo "$(<"$dir/.hg/branch")"
61}
62
63function hg_get_bookmark_name() {
64  local dir
65  if ! dir=$(hg_root); then
66    return
67  fi
68
69  if [[ ! -f "$dir/.hg/bookmarks.current" ]]; then
70    return
71  fi
72
73  echo "$(<"$dir/.hg/bookmarks.current")"
74}
75
76function hg_prompt_info {
77  local dir branch dirty
78  if ! dir=$(hg_root); then
79    return
80  fi
81
82  if [[ ! -f "$dir/.hg/branch" ]]; then
83    branch=default
84  else
85    branch="$(<"$dir/.hg/branch")"
86  fi
87
88  dirty="$(hg_dirty)"
89
90  echo "${ZSH_THEME_HG_PROMPT_PREFIX}${branch:gs/%/%%}${dirty}${ZSH_THEME_HG_PROMPT_SUFFIX}"
91}
92
93function hg_dirty {
94  # Do nothing if clean / dirty settings aren't defined
95  if [[ -z "$ZSH_THEME_HG_PROMPT_DIRTY" && -z "$ZSH_THEME_HG_PROMPT_CLEAN" ]]; then
96    return
97  fi
98
99  # Check if there are modifications
100  local hg_status
101  if [[ "$DISABLE_UNTRACKED_FILES_DIRTY" = true ]]; then
102    if ! hg_status="$(hg status -q 2>/dev/null)"; then
103      return
104    fi
105  else
106    if ! hg_status="$(hg status 2>/dev/null)"; then
107      return
108    fi
109  fi
110
111  # grep exits with 0 when dirty
112  if command grep -Eq '^\s*[ACDIMR!?L].*$' <<< "$hg_status"; then
113    echo $ZSH_THEME_HG_PROMPT_DIRTY
114    return
115  fi
116
117  echo $ZSH_THEME_HG_PROMPT_CLEAN
118}
119