Skip to content

Commit

Permalink
Escape variables with %% in external commands (#1095)
Browse files Browse the repository at this point in the history
Example: %%(refname)

References #1091
  • Loading branch information
koutcher authored May 21, 2021
1 parent d4bbad8 commit 6134336
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,17 +335,18 @@ format_append_arg(struct format_context *format, const char ***dst_argv, const c

while (arg) {
const char *var = strstr(arg, "%(");
const char *esc = var > arg && *(var - 1) == '%' ? var - 1 : NULL;
const char *closing = var ? strchr(var, ')') : NULL;
const char *next = closing ? closing + 1 : NULL;
const int len = var ? var - arg : strlen(arg);
const char *next = esc > arg ? esc : closing ? closing + 1 : NULL;
const int len = var && !esc ? var - arg : esc > arg ? esc - arg : next ? next - ++arg : strlen(arg);

if (var && !closing)
return false;

if (len && !string_format_from(format->buf, &format->bufpos, "%.*s", len, arg))
return false;

if (var && !format_expand_arg(format, var, next))
if (var && !esc && !format_expand_arg(format, var, next))
return false;

arg = next;
Expand Down
19 changes: 19 additions & 0 deletions test/tigrc/escape-var-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

. libtest.sh
. libgit.sh

tigrc <<EOF
bind generic 1 @sh -c "echo '%%(refname)=%(refname)' > $HOME/shell-command.txt"
EOF

steps '
1
'

in_work_dir create_repo_from_tgz "$base_dir/files/scala-js-benchmarks.tgz"
test_tig

assert_equals 'shell-command.txt' <<EOF
%(refname)=master
EOF

0 comments on commit 6134336

Please sign in to comment.