builtin: patch-id: add --verbatim as a command mode
There are situations where the user might not want the default setting where patch-id strips all whitespace. They might be working in a language where white space is syntactically important, or they might have CI testing that enforces strict whitespace linting. In these cases, a whitespace change would result in the patch fundamentally changing, and thus deserving of a different id. Add a new mode that is exclusive of --stable and --unstable called --verbatim. It also corresponds to the config patchid.verbatim = true. In this mode, the stable algorithm is used and whitespace is not stripped from the patch text. Users of --unstable mainly care about compatibility with old git versions, which unstripping the whitespace would break. Thus there isn't a usecase for the combination of --verbatim and --unstable, and we don't expose this so as to not add maintainence burden. Signed-off-by: Jerry Zhang <jerry@skydio.com> fixes https://github.com/Skydio/revup/issues/2 Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
93105aba6c
commit
2871f4d447
@ -2,6 +2,7 @@
|
||||
#include "builtin.h"
|
||||
#include "config.h"
|
||||
#include "diff.h"
|
||||
#include "parse-options.h"
|
||||
|
||||
static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
|
||||
{
|
||||
@ -57,7 +58,7 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
|
||||
}
|
||||
|
||||
static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
|
||||
struct strbuf *line_buf, int stable)
|
||||
struct strbuf *line_buf, int stable, int verbatim)
|
||||
{
|
||||
int patchlen = 0, found_next = 0;
|
||||
int before = -1, after = -1;
|
||||
@ -76,8 +77,11 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
|
||||
if (!skip_prefix(line, "diff-tree ", &p) &&
|
||||
!skip_prefix(line, "commit ", &p) &&
|
||||
!skip_prefix(line, "From ", &p) &&
|
||||
starts_with(line, "\\ ") && 12 < strlen(line))
|
||||
starts_with(line, "\\ ") && 12 < strlen(line)) {
|
||||
if (verbatim)
|
||||
the_hash_algo->update_fn(&ctx, line, strlen(line));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!get_oid_hex(p, next_oid)) {
|
||||
found_next = 1;
|
||||
@ -152,8 +156,8 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
|
||||
if (line[0] == '+' || line[0] == ' ')
|
||||
after--;
|
||||
|
||||
/* Compute the sha without whitespace */
|
||||
len = remove_space(line);
|
||||
/* Add line to hash algo (possibly removing whitespace) */
|
||||
len = verbatim ? strlen(line) : remove_space(line);
|
||||
patchlen += len;
|
||||
the_hash_algo->update_fn(&ctx, line, len);
|
||||
}
|
||||
@ -166,7 +170,7 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
|
||||
return patchlen;
|
||||
}
|
||||
|
||||
static void generate_id_list(int stable)
|
||||
static void generate_id_list(int stable, int verbatim)
|
||||
{
|
||||
struct object_id oid, n, result;
|
||||
int patchlen;
|
||||
@ -174,21 +178,32 @@ static void generate_id_list(int stable)
|
||||
|
||||
oidclr(&oid);
|
||||
while (!feof(stdin)) {
|
||||
patchlen = get_one_patchid(&n, &result, &line_buf, stable);
|
||||
patchlen = get_one_patchid(&n, &result, &line_buf, stable, verbatim);
|
||||
flush_current_id(patchlen, &oid, &result);
|
||||
oidcpy(&oid, &n);
|
||||
}
|
||||
strbuf_release(&line_buf);
|
||||
}
|
||||
|
||||
static const char patch_id_usage[] = "git patch-id [--stable | --unstable]";
|
||||
static const char *const patch_id_usage[] = {
|
||||
N_("git patch-id [--stable | --unstable | --verbatim]"), NULL
|
||||
};
|
||||
|
||||
struct patch_id_opts {
|
||||
int stable;
|
||||
int verbatim;
|
||||
};
|
||||
|
||||
static int git_patch_id_config(const char *var, const char *value, void *cb)
|
||||
{
|
||||
int *stable = cb;
|
||||
struct patch_id_opts *opts = cb;
|
||||
|
||||
if (!strcmp(var, "patchid.stable")) {
|
||||
*stable = git_config_bool(var, value);
|
||||
opts->stable = git_config_bool(var, value);
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(var, "patchid.verbatim")) {
|
||||
opts->verbatim = git_config_bool(var, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -197,21 +212,29 @@ static int git_patch_id_config(const char *var, const char *value, void *cb)
|
||||
|
||||
int cmd_patch_id(int argc, const char **argv, const char *prefix)
|
||||
{
|
||||
int stable = -1;
|
||||
/* if nothing is set, default to unstable */
|
||||
struct patch_id_opts config = {0, 0};
|
||||
int opts = 0;
|
||||
struct option builtin_patch_id_options[] = {
|
||||
OPT_CMDMODE(0, "unstable", &opts,
|
||||
N_("use the unstable patch-id algorithm"), 1),
|
||||
OPT_CMDMODE(0, "stable", &opts,
|
||||
N_("use the stable patch-id algorithm"), 2),
|
||||
OPT_CMDMODE(0, "verbatim", &opts,
|
||||
N_("don't strip whitespace from the patch"), 3),
|
||||
OPT_END()
|
||||
};
|
||||
|
||||
git_config(git_patch_id_config, &stable);
|
||||
git_config(git_patch_id_config, &config);
|
||||
|
||||
/* If nothing is set, default to unstable. */
|
||||
if (stable < 0)
|
||||
stable = 0;
|
||||
/* verbatim implies stable */
|
||||
if (config.verbatim)
|
||||
config.stable = 1;
|
||||
|
||||
if (argc == 2 && !strcmp(argv[1], "--stable"))
|
||||
stable = 1;
|
||||
else if (argc == 2 && !strcmp(argv[1], "--unstable"))
|
||||
stable = 0;
|
||||
else if (argc != 1)
|
||||
usage(patch_id_usage);
|
||||
argc = parse_options(argc, argv, prefix, builtin_patch_id_options,
|
||||
patch_id_usage, 0);
|
||||
|
||||
generate_id_list(stable);
|
||||
generate_id_list(opts ? opts > 1 : config.stable,
|
||||
opts ? opts == 3 : config.verbatim);
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user