redis-cli prompt: show transaction state, and fix db number on aborted EXEC (#6728)

This commit is contained in:
Wen Hui 2020-12-13 07:40:54 -05:00 committed by GitHub
parent ab60dcf564
commit f74c32cad2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -252,6 +252,8 @@ static struct config {
clusterManagerCommand cluster_manager_command; clusterManagerCommand cluster_manager_command;
int no_auth_warning; int no_auth_warning;
int resp3; int resp3;
int in_multi;
int pre_multi_dbnum;
} config; } config;
/* User preferences. */ /* User preferences. */
@ -307,6 +309,10 @@ static void cliRefreshPrompt(void) {
if (config.dbnum != 0) if (config.dbnum != 0)
prompt = sdscatfmt(prompt,"[%i]",config.dbnum); 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. */ /* Copy the prompt in the static buffer. */
prompt = sdscatlen(prompt,"> ",2); prompt = sdscatlen(prompt,"> ",2);
snprintf(config.prompt,sizeof(config.prompt),"%s",prompt); snprintf(config.prompt,sizeof(config.prompt),"%s",prompt);
@ -1396,12 +1402,31 @@ static int cliSendCommand(int argc, char **argv, long repeat) {
return REDIS_ERR; return REDIS_ERR;
} else { } else {
/* Store database number when SELECT was successfully executed. */ /* 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]); config.dbnum = atoi(argv[1]);
cliRefreshPrompt(); cliRefreshPrompt();
} else if (!strcasecmp(command,"auth") && (argc == 2 || argc == 3)) } else if (!strcasecmp(command,"auth") && (argc == 2 || argc == 3)) {
{
cliSelect(); 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 (config.cluster_reissue_command){
@ -8139,6 +8164,7 @@ int main(int argc, char **argv) {
config.verbose = 0; config.verbose = 0;
config.set_errcode = 0; config.set_errcode = 0;
config.no_auth_warning = 0; config.no_auth_warning = 0;
config.in_multi = 0;
config.cluster_manager_command.name = NULL; config.cluster_manager_command.name = NULL;
config.cluster_manager_command.argc = 0; config.cluster_manager_command.argc = 0;
config.cluster_manager_command.argv = NULL; config.cluster_manager_command.argv = NULL;