@matheusbsbs / matheus-bagliano / commits / dcb3450fd8
Fork 0

sha1_to_hex() usage cleanup

Somebody on the #git channel complained that the sha1_to_hex() thing uses a static buffer which caused an error message to show the same hex output twice instead of showing two different ones. That's pretty easily rectified by making it uses a simple LRU of a few buffers, which also allows some other users (that were aware of the buffer re-use) to be written in a more straightforward manner. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 3, 2006 at 17:21 UTC dcb3450fd8785d76dd3d25aa49be66190aa5e7f3
3 files changed +7 -10
diff.c
+2 -4
@@ -1018,14 +1018,12 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
1018 }
1019
1020 if (memcmp(one->sha1, two->sha1, 20)) {
1021 - char one_sha1[41];
1021 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1023 - memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
1022
1023 len += snprintf(msg + len, sizeof(msg) - len,
1024 "index %.*s..%.*s",
1027 - abbrev, one_sha1, abbrev,
1028 - sha1_to_hex(two->sha1));
1025 + abbrev, sha1_to_hex(one->sha1),
1026 + abbrev, sha1_to_hex(two->sha1));
1027 if (one->mode == two->mode)
1028 len += snprintf(msg + len, sizeof(msg) - len,
1029 " %06o", one->mode);
merge-tree.c
+2 -4
@@ -24,16 +24,14 @@ static const char *sha1_to_hex_zero(const unsigned char *sha1)
24
25 static void resolve(const char *base, struct name_entry *branch1, struct name_entry *result)
26 {
27 - char branch1_sha1[50];
28 -
27 /* If it's already branch1, don't bother showing it */
28 if (!branch1)
29 return;
32 - memcpy(branch1_sha1, sha1_to_hex_zero(branch1->sha1), 41);
30
31 printf("0 %06o->%06o %s->%s %s%s\n",
32 branch1->mode, result->mode,
36 - branch1_sha1, sha1_to_hex_zero(result->sha1),
33 + sha1_to_hex_zero(branch1->sha1),
34 + sha1_to_hex_zero(result->sha1),
35 base, result->path);
36 }
37
sha1_file.c
+3 -2
@@ -108,9 +108,10 @@ int safe_create_leading_directories(char *path)
108
109 char * sha1_to_hex(const unsigned char *sha1)
110 {
111 - static char buffer[50];
111 + static int bufno;
112 + static char hexbuffer[4][50];
113 static const char hex[] = "0123456789abcdef";
113 - char *buf = buffer;
114 + char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
115 int i;
116
117 for (i = 0; i < 20; i++) {