Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add support to confirm the execution of a binding #52

Closed
wants to merge 1 commit into from

2 participants

Vivien Didelot Jonas Fonseca
Vivien Didelot
  • User-defined commands prefix with a '?' will be prompted for confirmation before being executed. This can be useful for sensible commands such as git revert, etc.
  • By the way, the built-in 'G' binding for git gc, and 'C' binding for git cherry-pick now need confirmation.

Examples of bindings:

# Ask before rebasing
bind main B !?git rebase -i %(commit)
# Ask before cleaning but do not display output
bind generic ! !@?git clean -fdx

Signed-off-by: Vivien Didelot vivien@didelot.org

Vivien Didelot vivien Add support to confirm the execution of a binding
* User-defined commands prefix with a '?' will be prompted for
  confirmation before being executed. This can be useful for sensible
  commands such as `git revert`, etc.
* By the way, the built-in 'G' binding for `git gc`, and 'C' binding for
  `git cherry-pick` now need confirmation.

Examples of bindings:

    # Ask before rebasing
    bind main B !?git rebase -i %(commit)
    # Ask before cleaning but do not display output
    bind generic ! !@?git clean -fdx

Signed-off-by: Vivien Didelot <vivien@didelot.org>
9237514
Jonas Fonseca
Owner

Thanks, reworked version committed as 85babe5.

Jonas Fonseca jonas closed this
Vivien Didelot

Great! I've commented the commit about a small issue (see line 3275). Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 11, 2012
  1. Vivien Didelot

    Add support to confirm the execution of a binding

    vivien authored
    * User-defined commands prefix with a '?' will be prompted for
      confirmation before being executed. This can be useful for sensible
      commands such as `git revert`, etc.
    * By the way, the built-in 'G' binding for `git gc`, and 'C' binding for
      `git cherry-pick` now need confirmation.
    
    Examples of bindings:
    
        # Ask before rebasing
        bind main B !?git rebase -i %(commit)
        # Ask before cleaning but do not display output
        bind generic ! !@?git clean -fdx
    
    Signed-off-by: Vivien Didelot <vivien@didelot.org>
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 11 deletions.
  1. +39 −9 tig.c
  2. +3 −2 tigrc.5.txt
48 tig.c
View
@@ -1104,6 +1104,7 @@ struct run_request {
int key;
const char **argv;
bool silent;
+ bool confirm;
};
static struct run_request *run_request;
@@ -1112,7 +1113,8 @@ static size_t run_requests;
DEFINE_ALLOCATOR(realloc_run_requests, struct run_request, 8)
static bool
-add_run_request(struct keymap *keymap, int key, const char **argv, bool silent, bool force)
+add_run_request(struct keymap *keymap, int key, const char **argv,
+ bool silent, bool force, bool confirm)
{
struct run_request *req;
@@ -1127,6 +1129,7 @@ add_run_request(struct keymap *keymap, int key, const char **argv, bool silent,
req = &run_request[run_requests++];
req->silent = silent;
+ req->confirm = confirm;
req->keymap = keymap;
req->key = key;
@@ -1150,10 +1153,10 @@ add_builtin_run_requests(void)
const char *commit[] = { "git", "commit", NULL };
const char *gc[] = { "git", "gc", NULL };
- add_run_request(get_keymap("main"), 'C', cherry_pick, FALSE, FALSE);
- add_run_request(get_keymap("status"), 'C', commit, FALSE, FALSE);
- add_run_request(get_keymap("branch"), 'C', checkout, FALSE, FALSE);
- add_run_request(get_keymap("generic"), 'G', gc, FALSE, FALSE);
+ add_run_request(get_keymap("main"), 'C', cherry_pick, FALSE, FALSE, TRUE);
+ add_run_request(get_keymap("status"), 'C', commit, FALSE, FALSE, FALSE);
+ add_run_request(get_keymap("branch"), 'C', checkout, FALSE, FALSE, FALSE);
+ add_run_request(get_keymap("generic"), 'G', gc, FALSE, FALSE, TRUE);
}
/*
@@ -1524,12 +1527,22 @@ option_bind_command(int argc, const char *argv[])
}
}
if (request == REQ_UNKNOWN && *argv[2]++ == '!') {
- bool silent = *argv[2] == '@';
+ bool silent = false;
+ bool confirm = false;
- if (silent)
+ if (*argv[2] == '@') {
+ silent = true;
argv[2]++;
- return add_run_request(keymap, key, argv + 2, silent, TRUE)
- ? OPT_OK : OPT_ERR_OUT_OF_MEMORY;
+ }
+
+ if (*argv[2] == '?') {
+ confirm = true;
+ argv[2]++;
+ }
+
+ if (add_run_request(keymap, key, argv + 2, silent, TRUE, confirm))
+ return OPT_OK;
+ return OPT_ERR_OUT_OF_MEMORY;
}
if (request == REQ_UNKNOWN)
return OPT_ERR_UNKNOWN_REQUEST_NAME;
@@ -3248,6 +3261,23 @@ open_run_request(enum request request)
}
if (format_argv(&argv, req->argv, FALSE)) {
+ if (req->confirm) {
+ char str[SIZEOF_STR];
+ int argc, wpos, spos = 0;
+
+ for (argc = 0; argv[argc]; argc++) {
+ for (wpos = 0; argv[argc][wpos] != '\0'; wpos++)
+ str[spos++] = argv[argc][wpos];
+ str[spos++] = ' ';
+ }
+ str[--spos] = '\0';
+ snprintf(str, sizeof(str), "Run `%s`?", strdup(str));
+ if (!prompt_yesno(str)) {
+ argv_free(argv);
+ free(argv);
+ return;
+ }
+ }
if (req->silent)
io_run_bg(argv);
else
5 tigrc.5.txt
View
@@ -270,8 +270,9 @@ Apart from the action names listed below, all actions starting with a '!' will
be available as an external command. External commands can contain variable
names that will be substituted before the command is run. By default, commands
are run in the foreground with their console output shown, if they should be
-run in the background wit no output prefix the command with '@'. Valid
-variable names are:
+run in the background with no output, prefix the command with '@'. If
+the command is also prefixed with a '?', the user will be prompted for
+confirmation. Valid variable names are:
.Browsing state variables
[frame="none",grid="none",cols="25<m,75<"]
Something went wrong with that request. Please try again.