diff --git a/src/redis-cli.c b/src/redis-cli.c index f5271b2a9..4f5b0fe03 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -252,6 +252,8 @@ static struct config { clusterManagerCommand cluster_manager_command; int no_auth_warning; int resp3; + int in_multi; + int pre_multi_dbnum; } config; /* User preferences. */ @@ -307,6 +309,10 @@ static void cliRefreshPrompt(void) { if (config.dbnum != 0) prompt = sdscatfmt(prompt,"[%i]",config.dbnum); + /* Add TX if in transaction state*/ + if (config.in_multi) + prompt = sdscatlen(prompt,"(TX)",4); + /* Copy the prompt in the static buffer. */ prompt = sdscatlen(prompt,"> ",2); snprintf(config.prompt,sizeof(config.prompt),"%s",prompt); @@ -1396,13 +1402,32 @@ static int cliSendCommand(int argc, char **argv, long repeat) { return REDIS_ERR; } else { /* Store database number when SELECT was successfully executed. */ - if (!strcasecmp(command,"select") && argc == 2 && config.last_cmd_type != REDIS_REPLY_ERROR) { + if (!strcasecmp(command,"select") && argc == 2 && + config.last_cmd_type != REDIS_REPLY_ERROR) + { config.dbnum = atoi(argv[1]); cliRefreshPrompt(); - } else if (!strcasecmp(command,"auth") && (argc == 2 || argc == 3)) - { + } else if (!strcasecmp(command,"auth") && (argc == 2 || argc == 3)) { cliSelect(); - } + } else if (!strcasecmp(command,"multi") && argc == 1 && + config.last_cmd_type != REDIS_REPLY_ERROR) + { + config.in_multi = 1; + config.pre_multi_dbnum = config.dbnum; + cliRefreshPrompt(); + } else if (!strcasecmp(command,"exec") && argc == 1 && config.in_multi) { + config.in_multi = 0; + if (config.last_cmd_type == REDIS_REPLY_ERROR) { + config.dbnum = config.pre_multi_dbnum; + } + cliRefreshPrompt(); + } else if (!strcasecmp(command,"discard") && argc == 1 && + config.last_cmd_type != REDIS_REPLY_ERROR) + { + config.in_multi = 0; + config.dbnum = config.pre_multi_dbnum; + cliRefreshPrompt(); + } } if (config.cluster_reissue_command){ /* If we need to reissue the command, break to prevent a @@ -8139,6 +8164,7 @@ int main(int argc, char **argv) { config.verbose = 0; config.set_errcode = 0; config.no_auth_warning = 0; + config.in_multi = 0; config.cluster_manager_command.name = NULL; config.cluster_manager_command.argc = 0; config.cluster_manager_command.argv = NULL;