Skip to main content
2 of 2
added 96 characters in body
janos
  • 111.7k
  • 15
  • 152
  • 391

Getting the url of origin

This is a bit awkward:

remote=$(grep -A 1 "remote \"origin\"" $(pwd)/.git/config | sed -n 2p)

Because:

  • The grep + sed is a bit hacky
  • $(pwd) can be replaced with a simple .
  • It's awkward to rely on .git/config content instead of some git command

Try to look for native Git commands to extract this kind of information, for example:

git remote show -n origin | grep Fetch.URL

The output is not exactly same as the original, but it will work fine with the rest of your script.

Named parameters

In Bash there are no named parameters. As a consequence, in this function it can be hard to remember what $1 refers to:

open_site() {
  # currently only works for SSH addresses
  if [[ $remote == *@* ]]; then
    name=$(echo $remote | sed  "s/^.*:\(.*\)\.git/\1/g")
    ext=$(get_ext $1)
    open "$1/${name}${ext}"
  fi
}

As a workaround, I recommend assigning the parameter to a well-named local variable, like this:

open_site() {
  local baseurl=$1
  # currently only works for SSH addresses
  if [[ $remote == *@* ]]; then
    name=$(echo $remote | sed  "s/^.*:\(.*\)\.git/\1/g")
    ext=$(get_ext $baseurl)
    open "$baseurl/${name}${ext}"
  fi
}

Pattern replacement

Instead of this:

name=$(echo $remote | sed  "s/^.*:\(.*\)\.git/\1/g")

I recommend simple pattern substitution:

# chop off the start until the last :
remote=${remote##*:}
# chop off .git at the end
remote=${remote%.git}

The benefit of this approach is that it doesn't run extra programs (no sed, no echo, no piping).

A minor note, the original sed didn't need the g flag.

Printing a blank line

You don't need "" to just echo a blank line:

echo ""

This is exactly the same:

echo
janos
  • 111.7k
  • 15
  • 152
  • 391