Unverified Commit 23e980e7 authored by Binbin's avatar Binbin Committed by GitHub
Browse files

Move cliVersion to cli_common and add --version support for redis-check-aof (#10856)

Let us see which version of redis this tool is part of.
Similarly to redis-cli, redis-benchmark and redis-check-rdb

redis-rdb-check and redis-aof-check are actually symlinks to redis,
so they will directly use getVersion in server, the format became:
```
{title} v={redis_version} sha={sha}:{dirty} malloc={malloc} bits={bits} build={build}
```

Move cliVersion into cli_common, redis-cli and redis-benchmark will
use it, and the format is not change:
```
{title} {redis_version} (git:{sha})
```
parent 5dc631d8
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "fmacros.h" #include "fmacros.h"
#include "cli_common.h" #include "cli_common.h"
#include "version.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -48,6 +50,9 @@ ...@@ -48,6 +50,9 @@
#define UNUSED(V) ((void) V) #define UNUSED(V) ((void) V)
char *redisGitSHA1(void);
char *redisGitDirty(void);
/* Wrapper around redisSecureConnection to avoid hiredis_ssl dependencies if /* Wrapper around redisSecureConnection to avoid hiredis_ssl dependencies if
* not building with TLS support. * not building with TLS support.
*/ */
...@@ -406,3 +411,16 @@ sds escapeJsonString(sds s, const char *p, size_t len) { ...@@ -406,3 +411,16 @@ sds escapeJsonString(sds s, const char *p, size_t len) {
} }
return sdscatlen(s,"\"",1); return sdscatlen(s,"\"",1);
} }
sds cliVersion(void) {
sds version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);
/* Add git commit and working tree status when available. */
if (strtoll(redisGitSHA1(),NULL,16)) {
version = sdscatprintf(version, " (git:%s", redisGitSHA1());
if (strtoll(redisGitDirty(),NULL,10))
version = sdscatprintf(version, "-dirty");
version = sdscat(version, ")");
}
return version;
}
...@@ -51,4 +51,6 @@ void freeCliConnInfo(cliConnInfo connInfo); ...@@ -51,4 +51,6 @@ void freeCliConnInfo(cliConnInfo connInfo);
sds escapeJsonString(sds s, const char *p, size_t len); sds escapeJsonString(sds s, const char *p, size_t len);
sds cliVersion(void);
#endif /* __CLICOMMON_H */ #endif /* __CLICOMMON_H */
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
*/ */
#include "fmacros.h" #include "fmacros.h"
#include "version.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
...@@ -186,8 +185,6 @@ typedef struct redisConfig { ...@@ -186,8 +185,6 @@ typedef struct redisConfig {
} redisConfig; } redisConfig;
/* Prototypes */ /* Prototypes */
char *redisGitSHA1(void);
char *redisGitDirty(void);
static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask); static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask);
static void createMissingClients(client c); static void createMissingClients(client c);
static benchmarkThread *createBenchmarkThread(int index); static benchmarkThread *createBenchmarkThread(int index);
...@@ -205,20 +202,6 @@ static void updateClusterSlotsConfiguration(void); ...@@ -205,20 +202,6 @@ static void updateClusterSlotsConfiguration(void);
int showThroughput(struct aeEventLoop *eventLoop, long long id, int showThroughput(struct aeEventLoop *eventLoop, long long id,
void *clientData); void *clientData);
static sds benchmarkVersion(void) {
sds version;
version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);
/* Add git commit and working tree status when available */
if (strtoll(redisGitSHA1(),NULL,16)) {
version = sdscatprintf(version, " (git:%s", redisGitSHA1());
if (strtoll(redisGitDirty(),NULL,10))
version = sdscatprintf(version, "-dirty");
version = sdscat(version, ")");
}
return version;
}
/* Dict callbacks */ /* Dict callbacks */
static uint64_t dictSdsHash(const void *key); static uint64_t dictSdsHash(const void *key);
static int dictSdsKeyCompare(dict *d, const void *key1, const void *key2); static int dictSdsKeyCompare(dict *d, const void *key1, const void *key2);
...@@ -1423,7 +1406,7 @@ int parseOptions(int argc, char **argv) { ...@@ -1423,7 +1406,7 @@ int parseOptions(int argc, char **argv) {
if (lastarg) goto invalid; if (lastarg) goto invalid;
config.numclients = atoi(argv[++i]); config.numclients = atoi(argv[++i]);
} else if (!strcmp(argv[i],"-v") || !strcmp(argv[i], "--version")) { } else if (!strcmp(argv[i],"-v") || !strcmp(argv[i], "--version")) {
sds version = benchmarkVersion(); sds version = cliVersion();
printf("redis-benchmark %s\n", version); printf("redis-benchmark %s\n", version);
sdsfree(version); sdsfree(version);
exit(0); exit(0);
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
*/ */
#include "server.h" #include "server.h"
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <regex.h> #include <regex.h>
...@@ -515,6 +516,13 @@ int redis_check_aof_main(int argc, char **argv) { ...@@ -515,6 +516,13 @@ int redis_check_aof_main(int argc, char **argv) {
if (argc < 2) { if (argc < 2) {
goto invalid_args; goto invalid_args;
} else if (argc == 2) { } else if (argc == 2) {
if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
sds version = getVersion();
printf("redis-check-aof %s\n", version);
sdsfree(version);
exit(0);
}
filepath = argv[1]; filepath = argv[1];
} else if (argc == 3) { } else if (argc == 3) {
if (!strcmp(argv[1], "--fix")) { if (!strcmp(argv[1], "--fix")) {
......
...@@ -394,20 +394,6 @@ err: ...@@ -394,20 +394,6 @@ err:
return 1; return 1;
} }
static sds checkRdbVersion(void) {
sds version;
version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);
/* Add git commit and working tree status when available */
if (strtoll(redisGitSHA1(),NULL,16)) {
version = sdscatprintf(version, " (git:%s", redisGitSHA1());
if (strtoll(redisGitDirty(),NULL,10))
version = sdscatprintf(version, "-dirty");
version = sdscat(version, ")");
}
return version;
}
/* RDB check main: called form server.c when Redis is executed with the /* RDB check main: called form server.c when Redis is executed with the
* redis-check-rdb alias, on during RDB loading errors. * redis-check-rdb alias, on during RDB loading errors.
* *
...@@ -427,7 +413,7 @@ int redis_check_rdb_main(int argc, char **argv, FILE *fp) { ...@@ -427,7 +413,7 @@ int redis_check_rdb_main(int argc, char **argv, FILE *fp) {
fprintf(stderr, "Usage: %s <rdb-file-name>\n", argv[0]); fprintf(stderr, "Usage: %s <rdb-file-name>\n", argv[0]);
exit(1); exit(1);
} else if (!strcmp(argv[1],"-v") || !strcmp(argv[1], "--version")) { } else if (!strcmp(argv[1],"-v") || !strcmp(argv[1], "--version")) {
sds version = checkRdbVersion(); sds version = getVersion();
printf("redis-check-rdb %s\n", version); printf("redis-check-rdb %s\n", version);
sdsfree(version); sdsfree(version);
exit(0); exit(0);
......
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
*/ */
#include "fmacros.h" #include "fmacros.h"
#include "version.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
...@@ -64,7 +63,6 @@ ...@@ -64,7 +63,6 @@
#include "connection.h" #include "connection.h"
#include "cli_common.h" #include "cli_common.h"
#include "mt19937-64.h" #include "mt19937-64.h"
#include "cli_commands.h" #include "cli_commands.h"
#define UNUSED(V) ((void) V) #define UNUSED(V) ((void) V)
...@@ -287,8 +285,6 @@ static struct pref { ...@@ -287,8 +285,6 @@ static struct pref {
static volatile sig_atomic_t force_cancel_loop = 0; static volatile sig_atomic_t force_cancel_loop = 0;
static void usage(int err); static void usage(int err);
static void slaveMode(int send_sync); static void slaveMode(int send_sync);
char *redisGitSHA1(void);
char *redisGitDirty(void);
static int cliConnect(int flags); static int cliConnect(int flags);
static char *getInfoField(char *info, char *field); static char *getInfoField(char *info, char *field);
...@@ -424,20 +420,6 @@ typedef struct { ...@@ -424,20 +420,6 @@ typedef struct {
static helpEntry *helpEntries = NULL; static helpEntry *helpEntries = NULL;
static int helpEntriesLen = 0; static int helpEntriesLen = 0;
static sds cliVersion(void) {
sds version;
version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);
/* Add git commit and working tree status when available */
if (strtoll(redisGitSHA1(),NULL,16)) {
version = sdscatprintf(version, " (git:%s", redisGitSHA1());
if (strtoll(redisGitDirty(),NULL,10))
version = sdscatprintf(version, "-dirty");
version = sdscat(version, ")");
}
return version;
}
/* For backwards compatibility with pre-7.0 servers. /* For backwards compatibility with pre-7.0 servers.
* cliLegacyInitHelp() sets up the helpEntries array with the command and group * cliLegacyInitHelp() sets up the helpEntries array with the command and group
* names from the commands.c file. However the Redis instance we are connecting * names from the commands.c file. However the Redis instance we are connecting
......
...@@ -6250,15 +6250,16 @@ void daemonize(void) { ...@@ -6250,15 +6250,16 @@ void daemonize(void) {
} }
} }
void version(void) { sds getVersion(void) {
printf("Redis server v=%s sha=%s:%d malloc=%s bits=%d build=%llx\n", sds version = sdscatprintf(sdsempty(),
"v=%s sha=%s:%d malloc=%s bits=%d build=%llx",
REDIS_VERSION, REDIS_VERSION,
redisGitSHA1(), redisGitSHA1(),
atoi(redisGitDirty()) > 0, atoi(redisGitDirty()) > 0,
ZMALLOC_LIB, ZMALLOC_LIB,
sizeof(long) == 4 ? 32 : 64, sizeof(long) == 4 ? 32 : 64,
(unsigned long long) redisBuildId()); (unsigned long long) redisBuildId());
exit(0); return version;
} }
void usage(void) { void usage(void) {
...@@ -6992,7 +6993,13 @@ int main(int argc, char **argv) { ...@@ -6992,7 +6993,13 @@ int main(int argc, char **argv) {
/* Handle special options --help and --version */ /* Handle special options --help and --version */
if (strcmp(argv[1], "-v") == 0 || if (strcmp(argv[1], "-v") == 0 ||
strcmp(argv[1], "--version") == 0) version(); strcmp(argv[1], "--version") == 0)
{
sds version = getVersion();
printf("Redis server %s\n", version);
sdsfree(version);
exit(0);
}
if (strcmp(argv[1], "--help") == 0 || if (strcmp(argv[1], "--help") == 0 ||
strcmp(argv[1], "-h") == 0) usage(); strcmp(argv[1], "-h") == 0) usage();
if (strcmp(argv[1], "--test-memory") == 0) { if (strcmp(argv[1], "--test-memory") == 0) {
......
...@@ -3789,6 +3789,7 @@ void killIOThreads(void); ...@@ -3789,6 +3789,7 @@ void killIOThreads(void);
void killThreads(void); void killThreads(void);
void makeThreadKillable(void); void makeThreadKillable(void);
void swapMainDbWithTempDb(redisDb *tempDb); void swapMainDbWithTempDb(redisDb *tempDb);
sds getVersion(void);
/* Use macro for checking log level to avoid evaluating arguments in cases log /* Use macro for checking log level to avoid evaluating arguments in cases log
* should be ignored due to low level. */ * should be ignored due to low level. */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment