Neels Hofmeyr | 45f3cec | 2017-08-13 03:25:38 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | Git() { |
| 3 | echo "git $@" |
| 4 | git $@ |
| 5 | if [ "$?" != "0" ]; then |
| 6 | echo "GIT RETURNED ERROR!" |
| 7 | exit 1 |
| 8 | fi |
| 9 | } |
| 10 | |
| 11 | Git_may_fail() { |
| 12 | git $@ |
| 13 | } |
| 14 | |
| 15 | Git_branch() { |
| 16 | echo "$(Git -C "$dir" status)" | grep 'On branch' | sed 's/On branch //' |
| 17 | } |
| 18 | |
| 19 | |
| 20 | gitk_start() { |
| 21 | if [ -n "$DISPLAY" ]; then |
| 22 | gitk --all & |
| 23 | gitk_started="1" |
| 24 | fi |
| 25 | } |
| 26 | |
| 27 | dance() { |
| 28 | echo |
| 29 | echo |
| 30 | br="$(Git_branch)" |
| 31 | |
| 32 | echo "$dir" |
| 33 | cd "$dir" |
| 34 | |
| 35 | st="$(Git status)" |
| 36 | mods="$(echo "$st" | grep 'modified:')" |
| 37 | |
| 38 | stline="$(echo "$st" | grep '\(behind\|ahead\|up-to-date\|diverged\)')" |
| 39 | |
| 40 | echo "$br" |
| 41 | echo "$stline" |
| 42 | |
| 43 | if [ -z "$mods" -a -n "$(echo "$stline" | grep up-to-date)" ]; then |
| 44 | return 0 |
| 45 | fi |
| 46 | |
| 47 | gitk_start |
| 48 | |
| 49 | if [ -n "$(echo "$stline" | grep behind)" ]; then |
| 50 | echo "Behind. git merge? (empty = no, 'ok' = yes)" |
| 51 | read ok |
| 52 | if [ "x$ok" = xok ]; then |
| 53 | Git merge |
| 54 | fi |
| 55 | elif [ -n "$(echo "$stline" | grep ahead)" ]; then |
| 56 | echo "Ahead. commit to new branch? (enter name, empty = no)" |
| 57 | read wipbranch |
| 58 | if [ -n "$wipbranch" ]; then |
| 59 | Git checkout -b "$wipbranch" |
| 60 | Git_may_fail commit -am wip |
| 61 | #Git push --set-upstream origin "$wipbranch" |
| 62 | Git checkout "$br" |
| 63 | fi |
| 64 | echo "$br: git reset --hard origin/$br ? (empty = no, 'ok' = yes)" |
| 65 | read ok |
| 66 | if [ "x$ok" = xok ]; then |
| 67 | Git reset --hard "origin/$br" |
| 68 | fi |
| 69 | return 0 |
| 70 | elif [ -n "$(echo "$stline" | grep diverged)" ]; then |
| 71 | echo "Diverged. git reset --hard origin/$br ? (empty = no, 'ok' = yes)" |
| 72 | read ok |
| 73 | if [ "x$ok" = xok ]; then |
| 74 | wipbranch="neels/wip_$(date +%Y%m%d_%H%M)" |
| 75 | Git checkout -b "$wipbranch" |
| 76 | Git_may_fail commit -am wip |
| 77 | Git checkout "$br" |
| 78 | Git reset --hard "origin/$br" |
| 79 | fi |
| 80 | elif [ -z "$(echo "$stline" | grep up-to-date)" ]; then |
| 81 | echo "Nothing to do." |
| 82 | echo "$st" |
| 83 | fi |
| 84 | |
| 85 | if [ -n "$mods" ]; then |
| 86 | echo "Local mods" |
| 87 | echo "$mods" |
| 88 | echo |
| 89 | echo "commit to new branch? (enter name, empty = no)" |
| 90 | read wipbranch |
| 91 | if [ -n "$wipbranch" ]; then |
| 92 | Git checkout -b "$wipbranch" |
| 93 | Git_may_fail commit -am wip |
| 94 | #Git push --set-upstream origin "$wipbranch" |
| 95 | Git checkout "$br" |
| 96 | return 0 |
| 97 | fi |
| 98 | |
| 99 | echo "commit to this branch $br ? (empty = no, 'ok' = yes)" |
| 100 | read ok |
| 101 | if [ "x$ok" = xok ]; then |
| 102 | Git commit -am wip |
| 103 | #Git push |
| 104 | return 0 |
| 105 | fi |
| 106 | fi |
| 107 | } |
| 108 | |
| 109 | kill_gitk() { |
| 110 | if [ "$gitk_started" = "1" ]; then |
| 111 | kill %1 |
| 112 | gitk_started="0" |
| 113 | fi |
| 114 | } |
| 115 | |
| 116 | |
| 117 | basedir="$(pwd)" |
| 118 | gitk_started="0" |
| 119 | for gitdir in */.git ; do |
| 120 | cd "$basedir" |
| 121 | dir="$(dirname "$gitdir")" |
| 122 | |
| 123 | orig_branch="$(Git_branch)" |
| 124 | |
| 125 | kill_gitk |
| 126 | dance |
| 127 | cd "$basedir" |
| 128 | |
| 129 | if [ "$orig_branch" != master ]; then |
| 130 | kill_gitk |
| 131 | git -C "$dir" checkout master || continue |
| 132 | dance |
| 133 | cd "$basedir" |
| 134 | pwd |
| 135 | git -C "$dir" checkout "$orig_branch" |
| 136 | fi |
| 137 | |
| 138 | # if [ "$dir" = "openbsc" ]; then |
| 139 | # kill_gitk |
| 140 | # Git checkout "sysmocom/iu" |
| 141 | # dance |
| 142 | # fi |
| 143 | |
| 144 | sleep .1 |
| 145 | |
| 146 | done |
| 147 | |
| 148 | kill_gitk |
| 149 | |
| 150 | echo |
| 151 | echo |
| 152 | ./st |
| 153 | |
| 154 | # vim: shiftwidth=2 expandtab |