| 1 |
# -*- shell-script -*- |
| 2 |
# These functions are useful functions when working with a bash shell |
| 3 |
# within a Net-SNMP git checkout. |
| 4 |
|
| 5 |
nsbuildroot=${nsbuildroot:=$HOME/ns-build-root} |
| 6 |
nsecho=${nsecho:=} |
| 7 |
nsbranches=${nsbranches:="V5-9-patches master"} |
| 8 |
|
| 9 |
# set nsverbose to ':' if you don't want verbose output of what's going on |
| 10 |
nsverbose=${nsverbose:="echo ::: Net-SNMP: "} |
| 11 |
|
| 12 |
_ns_maybemkdir () { |
| 13 |
dir=$1 |
| 14 |
if [ ! -d $dir ] ; then |
| 15 |
$nsverbose making directory $dir |
| 16 |
mkdir -p $dir |
| 17 |
fi |
| 18 |
} |
| 19 |
|
| 20 |
_ns_switchtobuilddir () { |
| 21 |
dir=$1 |
| 22 |
_ns_maybemkdir $dir |
| 23 |
$nsverbose cd $dir |
| 24 |
cd $dir |
| 25 |
} |
| 26 |
|
| 27 |
_ns_getbuilddir() { |
| 28 |
nssuffix=${1:-$nssuffix} |
| 29 |
nsbranch=`git branch | egrep '^\*' | sed 's/^..//'` |
| 30 |
NSBUILDDIR="$nsbuildroot/$nsbranch" |
| 31 |
NSSRCDIR="$PWD" |
| 32 |
|
| 33 |
# add on the suffix if created |
| 34 |
if [ "$nssuffix" != "" ]; then |
| 35 |
NSBUILDDIR="$nsbuildroot/$nsbranch-$nssuffix" |
| 36 |
fi |
| 37 |
} |
| 38 |
|
| 39 |
_ns_setbuilddir() { |
| 40 |
_ns_getbuilddir "$1" |
| 41 |
_ns_switchtobuilddir $NSBUILDDIR |
| 42 |
} |
| 43 |
|
| 44 |
_ns_cleanup() { |
| 45 |
$nsverbose cd $NSSRCDIR |
| 46 |
cd $NSSRCDIR |
| 47 |
} |
| 48 |
|
| 49 |
_ns_checkclean() { |
| 50 |
if [ `git status --porcelain --untracked-files=no | wc -l` -gt 0 ] ; then |
| 51 |
echo "Your directory has outstanding changes!" |
| 52 |
echo " Please either commit the changes, discard them or stash them" |
| 53 |
NSCLEAN=0 |
| 54 |
else |
| 55 |
NSCLEAN=1 |
| 56 |
fi |
| 57 |
} |
| 58 |
|
| 59 |
# |
| 60 |
# nsmake [-s suffix] |
| 61 |
# runs 'make' in $nsbuildroot/branch[-suffix] |
| 62 |
nsmake () { |
| 63 |
if [ "$1" = "-s" ]; then |
| 64 |
shift |
| 65 |
_ns_setbuilddir "$1" |
| 66 |
shift |
| 67 |
elif [ "$1" != "" ]; then |
| 68 |
echo "Unknown argument: $1" |
| 69 |
exit 1 |
| 70 |
else |
| 71 |
_ns_setbuilddir |
| 72 |
fi |
| 73 |
|
| 74 |
$nsverbose make "$@" |
| 75 |
$nsecho make "$@" |
| 76 |
|
| 77 |
_ns_cleanup |
| 78 |
} |
| 79 |
|
| 80 |
nsmakeall () { |
| 81 |
_ns_checkclean |
| 82 |
if [ $NSCLEAN != 1 ]; then |
| 83 |
return |
| 84 |
fi |
| 85 |
|
| 86 |
for branch in $nsbranches ; do |
| 87 |
git checkout $branch |
| 88 |
if [ $? != 0 ] ; then |
| 89 |
echo "" |
| 90 |
echo "---------------------------------------------" |
| 91 |
echo "Checkout of $branch failed!!!" |
| 92 |
echo |
| 93 |
return |
| 94 |
fi |
| 95 |
nsmake "$@" |
| 96 |
done |
| 97 |
} |
| 98 |
|
| 99 |
# |
| 100 |
# nsconfigure [-s suffix] [configure arguments] |
| 101 |
# runs 'configure' in $nsbuildroot/branch[-suffix] with passed arguments |
| 102 |
nsconfigure () { |
| 103 |
if [ "$1" = "-s" ]; then |
| 104 |
shift |
| 105 |
_ns_setbuilddir "$1" |
| 106 |
shift |
| 107 |
elif [ "$1" != "" ]; then |
| 108 |
echo "Unknown argument: $1" |
| 109 |
exit 1 |
| 110 |
else |
| 111 |
_ns_setbuilddir |
| 112 |
fi |
| 113 |
|
| 114 |
$nsverbose running $NSSRCDIR/configure "$@" |
| 115 |
$nsecho $NSSRCDIR/configure "$@" |
| 116 |
|
| 117 |
_ns_cleanup |
| 118 |
} |
| 119 |
|
| 120 |
nspatchtry() { |
| 121 |
_ns_checkclean |
| 122 |
if [ $NSCLEAN != 1 ]; then |
| 123 |
return |
| 124 |
fi |
| 125 |
|
| 126 |
# remember these branches |
| 127 |
nsgoodbranches="" |
| 128 |
nsbadbranches="" |
| 129 |
|
| 130 |
# remember the patch args for later use |
| 131 |
nspatchargs="$@" |
| 132 |
|
| 133 |
# attempt to add any missing patch arguments |
| 134 |
|
| 135 |
echo "x $nspatchargs x" | grep -- -p > /dev/null |
| 136 |
if [ $? != "0" ] ; then |
| 137 |
# they didn't specify a -p option. Try to guess at one... |
| 138 |
nspatchfile=`echo $nspatchargs | sed 's/.* \([^ ]+\)$/\1/'` |
| 139 |
# attempt to count the slashes before any agent/snmplib/apps/etc |
| 140 |
patchcount=$((`grep diff $nspatchfile | head -1 | awk '{print $NF}' | sed 's/\(agent\|snmplib\|apps\|local\|perl\|python\).*//;s#[^/]##g' | wc -c` - 1)) |
| 141 |
# if we failed to guess, bail out. |
| 142 |
if [ "X$patchcount" = "X-1" ]; then |
| 143 |
echo "Please specify -p argument; I failed to guess the right one." |
| 144 |
return |
| 145 |
fi |
| 146 |
nspatchargs="-p $patchcount $nspatchargs" |
| 147 |
fi |
| 148 |
|
| 149 |
echo "x $nspatchargs x" | grep -- "-i " > /dev/null |
| 150 |
if [ $? != "0" ] ; then |
| 151 |
# they didn't specify a -i option. Try to add one... |
| 152 |
nspatchargs=`echo $nspatchargs | sed 's/\(.*\) \(..*\)$/\1 -i \2/'` |
| 153 |
fi |
| 154 |
|
| 155 |
echo "Using patch args: $nspatchargs" |
| 156 |
|
| 157 |
for branch in $nsbranches ; do |
| 158 |
$nsverbose checking out and applying patch in $branch |
| 159 |
$nsecho git checkout $branch |
| 160 |
if [ $? != 0 ] ; then |
| 161 |
echo "" |
| 162 |
echo "---------------------------------------------" |
| 163 |
echo "Checkout of $branch failed!!!" |
| 164 |
echo |
| 165 |
return |
| 166 |
fi |
| 167 |
|
| 168 |
$nsverbose Appling patch |
| 169 |
$nsecho patch -N --batch $nspatchargs |
| 170 |
if [ $? = 0 ] ; then |
| 171 |
$nsverbose Patch succeeded on $branch |
| 172 |
nsgoodbranches="$nsgoodbranches $branch" |
| 173 |
else |
| 174 |
$nsverbose Patch failed on $branch |
| 175 |
nsbadbranches="$nsbadbranches $branch" |
| 176 |
fi |
| 177 |
|
| 178 |
$nsverbose cleaning source tree |
| 179 |
git checkout . |
| 180 |
done |
| 181 |
|
| 182 |
echo "" |
| 183 |
echo "Patch application results:" |
| 184 |
echo " Success: $nsgoodbranches" |
| 185 |
echo " Fail: $nsbadbranches" |
| 186 |
} |
| 187 |
|
| 188 |
_nspatchapplybase() { |
| 189 |
_ns_checkclean |
| 190 |
if [ $NSCLEAN != 1 ]; then |
| 191 |
return |
| 192 |
fi |
| 193 |
|
| 194 |
patchbranch=`echo $nsgoodbranches | sed 's/ .*//'` |
| 195 |
|
| 196 |
$nsverbose Checking out $patchbranch |
| 197 |
$nsecho git checkout $patchbranch |
| 198 |
if [ $? != 0 ] ; then |
| 199 |
echo "" |
| 200 |
echo "---------------------------------------------" |
| 201 |
echo "Checkout of $branch failed!!!" |
| 202 |
echo |
| 203 |
return |
| 204 |
fi |
| 205 |
|
| 206 |
$nsverbose applying the patch "$nspatchargs" |
| 207 |
$nsecho patch $nspatchargs |
| 208 |
$nsecho git diff | cat |
| 209 |
} |
| 210 |
|
| 211 |
nspatchapply() { |
| 212 |
_nspatchapplybase |
| 213 |
|
| 214 |
echo "" |
| 215 |
echo -n "commit the results to $patchbranch? [y/n]" |
| 216 |
read ans |
| 217 |
|
| 218 |
if [ $ans = 'y' -o $ans = 'Y' ] ; then |
| 219 |
$nsecho git commit -a "$@" |
| 220 |
fi |
| 221 |
} |
| 222 |
|
| 223 |
nssfpatchapply() { |
| 224 |
_nspatchapplybase |
| 225 |
nssfpatchcommit "$@" |
| 226 |
} |
| 227 |
|
| 228 |
nssfpatchcommit() { |
| 229 |
patch="$1" |
| 230 |
area="$2" |
| 231 |
|
| 232 |
if [ "$patch" = "" ] ; then |
| 233 |
echo "Enter patch number: " |
| 234 |
echo -n "> " |
| 235 |
read patch |
| 236 |
fi |
| 237 |
if [ "$patch" = "" ] ; then |
| 238 |
echo "Error: A patch number is required" |
| 239 |
return |
| 240 |
fi |
| 241 |
|
| 242 |
if [ "$area" = "" ] ; then |
| 243 |
echo "Which area does this patch affect? (agent, libnetsnmp, apps, ...)?" |
| 244 |
echo -n "> " |
| 245 |
read area |
| 246 |
fi |
| 247 |
if [ "$area" = "" ] ; then |
| 248 |
area="unknown" |
| 249 |
fi |
| 250 |
|
| 251 |
# |
| 252 |
# get the patch's html |
| 253 |
# |
| 254 |
tmpfile="/tmp/nspatch.$patch" |
| 255 |
if [ ! -f "$tmpfile" ]; then |
| 256 |
wget -O $tmpfile "https://sourceforge.net/tracker/?func=detail&aid="${patch}"&group_id=12694&atid=312694" |
| 257 |
fi |
| 258 |
|
| 259 |
echo "---------------------------------" |
| 260 |
|
| 261 |
username=`grep /users/ $tmpfile | head -1 | sed 's/.*.users.//;s/.".*//;'` |
| 262 |
echo "username: $username" |
| 263 |
|
| 264 |
fullname=`grep /users/ $tmpfile | head -1 | sed 's/.*title=.//;s/".*//;'` |
| 265 |
echo "fullname: $fullname" |
| 266 |
|
| 267 |
title=`grep "Detail: $patch" $tmpfile | head -1 | sed "s/.*$patch - //;s/<.*//" | sed 's/"//g;' | sed "s/'//g;"` |
| 268 |
echo commit msg: $title |
| 269 |
|
| 270 |
commitmsg="CHANGES: $area: PATCH $patch: from $username: $title" |
| 271 |
|
| 272 |
patchbranch=`echo $nsgoodbranches | sed 's/ .*//'` |
| 273 |
echo "Branch: $patchbranch" |
| 274 |
echo "Command: " |
| 275 |
echo " git commit -a -s \\" |
| 276 |
echo " --author='$fullname <[email protected]>'\\" |
| 277 |
echo " -m '$commitmsg'" |
| 278 |
echo "" |
| 279 |
echo -n "commit the results to $patchbranch? [y/n]" |
| 280 |
read ans |
| 281 |
|
| 282 |
if [ $ans = 'y' -o $ans = 'Y' ] ; then |
| 283 |
$nsecho git commit -a -s --author="$fullname <[email protected]>" -m "$commitmsg" |
| 284 |
else |
| 285 |
echo "if you don't wish to keep the changes, use 'git reset --hard'" |
| 286 |
fi |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
nsrollup() { |
| 291 |
fetch=1; |
| 292 |
|
| 293 |
if [ "$1" = "--no-fetch" ] ; then |
| 294 |
fetch=0 |
| 295 |
shift |
| 296 |
fi |
| 297 |
|
| 298 |
if [ "$1" = "--merge" ] ; then |
| 299 |
mergeop="merge" |
| 300 |
elif [ "$1" = "--rebase" ] ; then |
| 301 |
mergeop="rebase" |
| 302 |
elif [ "$1" != "" ]; then |
| 303 |
echo "Unknown argument: $1" |
| 304 |
exit 1 |
| 305 |
else |
| 306 |
mergeop="merge" |
| 307 |
fi |
| 308 |
|
| 309 |
_ns_checkclean |
| 310 |
if [ $NSCLEAN != 1 ]; then |
| 311 |
return |
| 312 |
fi |
| 313 |
|
| 314 |
if [ $fetch == 1 ]; then |
| 315 |
$nsverbose Pulling all upstream branches |
| 316 |
$nsecho git fetch --all |
| 317 |
fi |
| 318 |
|
| 319 |
nslastbranch="" |
| 320 |
|
| 321 |
nsbranchesdone="" |
| 322 |
nsbranchesnotdone="" |
| 323 |
|
| 324 |
for branch in $nsbranches ; do |
| 325 |
if [ "$nslastbranch" != "" ] ; then |
| 326 |
$nsverbose checking out $branch |
| 327 |
$nsecho git checkout $branch |
| 328 |
if [ $? != 0 ] ; then |
| 329 |
echo "" |
| 330 |
echo "---------------------------------------------" |
| 331 |
echo "Checkout of $branch failed!!!" |
| 332 |
echo |
| 333 |
return |
| 334 |
fi |
| 335 |
|
| 336 |
if [ -f dist/release ] ; then |
| 337 |
if [ "`egrep ^$branch dist/release`" = "$branch rc" ] ; then |
| 338 |
$nsverbose Skipping: branch is in rc phase of release |
| 339 |
nsbranchesnotdone="$nsbranchesnotdone $branch" |
| 340 |
|
| 341 |
# comment this out if you want nsrollup to skip the |
| 342 |
# branch and roll the changes in the lower branch up |
| 343 |
# to the super-parent when the parent is in release status |
| 344 |
nslastbranch=$branch |
| 345 |
|
| 346 |
continue |
| 347 |
fi |
| 348 |
fi |
| 349 |
|
| 350 |
$nsverbose rebasing our local changes on $branch |
| 351 |
MERGE_AUTOEDIT=no git $mergeop origin/$branch |
| 352 |
|
| 353 |
$nsverbose merging $nslastbranch into $branch |
| 354 |
MERGE_AUTOEDIT=no $nsecho git merge --log $nslastbranch |
| 355 |
if [ $? != 0 ] ; then |
| 356 |
echo "" |
| 357 |
echo "---------------------------------------------" |
| 358 |
echo "Merge of $nslastbranch into $branch failed!!!" |
| 359 |
echo "" |
| 360 |
echo "Hints for fixing this:" |
| 361 |
echo " You're on: $branch" |
| 362 |
echo " Steps:" |
| 363 |
echo " 1) fix all files marked as problematic" |
| 364 |
echo " 2) run 'git add' on each file" |
| 365 |
if [ $mergeop = "rebase" ] ; then |
| 366 |
echo " 3) run 'git rebase --continue' to commit the changes" |
| 367 |
else |
| 368 |
echo " 3) run 'git commit' to commit the changes" |
| 369 |
fi |
| 370 |
echo " 4) once done, re-run nsrollup" |
| 371 |
echo " Aborting:" |
| 372 |
echo " If instead you want to give up, run" |
| 373 |
echo " 1) git $mergeop --abort" |
| 374 |
echo "" |
| 375 |
echo "Updated the following branches:" |
| 376 |
echo " $nsbranchesdone" |
| 377 |
if [ "$nsbranchesnotdone" != "" ] ; then |
| 378 |
echo "Did NOT update the following branches:" |
| 379 |
echo " $nsbranchesnotdone" |
| 380 |
fi |
| 381 |
echo "Failed to update this branch:" |
| 382 |
echo " $branch" |
| 383 |
return |
| 384 |
fi |
| 385 |
|
| 386 |
_ns_checkclean |
| 387 |
if [ $NSCLEAN != 1 ] ; then |
| 388 |
# we really shouldn't git here. merge will either autocommit |
| 389 |
# or it will fail |
| 390 |
$nsverbose committing merge results |
| 391 |
$nsecho git commit -m "nsrollup: merging $nslastbranch into $branch" -a |
| 392 |
fi |
| 393 |
|
| 394 |
nsbranchesdone="$nsbranchesdone $branch" |
| 395 |
fi |
| 396 |
|
| 397 |
nslastbranch=$branch |
| 398 |
done |
| 399 |
|
| 400 |
echo "" |
| 401 |
echo "-------------------------------" |
| 402 |
echo "Updated the following branches:" |
| 403 |
echo " $nsbranchesdone" |
| 404 |
if [ "$nsbranchesnotdone" != "" ] ; then |
| 405 |
echo "Did NOT update the following branches:" |
| 406 |
echo " $nsbranchesnotdone" |
| 407 |
fi |
| 408 |
|
| 409 |
echo "" |
| 410 |
echo "Now would be a good time to run 'nssync'" |
| 411 |
} |
| 412 |
|
| 413 |
nspull() { |
| 414 |
nscurrentbranch=`git branch | egrep '^\*' | sed 's/^..//'` |
| 415 |
_ns_checkclean |
| 416 |
if [ $NSCLEAN != 1 ]; then |
| 417 |
return |
| 418 |
fi |
| 419 |
|
| 420 |
if [ "$1" = "--merge" ] ; then |
| 421 |
mergeop="merge" |
| 422 |
elif [ "$1" = "--rebase" ] ; then |
| 423 |
mergeop="rebase" |
| 424 |
elif [ "$1" != "" ]; then |
| 425 |
echo "Unknown argument: $1" |
| 426 |
exit 1 |
| 427 |
else |
| 428 |
mergeop="merge" |
| 429 |
fi |
| 430 |
|
| 431 |
git fetch --all |
| 432 |
for branch in $nsbranches ; do |
| 433 |
git checkout $branch |
| 434 |
if [ $? != 0 ] ; then |
| 435 |
echo "" |
| 436 |
echo "---------------------------------------------" |
| 437 |
echo "Checkout of $branch failed!!!" |
| 438 |
echo |
| 439 |
return |
| 440 |
fi |
| 441 |
MERGE_AUTOEDIT=no git $mergeop origin/$branch |
| 442 |
if [ $? != 0 ] ; then |
| 443 |
echo "git $mergeop failed; please fix first" |
| 444 |
return |
| 445 |
fi |
| 446 |
done |
| 447 |
git checkout $nscurrentbranch |
| 448 |
if [ $? != 0 ] ; then |
| 449 |
echo "" |
| 450 |
echo "---------------------------------------------" |
| 451 |
echo "Checkout of $branch failed!!!" |
| 452 |
echo |
| 453 |
return |
| 454 |
fi |
| 455 |
} |
| 456 |
|
| 457 |
nspush() { |
| 458 |
_ns_checkclean |
| 459 |
if [ $NSCLEAN != 1 ]; then |
| 460 |
return |
| 461 |
fi |
| 462 |
|
| 463 |
git push origin $nsbranches |
| 464 |
if [ $? != 0 ] ; then |
| 465 |
echo "git push failed; help?" |
| 466 |
return |
| 467 |
fi |
| 468 |
} |
| 469 |
|
| 470 |
# |
| 471 |
# pull/pushes all the known active branches |
| 472 |
# |
| 473 |
nssync() { |
| 474 |
nspull "$@" |
| 475 |
nspush "$@" |
| 476 |
} |
| 477 |
|
| 478 |
# |
| 479 |
# runs the right version of autoconf for a given branch |
| 480 |
# |
| 481 |
# configure autoconf versions using --prefix=/usr/local/autoconf-VERSION |
| 482 |
# |
| 483 |
nsautoconf() { |
| 484 |
if [ "$1" == "autoheader" ] ; then |
| 485 |
tool="autoheader" |
| 486 |
else |
| 487 |
tool="autoconf" |
| 488 |
fi |
| 489 |
|
| 490 |
COR=`cat dist/autoconf-version` |
| 491 |
PATH=/usr/local/autoconf-${COR}/bin:$PATH autoconf --version > /tmp/autoconf.version |
| 492 |
VER=`head -1 /tmp/autoconf.version | awk '{print $NF}'` |
| 493 |
if [ "$VER" != "$COR" ] ; then |
| 494 |
echo "failed to find the correct version of autoconf" |
| 495 |
echo "please install autoconf version $COR in /usr/local/autoconf-$COR" |
| 496 |
echo " (configure autoconf-$COR using --prefix=/usr/local/autoconf-$COR" |
| 497 |
return |
| 498 |
fi |
| 499 |
|
| 500 |
echo "RUNNING AUTOCONF $COR" |
| 501 |
PATH=/usr/local/autoconf-${COR}/bin:$PATH $tool |
| 502 |
} |