Skip to content

Commit f976722

Browse files
author
Linus Torvalds
committed
Add "git-patch-id" program to generate patch ID's.
A "patch ID" is nothing but a SHA1 of the diff associated with a patch, with whitespace and line numbers ignored. As such, it's "reasonably stable", but at the same time also reasonably unique, ie two patches that have the same "patch ID" are almost guaranteed to be the same thing. IOW, you can use this thing to look for likely duplicate commits.
1 parent 1809266 commit f976722

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ PROG= git-update-cache git-diff-files git-init-db git-write-tree \
3535
git-http-pull git-ssh-push git-ssh-pull git-rev-list git-mktag \
3636
git-diff-helper git-tar-tree git-local-pull git-write-blob \
3737
git-get-tar-commit-id git-mkdelta git-apply git-stripspace \
38-
git-cvs2git git-diff-stages git-rev-parse
38+
git-cvs2git git-diff-stages git-rev-parse git-patch-id
3939

4040
all: $(PROG)
4141

@@ -121,6 +121,7 @@ git-stripspace: stripspace.c
121121
git-cvs2git: cvs2git.c
122122
git-diff-stages: diff-stages.c
123123
git-rev-parse: rev-parse.c
124+
git-patch-id: patch-id.c
124125

125126
git-http-pull: LIBS += -lcurl
126127
git-rev-list: LIBS += -lssl

patch-id.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <ctype.h>
2+
#include "cache.h"
3+
4+
static void flush_current_id(int patchlen, unsigned char *id, SHA_CTX *c)
5+
{
6+
unsigned char result[20];
7+
char name[50];
8+
9+
if (!patchlen)
10+
return;
11+
12+
SHA1_Final(result, c);
13+
memcpy(name, sha1_to_hex(id), 41);
14+
printf("%s %s\n", sha1_to_hex(result), name);
15+
SHA1_Init(c);
16+
}
17+
18+
static int remove_space(char *line)
19+
{
20+
char *src = line;
21+
char *dst = line;
22+
unsigned char c;
23+
24+
while ((c = *src++) != '\0') {
25+
if (!isspace(c))
26+
*dst++ = c;
27+
}
28+
return dst - line;
29+
}
30+
31+
static void generate_id_list(void)
32+
{
33+
static unsigned char sha1[20];
34+
static char line[1000];
35+
SHA_CTX ctx;
36+
int patchlen = 0;
37+
38+
SHA1_Init(&ctx);
39+
while (fgets(line, sizeof(line), stdin) != NULL) {
40+
unsigned char n[20];
41+
char *p = line;
42+
int len;
43+
44+
if (!memcmp(line, "diff-tree ", 10))
45+
p += 10;
46+
47+
if (!get_sha1_hex(p, n)) {
48+
flush_current_id(patchlen, sha1, &ctx);
49+
memcpy(sha1, n, 20);
50+
patchlen = 0;
51+
continue;
52+
}
53+
54+
/* Ignore commit comments */
55+
if (!patchlen && memcmp(line, "diff ", 5))
56+
continue;
57+
58+
/* Ignore line numbers when computing the SHA1 of the patch */
59+
if (!memcmp(line, "@@ -", 4))
60+
continue;
61+
62+
/* Compute the sha without whitespace */
63+
len = remove_space(line);
64+
patchlen += len;
65+
SHA1_Update(&ctx, line, len);
66+
}
67+
flush_current_id(patchlen, sha1, &ctx);
68+
}
69+
70+
static const char patch_id_usage[] = "usage: git-patch-id < patch";
71+
72+
int main(int argc, char **argv)
73+
{
74+
if (argc != 1)
75+
usage(patch_id_usage);
76+
77+
generate_id_list();
78+
return 0;
79+
}

0 commit comments

Comments
 (0)