hub.bash_completion.sh 9.4 KB
Newer Older
1 2
# hub tab-completion script for bash.
# This script complements the completion script that ships with git.
N
Nathan Broadbent 已提交
3

4 5 6 7 8
# If there is no git tab completion, but we have the _completion loader try to load it
if ! declare -F _git > /dev/null && declare -F _completion_loader > /dev/null; then
  _completion_loader git
fi

9 10
# Check that git tab completion is available and we haven't already set up completion
if declare -F _git > /dev/null && ! declare -F __git_list_all_commands_without_hub > /dev/null; then
N
Nathan Broadbent 已提交
11 12 13 14 15 16 17
  # Duplicate and rename the 'list_all_commands' function
  eval "$(declare -f __git_list_all_commands | \
        sed 's/__git_list_all_commands/__git_list_all_commands_without_hub/')"

  # Wrap the 'list_all_commands' function with extra hub commands
  __git_list_all_commands() {
    cat <<-EOF
18
alias
N
Nathan Broadbent 已提交
19 20 21 22 23
pull-request
fork
create
browse
compare
24
ci-status
25
sync
N
Nathan Broadbent 已提交
26 27 28 29 30 31
EOF
    __git_list_all_commands_without_hub
  }

  # Ensure cached commands are cleared
  __git_all_commands=""
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  ##########################
  # hub command completions
  ##########################

  # hub alias [-s] [SHELL]
  _git_alias() {
    local i c=2 s=-s sh shells="bash zsh sh ksh csh fish"
    while [ $c -lt $cword ]; do
      i="${words[c]}"
      case "$i" in
        -s)
          unset s
          ;;
        *)
          for sh in $shells; do
            if [ "$sh" = "$i" ]; then
              unset shells
              break
            fi
          done
          ;;
      esac
      ((c++))
    done
    __gitcomp "$s $shells"
  }

  # hub browse [-u] [--|[USER/]REPOSITORY] [SUBPAGE]
  _git_browse() {
    local i c=2 u=-u repo subpage
63
    local subpages_="commits issues tree wiki pulls branches stargazers
64
      contributors network network/ graphs graphs/"
65 66
    local subpages_network="members"
    local subpages_graphs="commit-activity code-frequency punch-card"
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    while [ $c -lt $cword ]; do
      i="${words[c]}"
      case "$i" in
        -u)
          unset u
          ;;
        *)
          if [ -z "$repo" ]; then
            repo=$i
          else
            subpage=$i
          fi
          ;;
      esac
      ((c++))
    done
    if [ -z "$repo" ]; then
      __gitcomp "$u -- $(__hub_github_repos '\p')"
    elif [ -z "$subpage" ]; then
      case "$cur" in
        */*)
          local pfx="${cur%/*}" cur_="${cur#*/}"
89 90
          local subpages_var="subpages_$pfx"
          __gitcomp "${!subpages_var}" "$pfx/" "$cur_"
91 92
          ;;
        *)
93
          __gitcomp "$u ${subpages_}"
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
          ;;
      esac
    else
      __gitcomp "$u"
    fi
  }

  # hub compare [-u] [USER[/REPOSITORY]] [[START...]END]
  _git_compare() {
    local i c=$((cword - 1)) u=-u user remote owner repo arg_repo rev
    while [ $c -gt 1 ]; do
      i="${words[c]}"
      case "$i" in
        -u)
          unset u
          ;;
        *)
          if [ -z "$rev" ]; then
            # Even though the logic below is able to complete both user/repo
            # and revision in the right place, when there is only one argument
            # (other than -u) in the command, that argument will be taken as
            # revision. For example:
            # $ hub compare -u upstream
            # > https://github.com/USER/REPO/compare/upstream
            if __hub_github_repos '\p' | grep -Eqx "^$i(/[^/]+)?"; then
              arg_repo=$i
            else
              rev=$i
            fi
          elif [ -z "$arg_repo" ]; then
            arg_repo=$i
          fi
          ;;
      esac
      ((c--))
    done

    # Here we want to find out the git remote name of user/repo, in order to
    # generate an appropriate revision list
    if [ -z "$arg_repo" ]; then
      user=$(__hub_github_user)
      if [ -z "$user" ]; then
        for i in $(__hub_github_repos); do
          remote=${i%%:*}
          repo=${i#*:}
          if [ "$remote" = origin ]; then
            break
          fi
        done
      else
        for i in $(__hub_github_repos); do
          remote=${i%%:*}
          repo=${i#*:}
          owner=${repo%%/*}
          if [ "$user" = "$owner" ]; then
            break
          fi
        done
      fi
    else
      for i in $(__hub_github_repos); do
        remote=${i%%:*}
        repo=${i#*:}
        owner=${repo%%/*}
        case "$arg_repo" in
          "$repo"|"$owner")
            break
            ;;
        esac
      done
    fi

    local pfx cur_="$cur"
    case "$cur_" in
      *..*)
        pfx="${cur_%%..*}..."
        cur_="${cur_##*..}"
        __gitcomp_nl "$(__hub_revlist $remote)" "$pfx" "$cur_"
        ;;
      *)
        if [ -z "${arg_repo}${rev}" ]; then
          __gitcomp "$u $(__hub_github_repos '\o\n\p') $(__hub_revlist $remote)"
        elif [ -z "$rev" ]; then
          __gitcomp "$u $(__hub_revlist $remote)"
        else
          __gitcomp "$u"
        fi
        ;;
    esac
  }

  # hub create [NAME] [-p] [-d DESCRIPTION] [-h HOMEPAGE]
  _git_create() {
    local i c=2 name repo flags="-p -d -h"
    while [ $c -lt $cword ]; do
      i="${words[c]}"
      case "$i" in
        -d|-h)
          ((c++))
193 194 195
          flags=${flags/$i/}
          ;;
        -p)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
          flags=${flags/$i/}
          ;;
        *)
          name=$i
          ;;
      esac
      ((c++))
    done
    if [ -z "$name" ]; then
      repo=$(basename "$(pwd)")
    fi
    case "$prev" in
      -d|-h)
        COMPREPLY=()
        ;;
      -p|*)
        __gitcomp "$repo $flags"
        ;;
    esac
  }

  # hub fork [--no-remote]
  _git_fork() {
    local i c=2 remote=yes
    while [ $c -lt $cword ]; do
      i="${words[c]}"
      case "$i" in
        --no-remote)
          unset remote
          ;;
      esac
      ((c++))
    done
    if [ -n "$remote" ]; then
      __gitcomp "--no-remote"
    fi
  }

234
  # hub pull-request [-f] [-m <MESSAGE>|-F <FILE>|-i <ISSUE>|<ISSUE-URL>] [-b <BASE>] [-h <HEAD>] [-a <USER>] [-M <MILESTONE>] [-l <LABELS>]
235
  _git_pull_request() {
236
    local i c=2 flags="-f -m -F -i -b -h -a -M -l"
237 238 239
    while [ $c -lt $cword ]; do
      i="${words[c]}"
      case "$i" in
240
        -m|-F|-i|-b|-h|-a|-M|-l)
241
          ((c++))
242 243 244
          flags=${flags/$i/}
          ;;
        -f)
245 246 247 248 249 250 251 252 253
          flags=${flags/$i/}
          ;;
      esac
      ((c++))
    done
    case "$prev" in
      -i)
        COMPREPLY=()
        ;;
254
      -b|-h|-a|-M|-l)
255 256 257 258
        # (Doesn't seem to need this...)
        # Uncomment the following line when 'owner/repo:[TAB]' misbehaved
        #_get_comp_words_by_ref -n : cur
        __gitcomp_nl "$(__hub_heads)"
259 260 261 262
        # __ltrim_colon_completions "$cur"
        ;;
      -F)
        COMPREPLY=( "$cur"* )
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        ;;
      -f|*)
        __gitcomp "$flags"
        ;;
    esac
  }

  ###################
  # Helper functions
  ###################

  # __hub_github_user [HOST]
  # Return $GITHUB_USER or the default github user defined in hub config
  # HOST - Host to be looked-up in hub config. Default is "github.com"
  __hub_github_user() {
    if [ -n "$GITHUB_USER" ]; then
      echo $GITHUB_USER
      return
    fi
    local line h k v host=${1:-github.com} config=${HUB_CONFIG:-~/.config/hub}
    if [ -f "$config" ]; then
      while read line; do
        if [ "$line" = "---" ]; then
          continue
        fi
        k=${line%%:*}
        v=${line#*:}
        if [ -z "$v" ]; then
          if [ "$h" = "$host" ]; then
            break
          fi
          h=$k
          continue
        fi
        k=${k#* }
        v=${v#* }
        if [ "$h" = "$host" ] && [ "$k" = "user" ]; then
          echo "$v"
          break
        fi
      done < "$config"
    fi
  }

  # __hub_github_repos [FORMAT]
  # List all github hosted repository
  # FORMAT - Format string contains multiple of these:
  #   \m  remote
  #   \p  owner/repo
  #   \o  owner
  #   escaped characters (\n, \t ...etc) work
  # If omitted, prints all github repos in the format of "remote:owner/repo"
  __hub_github_repos() {
    local f format=$1
    if [ -z "$(__gitdir)" ]; then
      return
    fi
    if [ -z "$format" ]; then
      format='\1:\2'
    else
      format=${format//\m/\1}
      format=${format//\p/\2}
      format=${format//\o/\3}
    fi
327
    command git config --get-regexp 'remote\.[^.]*\.url' |
328 329 330 331 332 333 334 335 336
    grep -E ' ((https?|git)://|git@)github\.com[:/][^:/]+/[^/]+$' |
    sed -E 's#^remote\.([^.]+)\.url +.+[:/](([^/]+)/[^.]+)(\.git)?$#'"$format"'#'
  }

  # __hub_heads
  # List all local "branch", and remote "owner/repo:branch"
  __hub_heads() {
    local i remote repo branch dir=$(__gitdir)
    if [ -d "$dir" ]; then
337
      command git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
338 339 340 341
        "refs/heads/"
      for i in $(__hub_github_repos); do
        remote=${i%%:*}
        repo=${i#*:}
342
        command git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
343 344 345 346 347 348 349 350 351 352 353 354 355
          "refs/remotes/${remote}/" | while read branch; do
          echo "${repo}:${branch#${remote}/}"
        done
      done
    fi
  }

  # __hub_revlist [REMOTE]
  # List all tags, and branches under REMOTE, without the "remote/" prefix
  # REMOTE - Remote name to search branches from. Default is "origin"
  __hub_revlist() {
    local i remote=${1:-origin} dir=$(__gitdir)
    if [ -d "$dir" ]; then
356
      command git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
357 358 359
        "refs/remotes/${remote}/" | while read i; do
        echo "${i#${remote}/}"
      done
360
      command git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
361 362 363 364
        "refs/tags/"
    fi
  }

365 366 367
  # Enable completion for hub even when not using the alias
  complete -o bashdefault -o default -o nospace -F _git hub 2>/dev/null \
    || complete -o default -o nospace -F _git hub
N
Nathan Broadbent 已提交
368
fi