-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh.fish
90 lines (75 loc) · 2.5 KB
/
gh.fish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function __gh_setup
set --query GH_BASE_DIR
or set --universal GH_BASE_DIR $HOME
set --global gh_cmd_name (basename (status --current-filename) .fish)
set --global gh_version "1.1.5"
set --global gh_git_host github.com
end
function __gh_print_usage
echo "Usage: $gh_cmd_name [OPTION] USER REPO"
echo
echo "Description:"
echo " Quickly navigate across git repositories cloned from GitHub."
echo " Searches within $GH_BASE_DIR/$gh_git_host/. Clones repo if not found."
echo
echo "Examples:"
echo " $gh_cmd_name dideler fish-cd-git"
echo
echo "Options:"
echo " -h, --help Prints help information"
echo " -v, --version Prints the $gh_cmd_name version"
end
function __gh_print_version
echo "$gh_cmd_name version $gh_version"
end
__gh_setup
function $gh_cmd_name -d "Navigate to cloned repos from $gh_git_host" -a user repo
set --local argc (count $argv)
if test $argc -lt 1 -o $argc -gt 2
printf "Error: %s expected 2 arguments, got %d\n\n" $gh_cmd_name $argc 1>&2
__gh_print_usage 1>&2
return 1
end
switch $argv[1]
case '-h' '--help'
__gh_print_usage
return 0
case '-v' '--version'
__gh_print_version
return 0
case '-*'
printf "Error: '%s' is not a valid option\n\n" $argv[1] 1>&2
__gh_print_usage 1>&2
return 1
end
if test $argc -eq 1
set --local user_repo (__gh_autocorrect_user_repo $user)
and __gh_cd_git_repo $gh_git_host $user_repo[1] $user_repo[2]
or cd $GH_BASE_DIR/$gh_git_host/$user
else
__gh_cd_git_repo $gh_git_host $user $repo
end
end
function __gh_cd_git_repo --argument git_host user repo
set --local path $GH_BASE_DIR/$git_host/$user/$repo
__gh_clone_repo_if_missing $path $git_host $user $repo
and cd $path
end
function __gh_clone_repo_if_missing --argument path git_host user repo
# TODO: Use `command git` once we can test it: https://github.com/fisherman/mock/issues/4
if not test -d $path
set --local spinner_msg " @ cloning\r"
set --local git_url "git@$git_host:$user/$repo.git"
set --local clone_cmd "git clone --quiet $git_url $path"
spin --format $spinner_msg $clone_cmd
end
end
# Returns "user" "repo" list when given " user/repo " as input.
function __gh_autocorrect_user_repo --argument user_repo
set --local captures (string match -r "(.+)/(.+)" (string trim $user_repo))
and echo $captures[2]
and echo $captures[3]
end
function __gh_parse_args --argument user_repo
string split --max=1 / (string trim --char=/ $user_repo)
end