Skip to content

Commit e2aed5f

Browse files
seraphiregitster
authored andcommitted
git-p4: yes/no prompts should sanitize user text
When prompting the user interactively for direction, the tests are not forgiving of user input format. For example, the first query asks for a yes/no response. If the user enters the full word "yes" or "no" or enters a capital "Y" the test will fail. Create a new function, prompt(prompt_text) where * prompt_text is the text prompt for the user * returns a single character where valid return values are found by inspecting prompt_text for single characters surrounded by square brackets This new function must prompt the user for input and sanitize it by converting the response to a lower case string, trimming leading and trailing spaces, and checking if the first character is in the list of choices. If it is, return the first letter. Change the current references to raw_input() to use this new function. Since the method requires the returned text to be one of the available choices, remove the loop from the calling code that handles response verification. Thanks-to: Denton Liu <[email protected]> Signed-off-by: Ben Keene <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 53a06cf commit e2aed5f

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

git-p4.py

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ def die(msg):
167167
sys.stderr.write(msg + "\n")
168168
sys.exit(1)
169169

170+
def prompt(prompt_text):
171+
""" Prompt the user to choose one of the choices
172+
173+
Choices are identified in the prompt_text by square brackets around
174+
a single letter option.
175+
"""
176+
choices = set(m.group(1) for m in re.finditer(r"\[(.)\]", prompt_text))
177+
while True:
178+
response = raw_input(prompt_text).strip().lower()
179+
if not response:
180+
continue
181+
response = response[0]
182+
if response in choices:
183+
return response
184+
170185
def write_pipe(c, stdin):
171186
if verbose:
172187
sys.stderr.write('Writing pipe: %s\n' % str(c))
@@ -1778,12 +1793,11 @@ def edit_template(self, template_file):
17781793
if os.stat(template_file).st_mtime > mtime:
17791794
return True
17801795

1781-
while True:
1782-
response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1783-
if response == 'y':
1784-
return True
1785-
if response == 'n':
1786-
return False
1796+
response = prompt("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1797+
if response == 'y':
1798+
return True
1799+
if response == 'n':
1800+
return False
17871801

17881802
def get_diff_description(self, editedFiles, filesToAdd, symlinks):
17891803
# diff
@@ -2345,31 +2359,22 @@ def run(self, args):
23452359
" --prepare-p4-only")
23462360
break
23472361
if i < last:
2348-
quit = False
2349-
while True:
2350-
# prompt for what to do, or use the option/variable
2351-
if self.conflict_behavior == "ask":
2352-
print("What do you want to do?")
2353-
response = raw_input("[s]kip this commit but apply"
2354-
" the rest, or [q]uit? ")
2355-
if not response:
2356-
continue
2357-
elif self.conflict_behavior == "skip":
2358-
response = "s"
2359-
elif self.conflict_behavior == "quit":
2360-
response = "q"
2361-
else:
2362-
die("Unknown conflict_behavior '%s'" %
2363-
self.conflict_behavior)
2364-
2365-
if response[0] == "s":
2366-
print("Skipping this commit, but applying the rest")
2367-
break
2368-
if response[0] == "q":
2369-
print("Quitting")
2370-
quit = True
2371-
break
2372-
if quit:
2362+
# prompt for what to do, or use the option/variable
2363+
if self.conflict_behavior == "ask":
2364+
print("What do you want to do?")
2365+
response = prompt("[s]kip this commit but apply the rest, or [q]uit? ")
2366+
elif self.conflict_behavior == "skip":
2367+
response = "s"
2368+
elif self.conflict_behavior == "quit":
2369+
response = "q"
2370+
else:
2371+
die("Unknown conflict_behavior '%s'" %
2372+
self.conflict_behavior)
2373+
2374+
if response == "s":
2375+
print("Skipping this commit, but applying the rest")
2376+
if response == "q":
2377+
print("Quitting")
23732378
break
23742379

23752380
chdir(self.oldWorkingDirectory)

0 commit comments

Comments
 (0)