Splitting a command string could lead to unintended arguments. Use an argument list in the execute() function instead.
Showing the changes to scripts (1 of 1 file). Show all
Unified · Split
| @@ -149,11 +149,11 @@ def main(): · 148 unmodified lines | ||
| 149 | 149 | undefined_b = {} |
| 150 | 150 | |
| 151 | 151 | # get undefined items before the commit |
| 152 | − execute("git reset --hard %s" % commit_a) | |
| 152 | + reset(commit_a) | |
| 153 | 153 | undefined_a, _ = check_symbols(args.ignore) |
| 154 | 154 | |
| 155 | 155 | # get undefined items for the commit |
| 156 | − execute("git reset --hard %s" % commit_b) | |
| 156 | + reset(commit_b) | |
| 157 | 157 | undefined_b, defined = check_symbols(args.ignore) |
| 158 | 158 | |
| 159 | 159 | # report cases that are present for the commit but not before |
| @@ -170,7 +170,7 @@ def main(): · 10 unmodified lines | ||
| 170 | 170 | undefined[symbol] = files |
| 171 | 171 | |
| 172 | 172 | # reset to head |
| 173 | − execute("git reset --hard %s" % head) | |
| 173 | + reset(head) | |
| 174 | 174 | |
| 175 | 175 | # default to check the entire tree |
| 176 | 176 | else: |
| @@ -202,6 +202,11 @@ def main(): · 25 unmodified lines | ||
| 202 | 202 | print() # new line |
| 203 | 203 | |
| 204 | 204 | |
| 205 | +def reset(commit): | |
| 206 | + """Reset current git tree to %commit.""" | |
| 207 | + execute(["git", "reset", "--hard", commit]) | |
| 208 | + | |
| 209 | + | |
| 205 | 210 | def yel(string): |
| 206 | 211 | """ |
| 207 | 212 | Color %string yellow. |
| @@ -219,25 +224,25 @@ def red(string): · 11 unmodified lines | ||
| 219 | 224 | def execute(cmd): |
| 220 | 225 | """Execute %cmd and return stdout. Exit in case of error.""" |
| 221 | 226 | try: |
| 222 | − cmdlist = cmd.split(" ") | |
| 223 | − stdout = subprocess.check_output(cmdlist, stderr=subprocess.STDOUT, shell=False) | |
| 227 | + stdout = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False) | |
| 224 | 228 | stdout = stdout.decode(errors='replace') |
| 225 | 229 | except subprocess.CalledProcessError as fail: |
| 226 | − exit("Failed to execute %s\n%s" % (cmd, fail)) | |
| 230 | + exit(fail) | |
| 227 | 231 | return stdout |
| 228 | 232 | |
| 229 | 233 | |
| 230 | 234 | def find_commits(symbol, diff): |
| 231 | 235 | """Find commits changing %symbol in the given range of %diff.""" |
| 232 | − commits = execute("git log --pretty=oneline --abbrev-commit -G %s %s" | |
| 233 | − % (symbol, diff)) | |
| 236 | + commits = execute(["git", "log", "--pretty=oneline", | |
| 237 | + "--abbrev-commit", "-G", | |
| 238 | + symbol, diff]) | |
| 234 | 239 | return [x for x in commits.split("\n") if x] |
| 235 | 240 | |
| 236 | 241 | |
| 237 | 242 | def tree_is_dirty(): |
| 238 | 243 | """Return true if the current working tree is dirty (i.e., if any file has |
| 239 | 244 | been added, deleted, modified, renamed or copied but not committed).""" |
| 240 | − stdout = execute("git status --porcelain") | |
| 245 | + stdout = execute(["git", "status", "--porcelain"]) | |
| 241 | 246 | for line in stdout: |
| 242 | 247 | if re.findall(r"[URMADC]{1}", line[:2]): |
| 243 | 248 | return True |
| @@ -246,7 +251,7 @@ def tree_is_dirty(): · 2 unmodified lines | ||
| 246 | 251 | |
| 247 | 252 | def get_head(): |
| 248 | 253 | """Return commit hash of current HEAD.""" |
| 249 | − stdout = execute("git rev-parse HEAD") | |
| 254 | + stdout = execute(["git", "rev-parse", "HEAD"]) | |
| 250 | 255 | return stdout.strip('\n') |
| 251 | 256 | |
| 252 | 257 | |
| @@ -285,7 +290,7 @@ def find_sims(symbol, ignore, defined=[]): · 32 unmodified lines | ||
| 285 | 290 | def get_files(): |
| 286 | 291 | """Return a list of all files in the current git directory.""" |
| 287 | 292 | # use 'git ls-files' to get the worklist |
| 288 | − stdout = execute("git ls-files") | |
| 293 | + stdout = execute(["git", "ls-files"]) | |
| 289 | 294 | if len(stdout) > 0 and stdout[-1] == "\n": |
| 290 | 295 | stdout = stdout[:-1] |
| 291 | 296 | |