@matheusbsbs / matheus-bagliano / commits / e2aed5fd5b
Fork 0

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 <liu.denton@gmail.com> Signed-off-by: Ben Keene <seraphire@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ben Keene committed Dec 16, 2019 at 14:02 UTC e2aed5fd5bfb0381a6f69373c01ef09d70467a5c
1 file changed +36 -31
git-p4.py
+36 -31
@@ -167,6 +167,21 @@ def die(msg):
167 sys.stderr.write(msg + "\n")
168 sys.exit(1)
169
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 +
185 def write_pipe(c, stdin):
186 if verbose:
187 sys.stderr.write('Writing pipe: %s\n' % str(c))
@@ -1778,12 +1793,11 @@ class P4Submit(Command, P4UserMap):
1793 if os.stat(template_file).st_mtime > mtime:
1794 return True
1795
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
1801
1802 def get_diff_description(self, editedFiles, filesToAdd, symlinks):
1803 # diff
@@ -2345,31 +2359,22 @@ class P4Submit(Command, P4UserMap):
2359 " --prepare-p4-only")
2360 break
2361 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")
2378 break
2379
2380 chdir(self.oldWorkingDirectory)