{"thread":{"id":"3901","subject":"[PATCH] Suppress use of unsafe idiomatic use of && in cg-Xlib","startedAt":"2006-04-17T14:49:45Z","lastAt":"2006-04-17T14:49:45Z","messageCount":1,"participants":["Yann Dirson"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"18771","messageId":"20060417144944.806.87989.stgit@gandelf.nowhere.earth","threadId":"3901","inReplyTo":null,"subject":"[PATCH] Suppress use of unsafe idiomatic use of && in cg-Xlib","fromName":"Yann Dirson","fromEmail":"ydirson@altern.org","sentAt":"2006-04-17T14:49:45Z","receivedAt":"2006-04-17T14:49:45Z","isPatch":true,"sender":{"key":"ydirson@altern.org","avatar":"https://avatars.githubusercontent.com/u/1190950?v=4"},"body":"\nThis is a necessary step to make any cogito script \"set -e\"-safe.\n\nAlso fixes similar issues in cg-admin-rewritehist and turn \"set -e\" on\nthere.\n\nSigned-off-by: Yann Dirson <ydirson@altern.org>\n---\n\n cg-Xlib              |   82 ++++++++++++++++++++++++++------------------------\n cg-admin-rewritehist |    8 +++--\n 2 files changed, 48 insertions(+), 42 deletions(-)\n\ndiff --git a/cg-Xlib b/cg-Xlib\nindex e594986..83d4400 100755\n--- a/cg-Xlib\n+++ b/cg-Xlib\n@@ -26,7 +26,7 @@ warn()\n \tfi\n \n \techo \"Warning: $@\" >&2\n-\t[ \"$beep\" ] && echo -ne \"\\a\" >&2\n+\t[ -z \"$beep\" ] || echo -ne \"\\a\" >&2\n }\n \n die()\n@@ -177,7 +177,7 @@ showdate()\n {\n \tlocal secs=$1 tzhours=${2:0:3} tzmins=${2:0:1}${2:3} format=\"$3\"\n \t# bash doesn't like leading zeros\n-\t[ \"${tzhours:1:1}\" = 0 ] && tzhours=${2:0:1}${2:2:1}\n+\t[ \"${tzhours:1:1}\" != 0 ] || tzhours=${2:0:1}${2:2:1}\n \tsecs=$(($secs + $tzhours * 3600 + $tzmins * 60))\n \t[ \"$format\" ] || format=\"+%a, %d %b %Y %H:%M:%S $2\"\n \tif [ \"$has_gnudate\" ]; then\n@@ -213,7 +213,7 @@ colorify_diffcolors=\"$colorify_diffcolor\n colorify_setup()\n {\n \tlocal C=\"$1\"\n-\t[ -n \"$CG_COLORS\" ] && C=\"$C:$CG_COLORS\"\n+\t[ -z \"$CG_COLORS\" ] || C=\"$C:$CG_COLORS\"\n \n \tC=${C//=/=\\'$'\\e'[}\n \tC=col${C//:/m\\'; col}m\\'\n@@ -253,7 +253,7 @@ column_width()\n \tdone | sort -nr | head -n 1 |\n \t(\n \t\tread maxlen;\n-\t\t[ ${maxlen:-0} -gt $maxwidth ] && maxlen=$maxwidth;\n+\t\t[ ${maxlen:-0} -le $maxwidth ] || maxlen=$maxwidth;\n \t\techo ${maxlen:-0}\n \t)\n }\n@@ -275,7 +275,7 @@ columns_print()\n \t\telse\n \t\t\tfmt=\"$fmt%-${width}s\"\n \t\tfi\n-\t\t[ \"$tab\" ] && fmt=\"$fmt\\t\";\n+\t\t[ -z \"$tab\" ] || fmt=\"$fmt\\t\";\n \tdone\n \tprintf \"$fmt\\n\" \"${cols[@]}\"\n }\n@@ -313,7 +313,7 @@ pick_id()\n \t'\n \tLANG=C LC_ALL=C sed -ne \"$pick_id_script\"\n \t# Ensure non-empty id name.\n-\techo \"[ -z \\\"\\$GIT_${uid}_NAME\\\" ] && export GIT_${uid}_NAME=\\\"\\${GIT_${uid}_EMAIL%%@*}\\\"\"\n+\techo \"[ -n \\\"\\$GIT_${uid}_NAME\\\" ] || export GIT_${uid}_NAME=\\\"\\${GIT_${uid}_EMAIL%%@*}\\\"\"\n }\n \n pick_author()\n@@ -347,16 +347,16 @@ while IFS= read -r inp; do\n \tdone\n \tpath[${#path[@]}]=\"$inp\"\n \tfor (( i=0; $i < ${#path[@]}; i++ )); do\n-\t\t[ \"${path[$i]}\" = \".\" ] && continue\n+\t\t[ \"${path[$i]}\" != \".\" ] || continue\n \t\tif [ \"${path[$i]}\" = \"..\" ]; then\n-\t\t\t[ \"${#path2[@]}\" -gt 0 ] && unset path2[$((${#path2[@]} - 1))]\n+\t\t\t[ \"${#path2[@]}\" -le 0 ] || unset path2[$((${#path2[@]} - 1))]\n \t\t\tcontinue\n \t\tfi\n \t\tpath2[${#path2[@]}]=\"${path[$i]}\"\n \tdone\n \tfor (( i=0; $i < ${#path2[@]}; i++ )); do\n \t\techo -n \"${path2[$i]}\"\n-\t\t[ $i -lt $((${#path2[@]} - 1)) ] && echo -n /\n+\t\t[ $i -ge $((${#path2[@]} - 1)) ] || echo -n /\n \tdone\n \techo\n done\n@@ -392,7 +392,7 @@ #              only the dirname/ will be\n # EXTRAEXCLUDE: extra exclude pattern\n list_untracked_files()\n {\n-\t[ \"$_git_no_wc\" ] && die \"INTERNAL ERROR: list_untracked_files() outside a working copy\"\n+\t[ -z \"$_git_no_wc\" ] || die \"INTERNAL ERROR: list_untracked_files() outside a working copy\"\n \texcludeflag=\"$1\"; shift\n \tsquashflag=\"$1\"; shift\n \tEXCLUDE=()\n@@ -435,7 +435,7 @@ list_untracked_files()\n \t\tfi\n \tfi\n \tlocal listdirs=\n-\t[ \"$squashflag\" = \"squashdirs\" ] && listdirs=--directory\n+\t[ \"$squashflag\" != \"squashdirs\" ] || listdirs=--directory\n \tgit-ls-files -z --others $listdirs \"${EXCLUDE[@]}\"\n }\n \n@@ -504,8 +504,8 @@ editor()\n \tactionkey=\"$1\"; shift\n \n \t${EDITOR:-vi} \"$LOGMSG2\"\n-\t[ \"$force\" ] && return 0\n-\t[ \"$LOGMSG2\" -nt \"$LOGMSG\" ] && return 0\n+\t[ -z \"$force\" ] || return 0\n+\t[ \"$LOGMSG\" -nt \"$LOGMSG2\" ] || return 0\n \n \techo \"Log message unchanged or not specified\" >&2\n \twhile true; do\n@@ -530,7 +530,7 @@ # the working copy (if ROLLBACK_BOOL) an\n # Returns false in case of local modifications (but only if ROLLBACK_ROLL).\n tree_timewarp()\n {\n-\t[ \"$_git_no_wc\" ] && die \"INTERNAL ERROR: tree_timewarp() outside a working copy\"\n+\t[ -z \"$_git_no_wc\" ] || die \"INTERNAL ERROR: tree_timewarp() outside a working copy\"\n \tlocal no_head_update=\n \tif [ \"$1\" = \"--no-head-update\" ]; then\n \t\tno_head_update=1\n@@ -542,14 +542,14 @@ tree_timewarp()\n \tlocal branch=\"$1\"; shift\n \tlocal localmods=0\n \n-\t[ -s \"$_git/merging\" ] && die \"merge in progress - cancel it by cg-reset first\"\n+\t[ ! -s \"$_git/merging\" ] || die \"merge in progress - cancel it by cg-reset first\"\n \n \tif [ -n \"$rollback\" ]; then\n \t\tlocal patchfile=\"$(mktemp -t gituncommit.XXXXXX)\"\n \t\tcg-diff -r \"$base\" >\"$patchfile\"\n-\t\t[ -s \"$patchfile\" ] &&\n+\t\t[ ! -s \"$patchfile\" ] ||\n \t\t\twarn \"uncommitted local changes, trying to bring them $dirstr\"\n-\t\t[ -s \"$patchfile\" ] && localmods=1\n+\t\t[ ! -s \"$patchfile\" ] || localmods=1\n \n \t\tgit-read-tree -m \"$branch\" || die \"$branch: bad commit\"\n \t\t[ \"$no_head_update\" ] || git-update-ref HEAD \"$branch\" || :\n@@ -581,9 +581,9 @@ conservative_merge_base()\n \t_cg_base_conservative=\n \tfor (( safecounter=0; $safecounter < 1000; safecounter++ )) ; do\n \t\tbaselist=($(git-merge-base --all \"${baselist[@]}\")) || return 1\n-\t\t[ \"${#baselist[@]}\" -le \"1\" ] && break\n+\t\t[ \"${#baselist[@]}\" -gt \"1\" ] || break\n \tdone\n-\t[ $safecounter -gt 0 ] && _cg_base_conservative=$safecounter\n+\t[ $safecounter -le 0 ] || _cg_base_conservative=$safecounter\n \t_cg_baselist=(\"${baselist[@]}\")\n }\n \n@@ -595,7 +595,7 @@ # Never use it. If you do, accompany it \n # it safe to use it.\n update_index()\n {\n-\t[ \"$_git_no_wc\" ] && die \"INTERNAL ERROR: update_index() outside a working copy\"\n+\t[ -z \"$_git_no_wc\" ] || die \"INTERNAL ERROR: update_index() outside a working copy\"\n \tgit-update-index --refresh | sed 's/needs update$/locally modified/'\n }\n \n@@ -612,14 +612,14 @@ is_same_repo()\n \t# second side...\n \tif [ ! -w \"$dir1\" -o ! -w \"$dir2\" ]; then\n \t\t# ...except in readonly setups.\n-\t\t[ \"$(readlink -f \"$dir1\")\" = \"$(readlink -f \"$dir2\")\" ] && diff=0\n+\t\t[ \"$(readlink -f \"$dir1\")\" != \"$(readlink -f \"$dir2\")\" ] || diff=0\n \telse\n \t\tn=$$\n \t\twhile [ -e \"$dir1/.,,lnstest-$n\" -o -e \"$dir2/.,,lnstest-$n\" ]; do\n \t\t\tn=$((n+1))\n \t\tdone\n \t\ttouch \"$dir1/.,,lnstest-$n\"\n-\t\t[ -e \"$dir2/.,,lnstest-$n\" ] && diff=0\n+\t\t[ ! -e \"$dir2/.,,lnstest-$n\" ] || diff=0\n \t\trm \"$dir1/.,,lnstest-$n\"\n \tfi\n \treturn $diff\n@@ -655,7 +655,7 @@ deprecated_alias()\n \tcmd=\"${0##*/}\"\n \tpropername=\"$1\"; shift\n \tfor a in \"$@\"; do\n-\t\t[ \"$cmd\" = \"$a\" ] && \\\n+\t\t[ \"$cmd\" != \"$a\" ] || \\\n \t\t\twarn \"'$a' is a deprecated alias, please use '$propername' instead\"\n \tdone\n }\n@@ -685,7 +685,7 @@ print_help()\n \techo\n \techo \"Options:\"\n \tmaxlen=\"$(sed -n 's/^# \\(-.*\\)::[^A-Za-z0-9].*/\\1/p' < \"$_cg_cmd\" | column_width)\"\n-\t[ $maxlen -lt 11 ] && maxlen=11 # --long-help\n+\t[ $maxlen -ge 11 ] || maxlen=11 # --long-help\n \t_cg_fmt=\"  %-20s %s\\n\"\n \tsed -n 's/# \\(-.*\\)::[^A-Za-z0-9]\\(.*\\)/\\1\\n\\2/p' < \"$_cg_cmd\" | while read line; do\n \t\tcase \"$line\" in\n@@ -703,7 +703,7 @@ print_help()\n }\n \n for option in \"$@\"; do\n-\t[ x\"$option\" = x-- ] && break\n+\t[ x\"$option\" != x-- ] || break\n \tif [ x\"$option\" = x\"-h\" ] || [ x\"$option\" = x\"--help\" ]; then\n \t\tprint_help short \"${_cg_cmd##cg-}\"\n \telif [ x\"$option\" = x\"--long-help\" ]; then\n@@ -753,8 +753,8 @@ optparse()\n \t\t--)\toptshift; return 1 ;;\n \t\t-*)\treturn 0 ;;\n \t\t*)\twhile (( ++ARGPOS < ${#ARGS[@]} )); do\n-\t\t\t\t[[ \"${ARGS[$ARGPOS]}\" == -- ]] && return 1\n-\t\t\t\t[[ \"${ARGS[$ARGPOS]}\" == -* ]] && return 0\n+\t\t\t\t[[ \"${ARGS[$ARGPOS]}\" != -- ]] || return 1\n+\t\t\t\t[[ \"${ARGS[$ARGPOS]}\" != -* ]] || return 0\n \t\t\tdone;\n \t\t\treturn 1 ;;\n \t\tesac\n@@ -762,22 +762,26 @@ optparse()\n \n \tCUROPT=\"${ARGS[$ARGPOS]}\"\n \tlocal match=\"${1%=}\" minmatch=\"${2:-1}\" opt=\"$CUROPT\" o=\"$CUROPT\" val\n-\t[[ \"$1\" == *= ]] && val=\"$match\"\n+\t[[ \"$1\" != *= ]] || val=\"$match\"\n \tcase \"$match\" in\n \t--*)\n-\t\t[ \"$val\" ] && o=\"${o%%=*}\"\n+\t\t[ -z \"$val\" ] || o=\"${o%%=*}\"\n \t\t[ ${#o} -ge $((2 + $minmatch)) -a \\\n \t\t\t\"${match:0:${#o}}\" = \"$o\" ] || return 1\n-\t\t[[ -n \"$val\" && \"$opt\" == *=?* ]] \\\n-\t\t\t&& ARGS[$ARGPOS]=\"${opt#*=}\" \\\n-\t\t\t|| optshift \"$val\" ;;\n+\t\tif [[ -n \"$val\" && \"$opt\" == *=?* ]]; then\n+\t\t\tARGS[$ARGPOS]=\"${opt#*=}\"\n+\t\telse\n+\t\t\toptshift \"$val\"\n+\t\tfi ;;\n \t-?)\n \t\t[[ \"$o\" == $match* ]] || return 1\n \t\t[[ \"$o\" != -?-* || -n \"$val\" ]] || optfail\n \t\tARGS[$ARGPOS]=${o#$match}\n-\t\t[ -n \"${ARGS[$ARGPOS]}\" ] \\\n-\t\t\t&& { [ -n \"$val\" ] || ARGS[$ARGPOS]=-\"${ARGS[$ARGPOS]}\"; } \\\n-\t\t\t|| optshift \"$val\" ;;\n+\t\tif [ -n \"${ARGS[$ARGPOS]}\" ]; then\n+\t\t\t[ -n \"$val\" ] || ARGS[$ARGPOS]=-\"${ARGS[$ARGPOS]}\";\n+\t\telse\n+\t\t\toptshift \"$val\"\n+\t\tfi ;;\n \t*)\n \t\tdie \"optparse cannot handle $1\" ;;\n \tesac\n@@ -822,7 +826,7 @@ check_tool()\n \t\t\tfi\n \t\tdone\n \t\tIFS=\"$save_IFS\"\n-\t\t[ \"$hasname\" ] && break\n+\t\t[ -z \"$hasname\" ] || break\n \tdone 2>/dev/null\n }\n \n@@ -858,7 +862,7 @@ if [ ! \"$_git_repo_unneeded\" ]; then\n \t\t_git=.\n \t\texport GIT_DIR=.\n \tfi\n-\t[ \"$GIT_DIR\" = . ] && _git_no_wc=1\n+\t[ \"$GIT_DIR\" != . ] || _git_no_wc=1\n \tif [ ! -d \"$_git\" ]; then\n \t\techo \"There is no GIT repository here ($_git not found)\" >&2\n \t\texit 1\n@@ -870,8 +874,8 @@ if [ ! \"$_git_repo_unneeded\" ]; then\n \t\texit 1\n \tfi\n \t_git_head=master\n-\t[ -s \"$_git/HEAD\" ] && { _git_head=\"$(git-symbolic-ref HEAD)\"; _git_head=\"${_git_head#refs/heads/}\"; }\n-\t[ -s \"$_git/head-name\" ] && _git_head=\"$(cat \"$_git/head-name\")\"\n+\t[ ! -s \"$_git/HEAD\" ] || { _git_head=\"$(git-symbolic-ref HEAD)\"; _git_head=\"${_git_head#refs/heads/}\"; }\n+\t[ ! -s \"$_git/head-name\" ] || _git_head=\"$(cat \"$_git/head-name\")\"\n fi\n \n # Check if the script requires to be called from the workdir root.\ndiff --git a/cg-admin-rewritehist b/cg-admin-rewritehist\nindex 24670c7..9bb70cf 100755\n--- a/cg-admin-rewritehist\n+++ b/cg-admin-rewritehist\n@@ -131,6 +131,8 @@ # committed a merge between P1 and P2, i\n # and all children of the merge will become merge commits with P1,P2\n # as their parents instead of the merge commit.\n \n+set -e\n+\n USAGE=\"cg-admin-rewritehist [-d TEMPDIR] [-r STARTREV]... [FILTERS] DESTBRANCH\"\n _git_wc_unneeded=1\n _git_requires_root=1\n@@ -167,10 +169,10 @@ done\n \n dstbranch=\"${ARGS[0]}\"\n [ -n \"$dstbranch\" ] || die \"missing branch name\"\n-[ -s \"$_git/refs/heads/$dstbranch\" ] && die \"branch $dstbranch already exists\"\n-[ -s \"$_git/branches/$dstbranch\" ] && die \"branch $dstbranch is already a remote branch\"\n+[ ! -s \"$_git/refs/heads/$dstbranch\" ] || die \"branch $dstbranch already exists\"\n+[ ! -s \"$_git/branches/$dstbranch\" ] || die \"branch $dstbranch is already a remote branch\"\n \n-[ -e \"$tempdir\" ] && die \"$tempdir already exists, please remove it\"\n+[ ! -e \"$tempdir\" ] || die \"$tempdir already exists, please remove it\"\n mkdir -p \"$tempdir/t\"\n cd \"$tempdir/t\"\n \n"}]}