From dedc0ec5d778214528dae3bddf60d77cfcde4bd4 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 21 Jan 2010 20:31:25 -0800 Subject: make "git patch-id" a built-in Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- Makefile | 2 +- builtin-patch-id.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ builtin.h | 1 + git.c | 1 + patch-id.c | 87 ------------------------------------------------------ 5 files changed, 88 insertions(+), 88 deletions(-) create mode 100644 builtin-patch-id.c delete mode 100644 patch-id.c diff --git a/Makefile b/Makefile index 4558c4627..e1c7ae585 100644 --- a/Makefile +++ b/Makefile @@ -392,7 +392,6 @@ PROGRAMS += git-index-pack$X PROGRAMS += git-merge-index$X PROGRAMS += git-mktag$X PROGRAMS += git-pack-redundant$X -PROGRAMS += git-patch-id$X PROGRAMS += git-shell$X PROGRAMS += git-show-index$X PROGRAMS += git-unpack-file$X @@ -676,6 +675,7 @@ BUILTIN_OBJS += builtin-mv.o BUILTIN_OBJS += builtin-name-rev.o BUILTIN_OBJS += builtin-pack-objects.o BUILTIN_OBJS += builtin-pack-refs.o +BUILTIN_OBJS += builtin-patch-id.o BUILTIN_OBJS += builtin-prune-packed.o BUILTIN_OBJS += builtin-prune.o BUILTIN_OBJS += builtin-push.o diff --git a/builtin-patch-id.c b/builtin-patch-id.c new file mode 100644 index 000000000..af0911e4b --- /dev/null +++ b/builtin-patch-id.c @@ -0,0 +1,85 @@ +#include "cache.h" +#include "exec_cmd.h" + +static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c) +{ + unsigned char result[20]; + char name[50]; + + if (!patchlen) + return; + + git_SHA1_Final(result, c); + memcpy(name, sha1_to_hex(id), 41); + printf("%s %s\n", sha1_to_hex(result), name); + git_SHA1_Init(c); +} + +static int remove_space(char *line) +{ + char *src = line; + char *dst = line; + unsigned char c; + + while ((c = *src++) != '\0') { + if (!isspace(c)) + *dst++ = c; + } + return dst - line; +} + +static void generate_id_list(void) +{ + static unsigned char sha1[20]; + static char line[1000]; + git_SHA_CTX ctx; + int patchlen = 0; + + git_SHA1_Init(&ctx); + while (fgets(line, sizeof(line), stdin) != NULL) { + unsigned char n[20]; + char *p = line; + int len; + + if (!memcmp(line, "diff-tree ", 10)) + p += 10; + else if (!memcmp(line, "commit ", 7)) + p += 7; + + if (!get_sha1_hex(p, n)) { + flush_current_id(patchlen, sha1, &ctx); + hashcpy(sha1, n); + patchlen = 0; + continue; + } + + /* Ignore commit comments */ + if (!patchlen && memcmp(line, "diff ", 5)) + continue; + + /* Ignore git-diff index header */ + if (!memcmp(line, "index ", 6)) + continue; + + /* Ignore line numbers when computing the SHA1 of the patch */ + if (!memcmp(line, "@@ -", 4)) + continue; + + /* Compute the sha without whitespace */ + len = remove_space(line); + patchlen += len; + git_SHA1_Update(&ctx, line, len); + } + flush_current_id(patchlen, sha1, &ctx); +} + +static const char patch_id_usage[] = "git patch-id < patch"; + +int cmd_patch_id(int argc, const char **argv, const char *prefix) +{ + if (argc != 1) + usage(patch_id_usage); + + generate_id_list(); + return 0; +} diff --git a/builtin.h b/builtin.h index 2d95c3b2f..cb8489f97 100644 --- a/builtin.h +++ b/builtin.h @@ -76,6 +76,7 @@ extern int cmd_mktree(int argc, const char **argv, const char *prefix); extern int cmd_mv(int argc, const char **argv, const char *prefix); extern int cmd_name_rev(int argc, const char **argv, const char *prefix); extern int cmd_pack_objects(int argc, const char **argv, const char *prefix); +extern int cmd_patch_id(int argc, const char **argv, const char *prefix); extern int cmd_pickaxe(int argc, const char **argv, const char *prefix); extern int cmd_prune(int argc, const char **argv, const char *prefix); extern int cmd_prune_packed(int argc, const char **argv, const char *prefix); diff --git a/git.c b/git.c index cf7dd5d26..b60999ca9 100644 --- a/git.c +++ b/git.c @@ -341,6 +341,7 @@ static void handle_internal_command(int argc, const char **argv) { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE }, { "name-rev", cmd_name_rev, RUN_SETUP }, { "pack-objects", cmd_pack_objects, RUN_SETUP }, + { "patch-id", cmd_patch_id }, { "peek-remote", cmd_ls_remote }, { "pickaxe", cmd_blame, RUN_SETUP }, { "prune", cmd_prune, RUN_SETUP }, diff --git a/patch-id.c b/patch-id.c deleted file mode 100644 index 0df4cb086..000000000 --- a/patch-id.c +++ /dev/null @@ -1,87 +0,0 @@ -#include "cache.h" -#include "exec_cmd.h" - -static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c) -{ - unsigned char result[20]; - char name[50]; - - if (!patchlen) - return; - - git_SHA1_Final(result, c); - memcpy(name, sha1_to_hex(id), 41); - printf("%s %s\n", sha1_to_hex(result), name); - git_SHA1_Init(c); -} - -static int remove_space(char *line) -{ - char *src = line; - char *dst = line; - unsigned char c; - - while ((c = *src++) != '\0') { - if (!isspace(c)) - *dst++ = c; - } - return dst - line; -} - -static void generate_id_list(void) -{ - static unsigned char sha1[20]; - static char line[1000]; - git_SHA_CTX ctx; - int patchlen = 0; - - git_SHA1_Init(&ctx); - while (fgets(line, sizeof(line), stdin) != NULL) { - unsigned char n[20]; - char *p = line; - int len; - - if (!memcmp(line, "diff-tree ", 10)) - p += 10; - else if (!memcmp(line, "commit ", 7)) - p += 7; - - if (!get_sha1_hex(p, n)) { - flush_current_id(patchlen, sha1, &ctx); - hashcpy(sha1, n); - patchlen = 0; - continue; - } - - /* Ignore commit comments */ - if (!patchlen && memcmp(line, "diff ", 5)) - continue; - - /* Ignore git-diff index header */ - if (!memcmp(line, "index ", 6)) - continue; - - /* Ignore line numbers when computing the SHA1 of the patch */ - if (!memcmp(line, "@@ -", 4)) - continue; - - /* Compute the sha without whitespace */ - len = remove_space(line); - patchlen += len; - git_SHA1_Update(&ctx, line, len); - } - flush_current_id(patchlen, sha1, &ctx); -} - -static const char patch_id_usage[] = "git patch-id < patch"; - -int main(int argc, char **argv) -{ - if (argc != 1) - usage(patch_id_usage); - - git_extract_argv0_path(argv[0]); - - generate_id_list(); - return 0; -} -- cgit v1.2.1