#!/bin/sh
# ------------------------------------------------------------------------------
#
# git-qseries.sh: Implements the "git qseries" extension command.
#
# This command lists entries from the patch queue series file, or optionally,
# patch files which are present in the patch queue directory, but which do NOT
# have a registration entry in the series file.  Optionally, each listed entry
# may be qualified by its sequence number, status flag, and/or its descriptive
# summary, abstracted from the associated patch file itself.
#
# $Id$
#
# ------------------------------------------------------------------------------
#
# I'd have liked to call this a "SYNOPSIS", (which is what it is), but git's
# git-sh-setup script requires the much less appropriate name "OPTIONS_SPEC",
# (which describes only a small subset of its actual content).
#
OPTION_VERBOSE_DEFINED=true OPTIONS_SPEC="\
git qseries [-v | --verbose] [-s | --summary] [-m | --missing]

List entries from the patch queue series file.
--
m,missing!   list patches which lack a series file entry
s,summary!   include summary lines from patch headers
v,verbose!   show sequence numbers and status flag"
#
# ------------------------------------------------------------------------------
#
# $Id$
#
# Written by Keith Marshall <keith@users.osdn.me>
# Copyright (C) 2018-2020, 2022, Keith Marshall
#
#
# This file is part of the Git-MQ program suite.
#
# The Git-MQ program suite is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public Licence
# as published by the Free Software Foundation, either version 3 of
# the Licence, or (at your option) any later version.
#
# The Git-MQ program suite is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public Licence for more details.
#
# You should have received a copy of the GNU General Public Licence
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# ------------------------------------------------------------------------------
#
# Although they may not always have any effect, all Git-MQ commands, like
# their hg counterparts, are expected to accept the global verbosity, and
# colour control options; ensure that they are declared:
#
OPTIONS_SPEC="$OPTIONS_SPEC
colour?*     generic output colour control -- may have no effect"
${OPTION_VERBOSE_DEFINED-false} || OPTIONS_SPEC="$OPTIONS_SPEC
v,verbose!*  generic verbosity selector -- may have no effect"

# For Git-MQ options, such as "--colour", we prefer a spelling convention
# which conforms to "World English" standards; however, git itself adopts
# "US English" convention.  Thus, we must also accommodate users who will
# specify "--color" instead of "--colour", without creating any ambiguity
# in the possible abbreviations; to achieve this, we check for "--color",
# among the command-line arguments, replacing it with "--colour", BEFORE
# we allow git to parse them.
#
for mq_argv
do case "$mq_argv" in
     --color*) mq_argv=`echo $mq_argv | sed 's/^--colo/&u/'` ;;
     --no-color) mq_argv="--no-colour" ;;
   esac
   ${mq_argv_init-true} && { set -- "$mq_argv"; mq_argv_init=false
   } || set -- "$@" "$mq_argv"
done

# Now, we may let git parse the command line, and set up its shell script
# processing environment, for use within a git working directory tree.
#
SUBDIRECTORY_OK=true . "`git --exec-path`/git-sh-setup" && require_work_tree

libexecdir=`dirname "$0"`
test `basename "$libexecdir"` = bin && libexecdir=`dirname "$libexecdir"`
libexecdir="$libexecdir/libexec/git-mq/${VERSION=1.0}"

mq_require(){ test -f "$1" || die "fatal: '$1' not found."; . "$1"; }
mq_require "$libexecdir/git-mq-setup.sh"

# Parse any command line options, which the user may have specified;
# the git_mq_getopt function will implicitly handle the default global
# options, but we must explicitly handle command specific effects.
#
while git_mq_getopt "$@"
do case $1 in
     -v) mq_output='printf "%*d %s %s\n", fw, idx, state[$1],' ;;
     -m) mq_qseries_show_missing=true ;;
     -s) mq_qseries_append_summary=1 ;;
   esac; shift
done

# Set up the listing style which is appropriate to the "--missing"
# option; this overrides the default listing style.
#
${mq_qseries_show_missing-false} && {
  mq_qseries_report_setup='
    idx0 = entries;
    while( (getline < "/dev/stdin") > 0 )
      for( idx = 1; NF >= idx; idx++ )
	if( ! ($idx in state) )
	{ state[$idx] = "D"; entry[$idx] = entries;
	  series[entries++] = $idx;
	}'
  test "${mq_output+set}" = set && mq_output='printf "%s %s\n", state[$1],'
  mq_enumerate_patches(){
    cd "$1" && { for patch in *
       do test -f "$patch" && case $patch in guards | series | status) ;;
	   *) echo "$patch" ;; esac
       done
    }
  }
}
# Hand off the listing task to the generalized "git qseries" output
# module; this serves as a common back-end handler for all commands
# which are related to "git qseries"; it also specifies the default
# style for the output from these commands.
#
git_mq_enable_colour_pager mq_require mq-series-list
#
# ------------------------------------------------------------------------------
# $RCSfile$: end of file
