blob: f897bc12182d90f3aa44540b6ee85f780e63ba1b [file] [log] [blame]
Neels Hofmeyr45f3cec2017-08-13 03:25:38 +02001#!/usr/bin/env bash
Neels Hofmeyrd313c722018-02-28 18:27:47 +01002fastforwards=""
3
Neels Hofmeyr45f3cec2017-08-13 03:25:38 +02004Git() {
5 echo "git $@"
6 git $@
7 if [ "$?" != "0" ]; then
8 echo "GIT RETURNED ERROR!"
9 exit 1
10 fi
11}
12
13Git_may_fail() {
14 git $@
15}
16
17Git_branch() {
18 echo "$(Git -C "$dir" status)" | grep 'On branch' | sed 's/On branch //'
19}
20
21
22gitk_start() {
23 if [ -n "$DISPLAY" ]; then
24 gitk --all &
25 gitk_started="1"
26 fi
27}
28
Neels Hofmeyrd313c722018-02-28 18:27:47 +010029status() {
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 Hofmeyr45f3cec2017-08-13 03:25:38 +020039dance() {
40 echo
41 echo
42 br="$(Git_branch)"
43
44 echo "$dir"
45 cd "$dir"
46
Neels Hofmeyrd313c722018-02-28 18:27:47 +010047 status
Neels Hofmeyr45f3cec2017-08-13 03:25:38 +020048
49 if [ -z "$mods" -a -n "$(echo "$stline" | grep up-to-date)" ]; then
50 return 0
51 fi
52
53 gitk_start
54
Neels Hofmeyr45f3cec2017-08-13 03:25:38 +020055 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 Hofmeyrd313c722018-02-28 18:27:47 +010066 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 Hofmeyr45f3cec2017-08-13 03:25:38 +020073 fi
74
Neels Hofmeyrd313c722018-02-28 18:27:47 +010075 status
76
77 if [ -n "$mods" ]; then
Neels Hofmeyr45f3cec2017-08-13 03:25:38 +020078 return 0
79 fi
80 fi
Neels Hofmeyrd313c722018-02-28 18:27:47 +010081
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 Hofmeyr45f3cec2017-08-13 03:25:38 +0200124}
125
126kill_gitk() {
127 if [ "$gitk_started" = "1" ]; then
128 kill %1
129 gitk_started="0"
130 fi
131}
132
133
134basedir="$(pwd)"
135gitk_started="0"
136for 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
163done
164
165kill_gitk
166
167echo
168echo
169./st
170
Neels Hofmeyrd313c722018-02-28 18:27:47 +0100171if [ -n "$fastforwards" ]; then
172 echo
173 echo "FAST-FORWARDED: $fastforwards"
174fi
175
Neels Hofmeyr45f3cec2017-08-13 03:25:38 +0200176# vim: shiftwidth=2 expandtab