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