File: gime-git-branch.sh

package info (click to toggle)
libreswan 5.2-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 81,632 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (48 lines) | stat: -rwxr-xr-x 898 bytes parent folder | download
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/sh

if test $# -ne 1; then
    cat >>/dev/stderr <<EOF
Usage:
    $0 <rutdir>
Use a heuristic to determine the branch name of the current detached
head.
EOF
    exit 1
fi

rutdir=$1 ; shift

# Easy way, see what branch HEAD is on.

branch=$(git -C ${rutdir} rev-parse --abbrev-ref HEAD)
if test ${branch} != HEAD ; then
    echo ${branch}
    exit 0
fi

# Hard way, follow checkouts until one hits a branch.

git -C ${rutdir} reflog | awk '
BEGIN {
  found = ""
}
found == "" && $3 == "checkout:" && (sha == "" || sha == $1 ) {
  from = $(NF - 2)
  to = $NF
  # print "checkout:", "from=" from, "to=" to, "length(from)=" length(from) >> "/dev/stderr"
  if (length(from) != 40) {
    found = from
    # print "found=" found >> "/dev/stderr"
  } else {
    sha=substr(from, 1, 7)
    # print "sha=" sha >> "/dev/stderr"
  }
}
END {
  if (found) {
    print found
  } else {
    exit 1
  }
}
'