Skip to content

Commit

Permalink
umpf: answer interactive prompts with default when --default set
Browse files Browse the repository at this point in the history
When building or tagging an umpf, umpf can ask which branch to use when
multiple are available. Piping yes(1) into umpf in this case doesn't
work, because the variable populated by read in that case is left empty.

Provide for this use case, a --default option that will take a default
if one is known.

Signed-off-by: Ahmad Fatoum <[email protected]>
  • Loading branch information
a3f committed Jan 31, 2025
1 parent 10a5951 commit 02cc048
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions umpf
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ FLAGS=""
PATCH_DIR="umpf-patches"
IDENTICAL=false
STABLE=false
DEFAULT=false
FORCE=false
UPDATE=false
VERBOSE=false
Expand Down Expand Up @@ -173,6 +174,7 @@ usage() {
usage: $0 [<options>] [--] <command>
Mandatory arguments to long options are mandatory for short options too.
--default answer interactive prompts with the default option
--auto-rerere automatically try to use rerere after conflicts
--bb with format-patch: write patch series for bitbake
--nix with format-patch: write patch series nix
Expand Down Expand Up @@ -245,7 +247,7 @@ setup() {
fi

o="fhilsub:n:p:r:v:"
l="auto-rerere,bb,nix,flags:,force,help,identical,stable,update,base:,name:,patchdir:,relative:,override:,remote:,local,version:"
l="auto-rerere,bb,nix,flags:,default,force,help,identical,stable,update,base:,name:,patchdir:,relative:,override:,remote:,local,version:"
if ! args="$(getopt -n umpf -o "${o}" -l "${l}" -- "${@}")"; then
usage
exit 1
Expand All @@ -268,6 +270,9 @@ setup() {
--nix)
NIX=true
;;
--default)
DEFAULT=true
;;
-f|--force)
FORCE=true
;;
Expand Down Expand Up @@ -414,6 +419,16 @@ nice_branch() {
read -r -a replies < <(sed -r -n 's,^(remotes/([^/]*)/|heads/)?(.*),\3 \2,p' <<< "${1}")
}

read_interactive() {
local prompt="$1" def="$2"
if ${DEFAULT} && [ -n "${def}" ]; then
echo "${prompt}: ${def}"
choice="${def}"
else
read -e -i "${def}" -p "${prompt}: " choice
fi
}

find_branch_rev() {
local name branch remote
local -a branches replies
Expand Down Expand Up @@ -447,8 +462,8 @@ find_branch_rev() {
fi
i=$((i+1))
done
read -e -i ${def} -p "branch number: " i
nice_branch "${branches[$i]}"
read_interactive "branch number" "${def}"
nice_branch "${branches[$choice]}"
remote="${replies[1]:-refs/heads}/"
if [ -z "${GIT_REMOTE}" ]; then
GIT_REMOTE="${remote}"
Expand All @@ -472,8 +487,7 @@ find_branch_rev() {
info "Warning: The following commits are in '${branch}' but not in '${remote}${name}':"
GIT_PAGER="" ${GIT} log --oneline "${reply}...${b}"
if tty -s; then
local choice=""
read -e -i n -p "Use ${branch} instead? [y/n]: " choice
read_interactive "Use ${branch} instead? [y/n]" "n"
if [ "${choice}" == "y" ]; then
reply="${b}"
fi
Expand Down Expand Up @@ -511,7 +525,8 @@ find_branch_name() {
0)
info "No branch found for ${mergelog}"
candidate=$(sed -n "s/^[0-9a-f]* Merge.* '\([^ ]*\)' .*/\1/p" <<< "${mergelog}")
read -e -i "${candidate}" -p "topic: " name
read_interactive "topic" "${candidate}"
name="${choice}"
;;
1)
nice_branch "${branches[0]}"
Expand Down Expand Up @@ -646,7 +661,8 @@ import_series() {

if [ -z "${BASE}" ]; then
BASE="$(${GIT} describe "${base_rev}" 2>/dev/null)"
read -e -i "${BASE}" -p "base: " BASE
read_interactive "base" "${BASE}"
BASE="${choice}"
fi
echo "# umpf-base: ${BASE}" >> "${series}"
if [ -n "${GIT_RELATIVE}" ]; then
Expand Down Expand Up @@ -1680,7 +1696,7 @@ apply_to_topic() {
esac
while [ -z "${topic}" ]; do
local i=0 ret default
local i=0 default
for branch in "${branch_names[@]}"; do
echo "${i}) ${branch}"
if git log --pretty="format:%s" "${base}..${branches[${i}]}" | grep -q "^${match}$"; then
Expand All @@ -1690,8 +1706,8 @@ apply_to_topic() {
done
echo "s) show patch"
echo "x) skip patch"
read -e -p "Branch: " -i "${default}" ret
case "${ret}" in
read_interactive "Branch" "${default}"
case "${choice}" in
s)
${GIT} show "${rev}"
continue
Expand All @@ -1700,23 +1716,23 @@ apply_to_topic() {
return
;;
[0-9]*)
branch="${branch_names[${ret}]}"
branch="${branch_names[${choice}]}"
if [ -z "${branch}" ]; then
echo "'$ret' is not a valid branch number"
echo "'$choice' is not a valid branch number"
fi
;;
*)
echo "Invalid command '$ret'"
echo "Invalid command '$choice'"
continue
esac
if ! topic="$(${GIT} rev-parse -q --verify "refs/umpf/${branch}^{}")"; then
topic="${branches[${ret}]}"
topic="${branches[${choice}]}"
fi
if [ -z "${topic}" ]; then
local reply
IDENTICAL=false find_branch_rev "${branch}"
topic="${reply}"
branches[${ret}]="${topic}"
branches[${choice}]="${topic}"
fi
echo "${branch}" > "${STATE}/distribute-branch"
echo "${topic}" > "${STATE}/distribute-topic"
Expand Down

0 comments on commit 02cc048

Please sign in to comment.