File: git-psykorebase

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 (83 lines) | stat: -rwxr-xr-x 2,346 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env bash
PROGRAM=$0
PRIMARY_BRANCH=""
SECONDARY_BRANCH=""
FF="--ff"
CONTINUE="no"

current_branch() {
    git rev-parse --abbrev-ref HEAD
}

function usage()
{
    echo "USAGE: ${PROGRAM} PRIMARY_BRANCH [SECONDARY_BRANCH] [--no-ff]"
    echo "USAGE: ${PROGRAM} --continue"
    echo ""
    echo "OPTIONS:"
    echo "  --no-ff:			Force rebase commit."
    echo "  -c|--continue:		Continue after the user updates conflicts."
}

while [[ $# -gt 0 ]]
do
  key="$1"

  case $key in
      --no-ff)
      FF="--no-ff"
      ;;
      -c|--continue)
      CONTINUE=yes
      ;;
      *)
      if [[ "$PRIMARY_BRANCH" == "" ]]; then
          PRIMARY_BRANCH=$key
      elif [[ "$SECONDARY_BRANCH" == "" ]]; then
          SECONDARY_BRANCH=$key
      else
          usage
          exit 20  # Error during arguments parsing
      fi
      ;;
  esac
  shift  # past argument or value
done

if [[ "$SECONDARY_BRANCH" == "" ]]; then
    SECONDARY_BRANCH=$(current_branch)
fi

if [[ "$CONTINUE" == "yes" ]]; then
    TARGET_BRANCH=$(current_branch)
    SECONDARY_BRANCH=${TARGET_BRANCH%"-rebased-on-top-of-"*}
    PRIMARY_BRANCH=${TARGET_BRANCH#*"-rebased-on-top-of-"}
    if [[ "${SECONDARY_BRANCH}-rebased-on-top-of-${PRIMARY_BRANCH}" != $TARGET_BRANCH ]]; then
        echo "Couldn't continue rebasing on ${TARGET_BRANCH}"
        exit 30  # Impossible to detect PRIMARY_BRANCH AND SECONDARY_BRANCH
    fi

    echo "Continuing rebasing of $SECONDARY_BRANCH on top of $PRIMARY_BRANCH"
    git commit || exit 51
    git branch -d "${SECONDARY_BRANCH}"  || exit 52
    git branch -m "${TARGET_BRANCH}" "${SECONDARY_BRANCH}" || exit 53

elif [[ "$PRIMARY_BRANCH" == "" ]]; then
    usage
    exit 10  # Missing arguments PRIMARY_BRANCH
else
    echo "Rebasing $SECONDARY_BRANCH on top of $PRIMARY_BRANCH"
    TARGET_BRANCH="${SECONDARY_BRANCH}-rebased-on-top-of-${PRIMARY_BRANCH}"

    git checkout "${PRIMARY_BRANCH}" || exit 41
    git checkout -b "${TARGET_BRANCH}" || exit 42

    if git merge "${SECONDARY_BRANCH}" ${FF} \
        -m "Psycho-rebased branch ${SECONDARY_BRANCH} on top of ${PRIMARY_BRANCH}"; then
        git branch -d "${SECONDARY_BRANCH}" || exit 43
        git branch -m "${TARGET_BRANCH}" "${SECONDARY_BRANCH}" || exit 44
    else
        echo "Resolve the conflict and run ``${PROGRAM} --continue``."
        exit 1
    fi
fi