aboutsummaryrefslogtreecommitdiff
path: root/git-submodule-checkout-branch
blob: 6613bf47fcbbdf59db72bc51b82e5c9a82aafaa5 (plain)
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
#!/bin/bash
#
# Checkout branche a submodule.
#

# Parameters
BASENAME="`basename $0`"

# Checkout the branch containing a commit
function checkout_branch {
  # Check if we are in a detached HEAD
  if git branch | grep -q '* (HEAD detached'; then
    # Determine the commit we're in
    local commit="`git log -n 1 | head -1 | cut -d ' ' -f 2`"

    # Get the first remote branch that contains our commit
    #
    # In the future some criteria might be stablished to determine how to decide
    # if the comment is present in more than one branch. Which one to prioritize?
    # A topic branch in the form of "feature/"? The "develop" branch?
    local branch="`git branch -r --contains $commit 2> /dev/null | grep -v 'HEAD' | head -1 | sed -e 's|^[^/]*/||'`"

    # Checkout to the given commit
    if [ ! -z "$branch" ]; then
      git checkout $branch
    else
      echo "$BASENAME: no such branch containing dangling commit $commit"
    fi
  fi
}

# Dispatch
checkout_branch