@ -2,6 +2,7 @@
#include "builtin.h"
#include "builtin.h"
#include "config.h"
#include "config.h"
#include "diff.h"
#include "diff.h"
#include "parse-options.h"
static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
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,
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 patchlen = 0, found_next = 0;
int before = -1, after = -1;
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) &&
if (!skip_prefix(line, "diff-tree ", &p) &&
!skip_prefix(line, "commit ", &p) &&
!skip_prefix(line, "commit ", &p) &&
!skip_prefix(line, "From ", &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;
continue;
}
if (!get_oid_hex(p, next_oid)) {
if (!get_oid_hex(p, next_oid)) {
found_next = 1;
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] == ' ')
if (line[0] == '+' || line[0] == ' ')
after--;
after--;
/* Compute the sha without whitespace */
/* Add line to hash algo (possibly removing whitespace) */
len = remove_space(line);
len = verbatim ? strlen(line) : remove_space(line);
patchlen += len;
patchlen += len;
the_hash_algo->update_fn(&ctx, line, 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;
return patchlen;
}
}
static void generate_id_list(int stable)
static void generate_id_list(int stable, int verbatim)
{
{
struct object_id oid, n, result;
struct object_id oid, n, result;
int patchlen;
int patchlen;
@ -174,21 +178,32 @@ static void generate_id_list(int stable)
oidclr(&oid);
oidclr(&oid);
while (!feof(stdin)) {
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);
flush_current_id(patchlen, &oid, &result);
oidcpy(&oid, &n);
oidcpy(&oid, &n);
}
}
strbuf_release(&line_buf);
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)
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")) {
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;
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 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};
git_config(git_patch_id_config, &stable);
int opts = 0;
struct option builtin_patch_id_options[] = {
/* If nothing is set, default to unstable. */
OPT_CMDMODE(0, "unstable", &opts,
if (stable < 0)
N_("use the unstable patch-id algorithm"), 1),
stable = 0;
OPT_CMDMODE(0, "stable", &opts,
N_("use the stable patch-id algorithm"), 2),
if (argc == 2 && !strcmp(argv[1], "--stable"))
OPT_CMDMODE(0, "verbatim", &opts,
stable = 1;
N_("don't strip whitespace from the patch"), 3),
else if (argc == 2 && !strcmp(argv[1], "--unstable"))
OPT_END()
stable = 0;
};
else if (argc != 1)
usage(patch_id_usage);
git_config(git_patch_id_config, &config);
generate_id_list(stable);
/* verbatim implies stable */
if (config.verbatim)
config.stable = 1;
argc = parse_options(argc, argv, prefix, builtin_patch_id_options,
patch_id_usage, 0);
generate_id_list(opts ? opts > 1 : config.stable,
opts ? opts == 3 : config.verbatim);
return 0;
return 0;
}
}