如何从网址获取git repo

时间:2023-02-04 01:19:51

I have repo git@gitlab.com:alex93ushakov-mail/test.git.

我有repo git@gitlab.com:alex93ushakov-mail / test.git。

In Bash, I'd like to write a function which takes the git url as an argument and returns the repository name 'test'. How to do this?

在Bash中,我想编写一个函数,它将git url作为参数并返回存储库名称'test'。这该怎么做?

2 个解决方案

#1


function foo() {
  local a="$1"          # copy first argument to local variable a
  a="${a##*/}"          # strip left part including /
  echo "${a%.git*}"     # strip trailing .git
}

foo "git@gitlab.com:alex93ushakov-mail/test.git"

Output:

test

To avoid trailing newline: add option -n to echo

要避免尾随换行:将选项-n添加到echo

#2


You could try a regex:

你可以试试一个正则表达式:

s/.+\/(\w+)\\\.git/$+/

Not sure if bash is equivalent to perl, which I normally use, but this would work.

不确定bash是否等同于我通常使用的perl,但这可行。

#1


function foo() {
  local a="$1"          # copy first argument to local variable a
  a="${a##*/}"          # strip left part including /
  echo "${a%.git*}"     # strip trailing .git
}

foo "git@gitlab.com:alex93ushakov-mail/test.git"

Output:

test

To avoid trailing newline: add option -n to echo

要避免尾随换行:将选项-n添加到echo

#2


You could try a regex:

你可以试试一个正则表达式:

s/.+\/(\w+)\\\.git/$+/

Not sure if bash is equivalent to perl, which I normally use, but this would work.

不确定bash是否等同于我通常使用的perl,但这可行。