Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

umpf: answer interactive prompts with default when --default set #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use ${...} for all variables. We're not fully consistent overall, but it used in most cases.
And choice schould be a local variable.

Same everywhere else.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

choice is intentionally not local, so it can be used outside the function. I am not good enough at bash to make it work with a local, can you elaborate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, I put that comment in the wrong place. It should be local in the calling function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding was that choice as written by read_interactive is a global variable, so the callers need to access to choice global variable. I need to try out whether it still works with local in the calling function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A local variable in bash is valid until the end of the function, including all functions that are called within that function.


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