git.fiddlerwoaroof.com
Browse code

Add git-get for easily cloning git repos

Ed Langley authored on 13/04/2019 18:41:31
Showing 2 changed files
... ...
@@ -1,3 +1,4 @@
1
+#:depends-on:git-get
1 2
 GIT_DEBUG=0
2 3
 
3 4
 git-update-repos() {
... ...
@@ -93,6 +94,10 @@ for line in sys.stdin:
93 94
 }
94 95
 
95 96
 alias git-msg=git-messages
97
+alias git-cj="git-get cj"
98
+alias git-hub="git-get github"
99
+alias git-lab="git-get gitlab"
100
+alias gh="git-hub"
96 101
 
97 102
 GIT_CMD="`which -p git 2>/dev/null`"
98 103
 GTI_CMD="`which -p gti 2>/dev/null`"
... ...
@@ -101,11 +106,18 @@ if [[ ! -z "$GIT_CMD" ]]; then
101 106
   # git wrapper that mimics the functionality of git for commandlets but also
102 107
   # searches shell functions.
103 108
   git() {
104
-    local POSSIBLE_CMD
105
-    POSSIBLE_CMD="$(expand-alias git-$1)"
109
+    local possible_cmd
110
+    local cmdlets
106 111
 
107
-    if is-function "$POSSIBLE_CMD"; then
108
-      $POSSIBLE_CMD "${@[2,-1]}"
112
+    possible_cmd="${${$(expand-alias "git-$1")#'}%'}"
113
+    printf "%s" "$possible_cmd" | read -A cmdlets 
114
+
115
+    if [[ "$GIT_DEBUG" != "0" ]]; then
116
+      echo "git: looking for: \"$possible_cmd\" command: \"${cmdlets[1]}\""
117
+    fi
118
+
119
+    if is-function "${cmdlets[1]}"; then
120
+      "${cmdlets[@]}" "${@[2,-1]}"
109 121
     else
110 122
       "$GIT_CMD" "$@"
111 123
     fi
112 124
new file mode 100644
... ...
@@ -0,0 +1,97 @@
1
+GIT_3DP_DIR="${GIT_3DP_DIR:-"$HOME/git_repos/3dp"}"
2
+
3
+typeset -g -A FORGE_ALIASES
4
+
5
+alias_usage="alias_forge <alias> <forge>"
6
+function alias_forge() {
7
+  local alias=${1:?the first parameter to alias_forge should be the alias you want to set}
8
+  local forge=${2:?the first parameter to alias_forge should be the forge the alias points to}
9
+
10
+  if (( $# > 2 )); then
11
+    printf "%s" "$alias_usage"
12
+  else
13
+    FORGE_ALIASES[$alias]=$forge
14
+  fi
15
+}
16
+
17
+alias_forge gh github
18
+alias_forge gl gitlab
19
+
20
+GITHUB_USE_SSH=${GIT_USE_SSH:-${GITHUB_USE_SSH:-yes}}
21
+function github_url() {
22
+  local git_spec="$package"
23
+
24
+  if [[ -n "$git_user" ]]; then
25
+    git_spec="$git_user/$package"
26
+  fi
27
+
28
+  if [[ "$GITHUB_USE_SSH" == "yes" ]]; then
29
+    printf "git@github.com:%s.git" "$git_spec"
30
+  else
31
+    printf "https://github.com/%s.git" "$git_spec"
32
+  fi
33
+}
34
+
35
+GITLAB_USE_SSH=${GIT_USE_SSH:-${GITLAB_USE_SSH:-yes}}
36
+function gitlab_url() {
37
+  local git_spec="$package"
38
+
39
+  if [[ -n "$git_user" ]]; then
40
+    git_spec="$git_user/$package"
41
+  fi
42
+
43
+  if [[ "$GITLAB_USE_SSH" == "yes" ]]; then
44
+    printf "git@gitlab.com:%s.git" "$git_spec"
45
+  else
46
+    printf "https://gitlab.com/%s.git" "$git_spec"
47
+  fi
48
+}
49
+
50
+function git-forge-clone() {
51
+  if [[ ! -e "$(basename "$package")" ]]; then
52
+    git clone "$($1)"
53
+  else
54
+    echo "package already cloned"
55
+  fi
56
+}
57
+
58
+function get_forge_function() {
59
+  local forge="$1"
60
+  local aliased_forge="${forge_aliases[$1]}"
61
+  if [[ ! -z "$aliased_forge" ]]; then
62
+    forge="$aliased_forge"
63
+  fi
64
+  echo "${forge}_url"
65
+}
66
+
67
+function git-get() {
68
+  local git_user
69
+  local package
70
+  
71
+  forge=${1?Need a forge spec}
72
+
73
+  shift 
74
+
75
+  if [[ $# == 1 ]]; then
76
+    git_user=
77
+    package=$1
78
+  elif (( $# == 2 )); then
79
+    git_user=$1
80
+    package=$2
81
+    shift
82
+  else
83
+    echo 'usage: <forge> <user> <package> or <forge> <package>'
84
+    return 2
85
+  fi
86
+
87
+  package=${package%.git}
88
+
89
+  shift
90
+
91
+  cd "$GIT_3DP_DIR"
92
+
93
+  local forge_url_function="${$(get_forge_function "$forge"):?forge not recognized}"
94
+  git-forge-clone "$forge_url_function"
95
+  
96
+  cd "$(basename "$package")"
97
+}