Paul
Paul

Reputation: 4460

Bash git ls-remote remove the hash via regular expression

I have the following command which allows me to get a list like the one below:

git ls-remote https://github.com/CodeEditApp/CodeEdit --h --sort origin "refs/heads/*"
...
45955931b326913390596b1970ebeb928ccc741e    refs/heads/add-docs-video
4d872980b6a606b9d40c9c3afa5987bbac701fc2    refs/heads/animation-test
c3969f86ea8e332c5b7e63ea8d246d5e7917d475    refs/heads/apple
....

the result I would like to get in the end would be just the branch names:

...
add-docs-video
animation-test
apple
....

Upvotes: 0

Views: 592

Answers (2)

torek
torek

Reputation: 489638

As terrorrussia-keeps-killing noted, the output from git ls-remote is well-formatted. I'd personally use:

git ls-remote ... 'refs/heads/*' | sed "s,.*${TAB}refs/heads/,,"

for this case, with $TAB set to a tab character (and re-insert desired URL and options into the git ls-remote of course):

TAB=$'\t'

You can use a literal tab character rather than expanding the variable ${TAB}, or you can use the $'...' syntax in the sed command itself:

... | sed $'s,.*\trefs/heads/,,

but I like to use named variables ($TAB, $NL, etc.) to hold my white-space characters for readability. The need for variable-name expansion is why the argument to sed is in double quotes, vs the stronger single quotes I use elsewhere, but in this case you could in fact use double quotes everywhere, or—with the $'...' syntax—single quotes everywhere (but another reason to use a variable like $TAB is that the $'...' syntax can be misleading).

Upvotes: 2

kiyell
kiyell

Reputation: 432

You could use awk and grab the last field using / as a separator and then print the rest of the line from the 3rd field using the substring function like so:

yourcommand | awk -F/ '{print substr($0, index($0,$3))}'

Upvotes: 2

Related Questions