Skip to content

Commit bc85523

Browse files
committed
Merge branch 'bk/p4-misc-usability'
Miscellaneous small UX improvements on "git-p4". * bk/p4-misc-usability: git-p4: show detailed help when parsing options fail git-p4: yes/no prompts should sanitize user text
2 parents 0a76bd7 + 608e380 commit bc85523

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

git-p4.py

Lines changed: 42 additions & 32 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))
@@ -1784,12 +1799,11 @@ def edit_template(self, template_file):
17841799
if os.stat(template_file).st_mtime > mtime:
17851800
return True
17861801

1787-
while True:
1788-
response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1789-
if response == 'y':
1790-
return True
1791-
if response == 'n':
1792-
return False
1802+
response = prompt("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1803+
if response == 'y':
1804+
return True
1805+
if response == 'n':
1806+
return False
17931807

17941808
def get_diff_description(self, editedFiles, filesToAdd, symlinks):
17951809
# diff
@@ -2351,31 +2365,22 @@ def run(self, args):
23512365
" --prepare-p4-only")
23522366
break
23532367
if i < last:
2354-
quit = False
2355-
while True:
2356-
# prompt for what to do, or use the option/variable
2357-
if self.conflict_behavior == "ask":
2358-
print("What do you want to do?")
2359-
response = raw_input("[s]kip this commit but apply"
2360-
" the rest, or [q]uit? ")
2361-
if not response:
2362-
continue
2363-
elif self.conflict_behavior == "skip":
2364-
response = "s"
2365-
elif self.conflict_behavior == "quit":
2366-
response = "q"
2367-
else:
2368-
die("Unknown conflict_behavior '%s'" %
2369-
self.conflict_behavior)
2370-
2371-
if response[0] == "s":
2372-
print("Skipping this commit, but applying the rest")
2373-
break
2374-
if response[0] == "q":
2375-
print("Quitting")
2376-
quit = True
2377-
break
2378-
if quit:
2368+
# prompt for what to do, or use the option/variable
2369+
if self.conflict_behavior == "ask":
2370+
print("What do you want to do?")
2371+
response = prompt("[s]kip this commit but apply the rest, or [q]uit? ")
2372+
elif self.conflict_behavior == "skip":
2373+
response = "s"
2374+
elif self.conflict_behavior == "quit":
2375+
response = "q"
2376+
else:
2377+
die("Unknown conflict_behavior '%s'" %
2378+
self.conflict_behavior)
2379+
2380+
if response == "s":
2381+
print("Skipping this commit, but applying the rest")
2382+
if response == "q":
2383+
print("Quitting")
23792384
break
23802385

23812386
chdir(self.oldWorkingDirectory)
@@ -4146,7 +4151,12 @@ def main():
41464151
description = cmd.description,
41474152
formatter = HelpFormatter())
41484153

4149-
(cmd, args) = parser.parse_args(sys.argv[2:], cmd);
4154+
try:
4155+
(cmd, args) = parser.parse_args(sys.argv[2:], cmd);
4156+
except:
4157+
parser.print_help()
4158+
raise
4159+
41504160
global verbose
41514161
verbose = cmd.verbose
41524162
if cmd.needsGit:

0 commit comments

Comments
 (0)