]> git.ozlabs.org Git - next-scripts/commitdiff
fetch_trees: shellcheck and general style clean up
authorStephen Rothwell <sfr@canb.auug.org.au>
Sat, 29 Sep 2018 02:52:55 +0000 (12:52 +1000)
committerStephen Rothwell <sfr@canb.auug.org.au>
Sat, 29 Sep 2018 02:52:55 +0000 (12:52 +1000)
fetch_trees

index 2747e92df23817b2e464a9691481c327b3ee3b09..e8c91f7a2b6ee621beee83f2d7194ac787af42be 100755 (executable)
@@ -1,26 +1,34 @@
 #!/bin/bash
 
-. "$(dirname $0)/common.sh" ""
+tools_dir=$(dirname "$0")
+# shellcheck source=/dev/null
+. "$tools_dir/common.sh" ''
 
-[ "$1" = "-n" ] && {
+if [ "$1" = '-n' ]; then
        shift
-       last=$(tail -n1 $SHA1_FILE | cut -f1 -d$'\t')
-       start_from=$(awk -F '\t' '/^[^#]/ && $3 == "'$last'" { do getline; while (/^#/ || $2 == "branch"); print $3; exit 0; }' $CTRL_FILE)
-}
+       last=$(tail -n1 "$SHA1_FILE" | cut -f1 -d$'\t')
+       start_from=$(awk -F '\t' '/^[^#]/ && $3 == "'"$last"'" {
+               do
+                       getline;
+               while (/^#/ || $2 == "branch");
+               print $3;
+               exit 0;
+       }' "$CTRL_FILE")
+fi
 
-[ "$1" = "-s" ] && {
+if [ "$1" = '-s' ]; then
        shift
        start_from="$1"
-       [ -z "$start_from" ] && {
-               echo "-s requires a start tree" 1>&2
+       if [ -z "$start_from" ]; then
+               printf '-s requires a start tree\n' 1>&2
                exit 1
-       }
+       fi
        shift
-}
+fi
 
 get_field()
 {
-       awk -F '\t' '/^[^#]/ && $3 == "'$1'" { print $'$2'; }' $CTRL_FILE
+       awk -F '\t' '/^[^#]/ && $3 == "'"$1"'" { print $'"$2"'; }' "$CTRL_FILE"
 }
 
 fetch_git()
@@ -31,18 +39,21 @@ fetch_git()
 fetch_mmotm()
 {
        (
-               cd ../mmotm || {
-                       echo "Cannot chdir to ../mmotm" 1>&2
+               if ! cd ../mmotm; then
+                       printf 'Cannot chdir to ../mmotm\n' 1>&2
                        return
-               }
-               rsync -az --partial --exclude .git\* --exclude broken-out.tar.gz --delete ozlabs.org::akpm/mmotm/. . || {
+               fi
+               if ! rsync -az --partial --exclude '.git*' \
+                               --exclude broken-out.tar.gz --delete \
+                               ozlabs.org::akpm/mmotm/. .; then
                        git reset --hard
-                       echo "Fetcing mmotm failed" 1>&2
+                       printf 'Fetcing mmotm failed\n' 1>&2
                        return
-               }
+               fi
                git add -A .
-               [ "$(git status --porcelain)" ] &&
+               if [ "$(git status --porcelain)" ]; then
                        git commit -m "$(head -n 1 .DATE)"
+               fi
        )
 }
 
@@ -52,26 +63,26 @@ fetch_quilt()
                url=$(get_field "$1" 4)
                url=${url%/}    # strip trailing / if necessary
 
-               cd ../quilt || {
-                       echo "Cannot chdir to ../quilt" 1>&2
+               if ! cd ../quilt; then
+                       printf 'Cannot chdir to ../quilt\n' 1>&2
                        return
-               }
-               [ -d "$1" ] || mkdir "$1" || {
-                       echo "Cannot create quilt directory for $1" 1>&2
+               fi
+               if ! [ -d "$1" ] && ! mkdir "$1"; then
+                       printf 'Cannot create quilt directory for %s\n' "$1" 1>&2
                        return
-               }
-               cd "$1" || {
-                       echo "Cannot chdir to quilt directory for $1" 1>&2
+               fi
+               if ! cd "$1"; then
+                       printf 'Cannot chdir to quilt directory for %s\n' "$1" 1>&2
                        return
-               }
-               wget -N -nv --no-cache "$url/series" || {
-                       echo "Wget of $1 series file failed" 1>&2
+               fi
+               if ! wget -N -nv --no-cache "$url/series"; then
+                       printf 'Wget of %s series file failed\n' "$1" 1>&2
                        cd ..
                        rm -rf "$1"
                        git checkout "$1"
                        return
-               }
-               find * -type f | grep -v '^series$' | sort >.series.old
+               fi
+               find . -type f | sed 's,^./,,;/^series$/d' | sort >.series.old
                if grep -q NEXT_PATCHES series; then
                        sed -n '/NEXT_PATCHES_START/,/NEXT_PATCHES_END/p' series
                else
@@ -79,48 +90,56 @@ fetch_quilt()
                fi |
                        sed -e 's/[ \t]*#.*$//' -e '/^[ \t]*$/d' |
                        sort >.series.next
-               [ -s .series.next ] && {
-                       wget -N -nv --no-cache -B "$url/" -i .series.next || {
-                               echo "Wget of series '$1' failed" 1>&2
+               if [ -s .series.next ]; then
+                       if ! wget -N -nv --no-cache -B "$url/" -i .series.next; then
+                               printf 'Wget of series %s failed\n' "$1" 1>&2
                                cd ..
                                rm -rf "$1"
                                git checkout "$1"
                                return
-                       }
-               }
+                       fi
+               fi
                comm -23 .series.old .series.next | xargs -r rm -f
                rm -f .series.old .series.next
-               setfattr -x user.xdg.origin.url * >/dev/null 2>&1
+               setfattr -x user.xdg.origin.url ./* >/dev/null 2>&1
        )
 }
 
-trees="$@"
-[ "$trees" ] ||
-       trees=$(awk -F '\t' '/^[^#]/ && $2 != "branch" { print $3 }' $CTRL_FILE)
+trees="$*"
+if ! [ "$trees" ]; then
+       trees=$(awk -F '\t' '/^[^#]/ && $2 != "branch" { print $3 }' "$CTRL_FILE")
+fi
 
 for name in $trees; do
-       [ -n "$start_from" ] && {
+       if [ -n "$start_from" ]; then
                if [ "$name" = "$start_from" ]; then
-                       start_from=""
+                       start_from=''
                else
                        continue
                fi
-       }
+       fi
 
        type=$(get_field "$name" 2)
        if ! [ "$type" ]; then
-               printf "%s: unknown tree\n" "$name" 1>&2
+               printf '%s: unknown tree\n' "$name" 1>&2
                continue
        fi
 
-       echo $name: $type
-       [ "$(type -t fetch_$type)" = "function" ] &&
-               fetch_$type "$name"
+       printf '%s: %s\n' "$name" "$type"
+       fun="fetch_$type"
+       tfun=$(type -t "$fun")
+       if [ "$tfun" = 'function' ]; then
+               "$fun" "$name"
+       fi
 done
 
-cd ../quilt
+if ! cd ../quilt; then
+       printf 'hmmm, what happened to the quilt directory?\n' 1>&2
+       exit 1
+fi
 git add -A .
-[ "$(git status --porcelain)" ] &&
+if [ "$(git status --porcelain)" ]; then
        git commit -v -a -m "$(date '+%F-%H:%M')" -e
+fi
 
 exit 0