pw-apply: Unify the merge paths

Auto-merge only works with -s today, while BPF folks seem
to favor -c. Unify the two paths. Since both -c and -s
auto-detect merges make merges the default and let user
prevent merges instead. Or force a manual merge.
Both of those options will likely never be used anyway :)

Note that use of auto-merge changes the format of merge
commits slightly. A Link is added at the end, but more
notably we use a real branch name, e.g.:

 Merge branch 'net-give-napi_threaded_poll-some-love'

instead of the constructed message:

 Merge branch 'net: give napi_threaded_poll() some love'

the subject now lands as the first line of the body:

    Merge branch 'net-give-napi_threaded_poll-some-love'

    Eric Dumazet says:

    ====================
    net: give napi_threaded_poll() some love

    There is interest to revert commit 4cd13c21b207...

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
diff --git a/pw-apply b/pw-apply
index 5fc37a2..89ec22a 100755
--- a/pw-apply
+++ b/pw-apply
@@ -8,7 +8,7 @@
 usage()
 {
   cat <<-EOF
-  usage: pw-apply [-h] [-s SERIES] [-b MBOX] [-m BRANCHNAME] [-M]
+  usage: pw-apply [-h] [-s SERIES] [-b MBOX] [-P] [-M]
                   [-a] [-3] [-N] [-e] [-T]
                   -- [-C] [-a ACKEDBY] [-r REVIEWEDBY] [-t TESTEDBY]
 EOF
@@ -83,21 +83,20 @@
 cover_from_url()
 {
   curl -s $1 | gunzip -f -c > tmp.i
-  series_num=`grep "href=\"/series" tmp.i|cut -d/ -f3|head -1`
+  series=`grep "href=\"/series" tmp.i|cut -d/ -f3|head -1`
   cover_url=`grep "href=\"/project/netdevbpf/cover" tmp.i|cut -d\" -f2`
-  if [ ! -z "$cover_url" ]; then
-    curl -s https://patchwork.kernel.org${cover_url}mbox/ | gunzip -f -c > cover.i
-    merge="1"
+  if [ -z "$cover_url" ]; then
+    merge=
   fi
-  curl -s https://patchwork.kernel.org/series/$series_num/mbox/ | gunzip -f -c > mbox.i
 }
 
+auto_branch="1"
 edits=""
 am_flags=""
-branch="mbox"
 series=""
 accept=""
-merge=""
+merge="1"
+no_merge=""
 mbox=""
 cover=""
 cover_tags="y"
@@ -110,16 +109,19 @@
     -a | --accept ) accept="1"; shift ;;
     -e | --do-edits ) edits="1"; shift ;;
     -N | --no-mb2q ) mb2q_normalize=""; am_flags="$am_flags -s"; shift ;;
-    -m | --merge ) merge="1"; branch="$2"; shift 2 ;;
-    -M | --auto-merge) merge="1"; auto_branch="1"; shift ;;
+    -P | --no-merge ) no_merge="1"; shift ;;
+    -M | --manual-merge) auto_branch=""; shift ;;
     -b | --mbox ) mbox="$2"; shift 2 ;;
-    -c | --cover) branch="tmp"; cover="$2"; shift 2 ;;
+    -c | --cover) cover="$2"; shift 2 ;;
     -T | --no-cover-tags) cover_tags=""; shift ;;
     -h | --help ) usage; break ;;
     -- ) shift; break ;;
     * )  break ;;
   esac
 done
+# Load the info from cover first, it may will populate $series and $merge
+[ ! -z "$cover" ]  && cover_from_url $cover
+
 [ ! -z "$auto_branch" ] && [ -z "$series" ] && usage
 [ ! -z "$mbox" ]   && [ ! -z "$series" ] && usage
 [   -z "$mbox" ]   && [   -z "$series" ] && [ -z "$cover" ] && usage
@@ -127,9 +129,10 @@
 [ ! -z "$series" ] && mbox_from_series $series
 [ ! -z "$mbox" ]   && mbox_from_url $mbox
 [ ! -z "$accept" ] && accept_series $series
-[ ! -z "$cover" ]  && cover_from_url $cover
+[ ! -z "$no_merge" ] && merge=
 
 target=$(git branch --show-current)
+branch="mbox"
 
 body=
 author=XYZ
@@ -178,29 +181,7 @@
 git checkout "$target"
 if [ ! -z "$merge" ]; then
   git merge --stat --log --no-edit --no-ff $branch
-  if [ ! -z "$cover" ]; then
-    author=$(grep 'X-Patchwork-Submitter:' cover.i | cut -d' ' -f2- | cut -d'<' -f1 | awk '{$1=$1;print}')
-    if [[ "$author" =~ ^=\?utf-8\?b\?(.*)\?=$ ]]; then
-	    # strip away "=?utf-8?b?" prefix and "?=" suffix and base64-decode
-	    author=$(echo "${BASH_REMATCH[1]}" | base64 -d)
-    elif [[ "$author" =~ ^=\?utf-8\?q\?(.*)\?=$ ]]; then
-	    # strip away "=?utf-8?q?" prefix and "?=" suffix and quoted-printable-decode
-	    author=$(echo "${BASH_REMATCH[1]}" | tr '_' ' ' | perl -MMIME::QuotedPrint -0777 -nle 'print decode_qp($_)')
-    fi
-    # Extract cover letter subject, potentially split into two lines
-    branch_name=$(awk '/^\w+: / { subj=0 } /^Subject: / { subj = 1 } subj { print $0 }' cover.i | tr -d '\n' | cut -d']' -f2 | cut -c 2-)
-    text=`grep -A300 'X-Mailing-List:' cover.i |tail --lines=+3|grep -B300 -E "^.*\([0-9]+\):$"|head --lines=-2`
-    git commit --amend --signoff -F- <<EOF
-Merge branch '$branch_name'
-
-$author says:
-
-====================
-$text
-====================
-EOF
-  else
-    git commit --amend --signoff -F- <<EOF
+  git commit --amend --signoff -F- <<EOF
 Merge branch '$branch'
 
 $author says:
@@ -213,7 +194,6 @@
 
 Link: https://lore.kernel.org/r/$cover_msgid
 EOF
-  fi
 else
   git merge --stat --ff $branch
 fi