1
1
gitcd () {
2
2
basedir=" $HOME /Code"
3
- # 如果存在全局的 GITCD_HOME,就用它
3
+ # If global GITCD_HOME exists, use it
4
4
[[ -n $GITCD_HOME ]] && {
5
5
basedir=$GITCD_HOME
6
6
}
7
7
8
- # 用户传入的参数
8
+ # User input parameter
9
9
url=$1
10
10
11
- # 忘记输入参数,就跳出
11
+ # If no parameter is provided, exit
12
12
[[ $url == " " ]] && {
13
- # 显示红色加粗
13
+ # Display in red and bold
14
14
print -P " %B%F{red}[gitcd] Please input your repo url."
15
15
return
16
16
}
17
17
18
- # 通过 git url 计算出路径来
18
+ # Calculate the directory path from the git URL
19
19
dir=$( _giturl2dir $url )
20
20
target=$basedir /$dir
21
21
22
- # 如果文件夹已经存在,那直接 cd 过去就好了
22
+ # If the directory already exists, just cd into it
23
23
[[ -d $target ]] && {
24
- # 显示绿色加粗
24
+ # Display in green and bold
25
25
print -P " %B[gitcd] $target %F{green}already exists."
26
26
cd $target
27
27
return
28
28
}
29
29
30
30
git clone $url $target && cd $target
31
- # 显示绿色加粗
31
+ # Display in green and bold
32
32
print -P " %B%F{green}[gitcd] Done. Have a great day!"
33
33
}
34
34
35
35
_giturl2dir () {
36
- # 规则参考了 https://github.com/repo-utils/giturl/blob/master/lib/giturl.js
36
+ # Rules referenced from https://github.com/repo-utils/giturl/blob/master/lib/giturl.js
37
37
38
38
url=$1
39
- # 删掉 @ 和之前的内容,即 `git@` ` || `https://jpillora@` => ""
40
- url=${url#*@ } # 删除左端匹配到的内容,最小匹配
41
- # 删掉协议头,即 `git://` || `git+https://` => ""
39
+ # Remove `@` and everything before it, e.g., `git@` || `https://jpillora@` => ""
40
+ url=${url#*@ } # Remove left-matched content, minimal match
41
+ # Remove protocol header, e.g., `git://` || `git+https://` => ""
42
42
url=${url#*:// }
43
- # 删掉末尾的 .git,即 .git => ""
43
+ # Remove ` .git` at the end, e.g., .git => ""
44
44
url=${url% .git* }
45
- # 替换掉第一个冒号,即 github.com:foo/bar => github.com/foo/bar
45
+ # Replace the first colon with a slash, e.g., github.com:foo/bar => github.com/foo/bar
46
46
url=${url/:/ \/ }
47
47
48
48
[[ -n $GITCD_USEHOST ]] && [[ " ${GITCD_USEHOST: l} " == " false" ]] && {
49
49
# echo $url
50
50
url=$( echo $url | cut -d " /" -f 2-)
51
51
}
52
52
53
- # 用 echo 返回给调用方,而不是用 return
53
+ # Return to the caller using echo instead of return
54
54
echo $url
55
- }
55
+ }
0 commit comments