Skip to content

Commit e7814d0

Browse files
committed
umpf: answer interactive prompts with default when --default set
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]>
1 parent 10a5951 commit e7814d0

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

umpf

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ FLAGS=""
3737
PATCH_DIR="umpf-patches"
3838
IDENTICAL=false
3939
STABLE=false
40+
DEFAULT=false
4041
FORCE=false
4142
UPDATE=false
4243
VERBOSE=false
@@ -173,6 +174,7 @@ usage() {
173174
usage: $0 [<options>] [--] <command>
174175
175176
Mandatory arguments to long options are mandatory for short options too.
177+
--default answer interactive prompts with the default option
176178
--auto-rerere automatically try to use rerere after conflicts
177179
--bb with format-patch: write patch series for bitbake
178180
--nix with format-patch: write patch series nix
@@ -245,7 +247,7 @@ setup() {
245247
fi
246248

247249
o="fhilsub:n:p:r:v:"
248-
l="auto-rerere,bb,nix,flags:,force,help,identical,stable,update,base:,name:,patchdir:,relative:,override:,remote:,local,version:"
250+
l="auto-rerere,bb,nix,flags:,default,force,help,identical,stable,update,base:,name:,patchdir:,relative:,override:,remote:,local,version:"
249251
if ! args="$(getopt -n umpf -o "${o}" -l "${l}" -- "${@}")"; then
250252
usage
251253
exit 1
@@ -268,6 +270,9 @@ setup() {
268270
--nix)
269271
NIX=true
270272
;;
273+
--default)
274+
DEFAULT=true
275+
;;
271276
-f|--force)
272277
FORCE=true
273278
;;
@@ -414,6 +419,16 @@ nice_branch() {
414419
read -r -a replies < <(sed -r -n 's,^(remotes/([^/]*)/|heads/)?(.*),\3 \2,p' <<< "${1}")
415420
}
416421

422+
read_interactive() {
423+
local prompt="${1}" def="${2}"
424+
if ${DEFAULT} && [ -n "${def}" ]; then
425+
echo "${prompt}: ${def}"
426+
choice="${def}"
427+
else
428+
read -e -i "${def}" -p "${prompt}: " choice
429+
fi
430+
}
431+
417432
find_branch_rev() {
418433
local name branch remote
419434
local -a branches replies
@@ -438,7 +453,7 @@ find_branch_rev() {
438453
else
439454
info "Branch not found for '${remote}'. Choose alternative branch:"
440455
fi
441-
local i=0 def=0
456+
local i=0 def=0 choice
442457
for branch in "${branches[@]}"; do
443458
nice_branch "${branch}"
444459
echo "${i}) ${replies[1]:+${replies[1]}/}${replies[0]}"
@@ -447,8 +462,8 @@ find_branch_rev() {
447462
fi
448463
i=$((i+1))
449464
done
450-
read -e -i ${def} -p "branch number: " i
451-
nice_branch "${branches[$i]}"
465+
read_interactive "branch number" "${def}"
466+
nice_branch "${branches[${choice}]}"
452467
remote="${replies[1]:-refs/heads}/"
453468
if [ -z "${GIT_REMOTE}" ]; then
454469
GIT_REMOTE="${remote}"
@@ -464,16 +479,15 @@ find_branch_rev() {
464479
bailout "Failed to find '${name}'"
465480
fi
466481
for branch in "${branches[@]}"; do
467-
local b="$(${GIT} rev-parse --verify "${branch}^{}")"
482+
local choice b="$(${GIT} rev-parse --verify "${branch}^{}")"
468483
if [ "${reply}" == "${b}" ]; then
469484
continue
470485
fi
471486
if [ "$(${GIT} merge-base "${reply}" "${branch}")" == "${reply}" ]; then
472487
info "Warning: The following commits are in '${branch}' but not in '${remote}${name}':"
473488
GIT_PAGER="" ${GIT} log --oneline "${reply}...${b}"
474489
if tty -s; then
475-
local choice=""
476-
read -e -i n -p "Use ${branch} instead? [y/n]: " choice
490+
read_interactive "Use ${branch} instead? [y/n]" "n"
477491
if [ "${choice}" == "y" ]; then
478492
reply="${b}"
479493
fi
@@ -489,7 +503,7 @@ list_branch_names() {
489503
}
490504

491505
find_branch_name() {
492-
local head merge mergelog candidate
506+
local head merge mergelog candidate choice
493507
local -a branches replies
494508
head="${1}"
495509
merge="${2}"
@@ -511,7 +525,8 @@ find_branch_name() {
511525
0)
512526
info "No branch found for ${mergelog}"
513527
candidate=$(sed -n "s/^[0-9a-f]* Merge.* '\([^ ]*\)' .*/\1/p" <<< "${mergelog}")
514-
read -e -i "${candidate}" -p "topic: " name
528+
read_interactive "topic" "${candidate}"
529+
name="${choice}"
515530
;;
516531
1)
517532
nice_branch "${branches[0]}"
@@ -646,7 +661,9 @@ import_series() {
646661

647662
if [ -z "${BASE}" ]; then
648663
BASE="$(${GIT} describe "${base_rev}" 2>/dev/null)"
649-
read -e -i "${BASE}" -p "base: " BASE
664+
local choice
665+
read_interactive "base" "${BASE}"
666+
BASE="${choice}"
650667
fi
651668
echo "# umpf-base: ${BASE}" >> "${series}"
652669
if [ -n "${GIT_RELATIVE}" ]; then
@@ -1680,7 +1697,7 @@ apply_to_topic() {
16801697
esac
16811698
16821699
while [ -z "${topic}" ]; do
1683-
local i=0 ret default
1700+
local i=0 choice default
16841701
for branch in "${branch_names[@]}"; do
16851702
echo "${i}) ${branch}"
16861703
if git log --pretty="format:%s" "${base}..${branches[${i}]}" | grep -q "^${match}$"; then
@@ -1690,8 +1707,8 @@ apply_to_topic() {
16901707
done
16911708
echo "s) show patch"
16921709
echo "x) skip patch"
1693-
read -e -p "Branch: " -i "${default}" ret
1694-
case "${ret}" in
1710+
read_interactive "Branch" "${default}"
1711+
case "${choice}" in
16951712
s)
16961713
${GIT} show "${rev}"
16971714
continue
@@ -1700,23 +1717,23 @@ apply_to_topic() {
17001717
return
17011718
;;
17021719
[0-9]*)
1703-
branch="${branch_names[${ret}]}"
1720+
branch="${branch_names[${choice}]}"
17041721
if [ -z "${branch}" ]; then
1705-
echo "'$ret' is not a valid branch number"
1722+
echo "'${choice}' is not a valid branch number"
17061723
fi
17071724
;;
17081725
*)
1709-
echo "Invalid command '$ret'"
1726+
echo "Invalid command '${choice}'"
17101727
continue
17111728
esac
17121729
if ! topic="$(${GIT} rev-parse -q --verify "refs/umpf/${branch}^{}")"; then
1713-
topic="${branches[${ret}]}"
1730+
topic="${branches[${choice}]}"
17141731
fi
17151732
if [ -z "${topic}" ]; then
17161733
local reply
17171734
IDENTICAL=false find_branch_rev "${branch}"
17181735
topic="${reply}"
1719-
branches[${ret}]="${topic}"
1736+
branches[${choice}]="${topic}"
17201737
fi
17211738
echo "${branch}" > "${STATE}/distribute-branch"
17221739
echo "${topic}" > "${STATE}/distribute-topic"

0 commit comments

Comments
 (0)