File: git-root

package info (click to toggle)
git-extras 7.4.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,120 kB
  • sloc: sh: 4,312; python: 994; makefile: 146
file content (30 lines) | stat: -rwxr-xr-x 532 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env bash

git_root() {
  git rev-parse --show-toplevel
}

# get the relative path of current path according to root of repo
git_root_relative() {
    rel=$(git rev-parse --show-prefix)
    if [ -z "$rel" ]; then
        # git rev-parse --show-prefix will output empty string when we are in the root dir
        echo "."
    else
        echo "$rel"
    fi
}

if test $# -eq 0; then
    git_root
else
    case "$1" in
    -r|--relative)
        git_root_relative
        ;;
    *)
        git_root
        ;;
    esac
fi