nixpkgs-ml-tools

nixpkgs mailing list tools
git clone git://jb55.com/nixpkgs-ml-tools
Log | Files | Refs | README | LICENSE

notmuch-am (5007B)


      1 #!/usr/bin/env bash
      2 
      3 set -eo pipefail
      4 
      5 # ###
      6 #
      7 # Generated by argbash.io
      8 #
      9 # ###
     10 
     11 die()
     12 {
     13     local _ret="${2:-1}"
     14     test "${_PRINT_HELP:-no}" = yes && print_help >&2
     15     echo "$1" >&2
     16     exit "${_ret}"
     17 }
     18 
     19 
     20 begins_with_short_option()
     21 {
     22     local first_option all_short_options='h'
     23     first_option="${1:0:1}"
     24     test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
     25 }
     26 
     27 # THE DEFAULTS INITIALIZATION - POSITIONALS
     28 _positionals=()
     29 # THE DEFAULTS INITIALIZATION - OPTIONALS
     30 _arg_am="off"
     31 _arg_3="off"
     32 _arg_amsignoff="off"
     33 _arg_fuzzy="off"
     34 _arg_fuzzy_search=""
     35 
     36 
     37 print_help()
     38 {
     39     printf '%s\n' "Use b4 to apply a patchset from a thread."
     40     printf 'Usage: %s [-h|--help] [--(no-)am] <thread-id>\n' "$0"
     41     printf '\t%s\n' "<thread-id>: The notmuch thread id of the thread to apply"
     42     printf '\t%s\n' "--am, --no-am: Call git-am instead of generating a mbox file (off by default)"
     43     printf '\t%s\n' "-3: Call git-am with -3 (off by default, only effective with --am)"
     44     printf '\t%s\n' "-s, --signoff: Call git-am with --signoff (off by default, only effective with --am)"
     45     printf '\t%s\n' "-f, --fuzzy <search>: Use fzf to interactively select the thread for the patchset to apply, pass <search> to notmuch-search, if given"
     46     printf '\t%s\n' "-h, --help: Prints help"
     47     printf %s "\
     48 
     49 Example
     50 
     51   $ notmuch-am thread:000000000001b70e
     52   Analyzing 6 messages in the thread
     53   ---
     54   Writing ./20210206_jb55_b4_init_at_0_6_2.mbx
     55     [PATCH] b4: init at 0.6.2
     56       + Reviewed-by: Xinglu Chen <public@yoctocell.xyz> (✓ DKIM/yoctocell.xyz)
     57       + Reviewed-by: Matthias Beyer <mail@beyermatthias.de>
     58 "
     59 }
     60 parse_commandline()
     61 {
     62     _positionals_count=0
     63     while test $# -gt 0
     64     do
     65         _key="$1"
     66         case "$_key" in
     67             -h|--help)
     68                 print_help
     69                 exit 0
     70                 ;;
     71             -h*)
     72                 print_help
     73                 exit 0
     74                 ;;
     75             --no-am|--am)
     76                 _arg_am="on"
     77                 test "${1:0:5}" = "--no-" && _arg_am="off"
     78                 ;;
     79             -3)
     80                 _arg_3="on"
     81                 ;;
     82             -s|--signoff)
     83                 _arg_amsignoff="on"
     84                 ;;
     85             -l|--add-link)
     86                 _arg_b4addlink="on"
     87                 ;;
     88             -f|--fuzzy)
     89                 _arg_fuzzy="on"
     90                 _arg_fuzzy_search="${2:-*}"
     91                 ;;
     92             *)
     93                 _last_positional="$1"
     94                 _positionals+=("$_last_positional")
     95                 _positionals_count=$((_positionals_count + 1))
     96                 ;;
     97         esac
     98         shift
     99     done
    100 }
    101 
    102 
    103 handle_passed_args_count()
    104 {
    105     local _required_args_string="'thread-id'"
    106     if [[ "${_positionals_count}" -ge 2 ]] && [[ "$_arg_fuzzy" == "off" ]]; then
    107         _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require exactly 1 (namely: $_required_args_string), but got only ${_positionals_count}." 1
    108     fi
    109     if [[ "${_positionals_count}" -lt 1 ]] && [[ "$_arg_fuzzy" == "off" ]]; then
    110         _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect exactly 1 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}')." 1
    111     fi
    112 }
    113 
    114 
    115 assign_positional_args()
    116 {
    117     local _positional_name _shift_for=$1
    118     _positional_names="_arg_thread_id "
    119 
    120     shift "$_shift_for"
    121     for _positional_name in ${_positional_names}
    122     do
    123         test $# -gt 0 || break
    124         eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
    125         shift
    126     done
    127 }
    128 
    129 parse_commandline "$@"
    130 handle_passed_args_count
    131 assign_positional_args 1 "${_positionals[@]}"
    132 
    133 # ###
    134 #
    135 # / Generated by argbash.io
    136 #
    137 # ###
    138 
    139 B4AM_ARGS=${B4AM_ARGS:-""}
    140 
    141 main () {
    142     mbox=$(mktemp)
    143     notmuch show --format=mbox "$1" > "$mbox"
    144     msgid=$(grep -i ^message-id "$mbox" | cut -d" " -f2 | head -n1)
    145 
    146     if [[ "$_arg_b4addlink" == "on" ]]; then
    147         B4AM_ARGS+=" --add-link"
    148     fi
    149 
    150     B4AM_ARGS+=" $msgid"
    151 
    152     if [[ "$_arg_am" == "on" ]]; then
    153         AM_ARGS="$NOTMUCH_AM_ARGS"
    154         if [[ "$_arg_3" == "on" ]]; then
    155             AM_ARGS+=" -3"
    156         fi
    157         if [[ "$_arg_amsignoff" == "on" ]]; then
    158             AM_ARGS+=" --signoff"
    159         fi
    160 
    161         b4 am $B4AM_ARGS -m "$mbox" -o - | git am $AM_ARGS
    162     else
    163         b4 am $B4AM_ARGS -m "$mbox"
    164     fi
    165 
    166     rm -f "$mbox"
    167 }
    168 
    169 thread_id="$_arg_thread_id"
    170 
    171 if [[ "$_arg_fuzzy" == "on" ]]; then
    172     preview_script='echo {} | sed "s,\t.*,,; s,^,thread:," | xargs notmuch search --output=files --format=text | xargs grep -ihE "^Subject" | sort'
    173 
    174     user_email="$(git config user.email)"
    175 
    176     thread_id=$(notmuch search --output=summary --format=json "$_arg_fuzzy_search" | \
    177         jq -rcC '.[] | [.thread,.date_relative,.subject,.authors] | @tsv' | \
    178         fzf -m --preview "$preview_script" | \
    179         sed 's,\t.*,,; s,^,thread:,')
    180 fi
    181 
    182 [ -n "$1" ] || usage && main "$thread_id"
    183