summaryrefslogtreecommitdiff
path: root/builtin/patch-id.c
diff options
context:
space:
mode:
Diffstat (limited to 'builtin/patch-id.c')
-rw-r--r--builtin/patch-id.c55
1 files changed, 37 insertions, 18 deletions
diff --git a/builtin/patch-id.c b/builtin/patch-id.c
index 3894d2b970..cdef2ec10a 100644
--- a/builtin/patch-id.c
+++ b/builtin/patch-id.c
@@ -1,3 +1,5 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
#include "builtin.h"
#include "config.h"
#include "diff.h"
@@ -5,14 +7,15 @@
#include "hash.h"
#include "hex.h"
#include "parse-options.h"
+#include "setup.h"
-static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
+static void flush_current_id(size_t patchlen, struct object_id *id, struct object_id *result)
{
if (patchlen)
printf("%s %s\n", oid_to_hex(result), oid_to_hex(id));
}
-static int remove_space(char *line)
+static size_t remove_space(char *line)
{
char *src = line;
char *dst = line;
@@ -59,29 +62,30 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
return 1;
}
-static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
- struct strbuf *line_buf, int stable, int verbatim)
+static size_t get_one_patchid(struct object_id *next_oid, struct object_id *result,
+ struct strbuf *line_buf, int stable, int verbatim)
{
- int patchlen = 0, found_next = 0;
+ size_t patchlen = 0;
+ int found_next = 0;
int before = -1, after = -1;
int diff_is_binary = 0;
char pre_oid_str[GIT_MAX_HEXSZ + 1], post_oid_str[GIT_MAX_HEXSZ + 1];
- git_hash_ctx ctx;
+ struct git_hash_ctx ctx;
the_hash_algo->init_fn(&ctx);
- oidclr(result);
+ oidclr(result, the_repository->hash_algo);
while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
char *line = line_buf->buf;
const char *p = line;
- int len;
+ size_t len;
/* Possibly skip over the prefix added by "log" or "format-patch" */
if (!skip_prefix(line, "commit ", &p) &&
!skip_prefix(line, "From ", &p) &&
starts_with(line, "\\ ") && 12 < strlen(line)) {
if (verbatim)
- the_hash_algo->update_fn(&ctx, line, strlen(line));
+ git_hash_update(&ctx, line, strlen(line));
continue;
}
@@ -100,10 +104,10 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
starts_with(line, "Binary files")) {
diff_is_binary = 1;
before = 0;
- the_hash_algo->update_fn(&ctx, pre_oid_str,
- strlen(pre_oid_str));
- the_hash_algo->update_fn(&ctx, post_oid_str,
- strlen(post_oid_str));
+ git_hash_update(&ctx, pre_oid_str,
+ strlen(pre_oid_str));
+ git_hash_update(&ctx, post_oid_str,
+ strlen(post_oid_str));
if (stable)
flush_one_hunk(result, &ctx);
continue;
@@ -161,11 +165,11 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
/* 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);
+ git_hash_update(&ctx, line, len);
}
if (!found_next)
- oidclr(next_oid);
+ oidclr(next_oid, the_repository->hash_algo);
flush_one_hunk(result, &ctx);
@@ -175,10 +179,10 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
static void generate_id_list(int stable, int verbatim)
{
struct object_id oid, n, result;
- int patchlen;
+ size_t patchlen;
struct strbuf line_buf = STRBUF_INIT;
- oidclr(&oid);
+ oidclr(&oid, the_repository->hash_algo);
while (!feof(stdin)) {
patchlen = get_one_patchid(&n, &result, &line_buf, stable, verbatim);
flush_current_id(patchlen, &oid, &result);
@@ -213,7 +217,10 @@ static int git_patch_id_config(const char *var, const char *value,
return git_default_config(var, value, ctx, 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,
+ struct repository *repo UNUSED)
{
/* if nothing is set, default to unstable */
struct patch_id_opts config = {0, 0};
@@ -237,6 +244,18 @@ int cmd_patch_id(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, builtin_patch_id_options,
patch_id_usage, 0);
+ /*
+ * We rely on `the_hash_algo` to compute patch IDs. This is dubious as
+ * it means that the hash algorithm now depends on the object hash of
+ * the repository, even though git-patch-id(1) clearly defines that
+ * patch IDs always use SHA1.
+ *
+ * NEEDSWORK: This hack should be removed in favor of converting
+ * the code that computes patch IDs to always use SHA1.
+ */
+ if (!the_hash_algo)
+ repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
+
generate_id_list(opts ? opts > 1 : config.stable,
opts ? opts == 3 : config.verbatim);
return 0;