Skip to content

Commit 00cbdec

Browse files
author
Junio C Hamano
committed
Merge branch 'pe/cleanup' into next
* pe/cleanup: Replace xmalloc+memset(0) with xcalloc. Use blob_, commit_, tag_, and tree_type throughout.
2 parents b411fda + 90321c1 commit 00cbdec

30 files changed

+120
-106
lines changed

apply.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <fnmatch.h>
1010
#include "cache.h"
1111
#include "quote.h"
12+
#include "blob.h"
1213

1314
// --check turns on checking that the working tree matches the
1415
// files that are being modified, but doesn't apply the patch
@@ -924,8 +925,7 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
924925
struct fragment *fragment;
925926
int len;
926927

927-
fragment = xmalloc(sizeof(*fragment));
928-
memset(fragment, 0, sizeof(*fragment));
928+
fragment = xcalloc(1, sizeof(*fragment));
929929
len = parse_fragment(line, size, patch, fragment);
930930
if (len <= 0)
931931
die("corrupt patch at line %d", linenr);
@@ -1296,7 +1296,7 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
12961296
* applies to.
12971297
*/
12981298
write_sha1_file_prepare(desc->buffer, desc->size,
1299-
"blob", sha1, hdr, &hdrlen);
1299+
blob_type, sha1, hdr, &hdrlen);
13001300
if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
13011301
return error("the patch applies to '%s' (%s), "
13021302
"which does not match the "
@@ -1651,15 +1651,14 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
16511651
if (!write_index)
16521652
return;
16531653

1654-
ce = xmalloc(ce_size);
1655-
memset(ce, 0, ce_size);
1654+
ce = xcalloc(1, ce_size);
16561655
memcpy(ce->name, path, namelen);
16571656
ce->ce_mode = create_ce_mode(mode);
16581657
ce->ce_flags = htons(namelen);
16591658
if (lstat(path, &st) < 0)
16601659
die("unable to stat newly created file %s", path);
16611660
fill_stat_cache_info(ce, &st);
1662-
if (write_sha1_file(buf, size, "blob", ce->sha1) < 0)
1661+
if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
16631662
die("unable to create backing store for newly created file %s", path);
16641663
if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
16651664
die("unable to add cache entry for %s", path);
@@ -1808,8 +1807,7 @@ static int apply_patch(int fd, const char *filename)
18081807
struct patch *patch;
18091808
int nr;
18101809

1811-
patch = xmalloc(sizeof(*patch));
1812-
memset(patch, 0, sizeof(*patch));
1810+
patch = xcalloc(1, sizeof(*patch));
18131811
nr = parse_chunk(buffer + offset, size, patch);
18141812
if (nr < 0)
18151813
break;

blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static void get_blob(struct commit *commit)
229229

230230
info->buf = read_sha1_file(info->sha1, type, &info->size);
231231

232-
assert(!strcmp(type, "blob"));
232+
assert(!strcmp(type, blob_type));
233233
}
234234

235235
/* For debugging only */

blob.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ struct blob *lookup_blob(const unsigned char *sha1)
88
{
99
struct object *obj = lookup_object(sha1);
1010
if (!obj) {
11-
struct blob *ret = xmalloc(sizeof(struct blob));
12-
memset(ret, 0, sizeof(struct blob));
11+
struct blob *ret = xcalloc(1, sizeof(struct blob));
1312
created_object(sha1, &ret->object);
1413
ret->object.type = blob_type;
1514
return ret;

cat-file.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
#include "cache.h"
77
#include "exec_cmd.h"
8+
#include "tag.h"
9+
#include "tree.h"
810

911
static void flush_buffer(const char *buf, unsigned long size)
1012
{
@@ -136,13 +138,13 @@ int main(int argc, char **argv)
136138
die("Not a valid object name %s", argv[2]);
137139

138140
/* custom pretty-print here */
139-
if (!strcmp(type, "tree"))
141+
if (!strcmp(type, tree_type))
140142
return execl_git_cmd("ls-tree", argv[2], NULL);
141143

142144
buf = read_sha1_file(sha1, type, &size);
143145
if (!buf)
144146
die("Cannot read object %s", argv[2]);
145-
if (!strcmp(type, "tag"))
147+
if (!strcmp(type, tag_type))
146148
return pprint_tag(sha1, buf, size);
147149

148150
/* otherwise just spit out the data */

combine-diff.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "commit.h"
3+
#include "blob.h"
34
#include "diff.h"
45
#include "diffcore.h"
56
#include "quote.h"
@@ -104,7 +105,7 @@ static char *grab_blob(const unsigned char *sha1, unsigned long *size)
104105
return xcalloc(1, 1);
105106
}
106107
blob = read_sha1_file(sha1, type, size);
107-
if (strcmp(type, "blob"))
108+
if (strcmp(type, blob_type))
108109
die("object '%s' is not a blob!", sha1_to_hex(sha1));
109110
return blob;
110111
}

commit-tree.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Copyright (C) Linus Torvalds, 2005
55
*/
66
#include "cache.h"
7+
#include "commit.h"
8+
#include "tree.h"
79

810
#define BLOCKING (1ul << 14)
911

@@ -93,13 +95,13 @@ int main(int argc, char **argv)
9395
if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0)
9496
usage(commit_tree_usage);
9597

96-
check_valid(tree_sha1, "tree");
98+
check_valid(tree_sha1, tree_type);
9799
for (i = 2; i < argc; i += 2) {
98100
char *a, *b;
99101
a = argv[i]; b = argv[i+1];
100102
if (!b || strcmp(a, "-p") || get_sha1(b, parent_sha1[parents]))
101103
usage(commit_tree_usage);
102-
check_valid(parent_sha1[parents], "commit");
104+
check_valid(parent_sha1[parents], commit_type);
103105
if (new_parent(parents))
104106
parents++;
105107
}
@@ -125,7 +127,7 @@ int main(int argc, char **argv)
125127
while (fgets(comment, sizeof(comment), stdin) != NULL)
126128
add_buffer(&buffer, &size, "%s", comment);
127129

128-
if (!write_sha1_file(buffer, size, "commit", commit_sha1)) {
130+
if (!write_sha1_file(buffer, size, commit_type, commit_sha1)) {
129131
printf("%s\n", sha1_to_hex(commit_sha1));
130132
return 0;
131133
}

commit.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ struct commit *lookup_commit(const unsigned char *sha1)
7373
{
7474
struct object *obj = lookup_object(sha1);
7575
if (!obj) {
76-
struct commit *ret = xmalloc(sizeof(struct commit));
77-
memset(ret, 0, sizeof(struct commit));
76+
struct commit *ret = xcalloc(1, sizeof(struct commit));
7877
created_object(sha1, &ret->object);
7978
ret->object.type = commit_type;
8079
return ret;

convert-objects.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
33
#include <time.h>
44
#include "cache.h"
5+
#include "blob.h"
6+
#include "commit.h"
7+
#include "tree.h"
58

69
struct entry {
710
unsigned char old_sha1[20];
@@ -18,8 +21,7 @@ static struct entry * convert_entry(unsigned char *sha1);
1821

1922
static struct entry *insert_new(unsigned char *sha1, int pos)
2023
{
21-
struct entry *new = xmalloc(sizeof(struct entry));
22-
memset(new, 0, sizeof(*new));
24+
struct entry *new = xcalloc(1, sizeof(struct entry));
2325
memcpy(new->old_sha1, sha1, 20);
2426
memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
2527
convert[pos] = new;
@@ -122,7 +124,7 @@ static int write_subdirectory(void *buffer, unsigned long size, const char *base
122124
buffer += len;
123125
}
124126

125-
write_sha1_file(new, newlen, "tree", result_sha1);
127+
write_sha1_file(new, newlen, tree_type, result_sha1);
126128
free(new);
127129
return used;
128130
}
@@ -262,8 +264,8 @@ static void convert_date(void *buffer, unsigned long size, unsigned char *result
262264
memcpy(new + newlen, buffer, size);
263265
newlen += size;
264266

265-
write_sha1_file(new, newlen, "commit", result_sha1);
266-
free(new);
267+
write_sha1_file(new, newlen, commit_type, result_sha1);
268+
free(new);
267269
}
268270

269271
static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1)
@@ -297,12 +299,12 @@ static struct entry * convert_entry(unsigned char *sha1)
297299

298300
buffer = xmalloc(size);
299301
memcpy(buffer, data, size);
300-
301-
if (!strcmp(type, "blob")) {
302-
write_sha1_file(buffer, size, "blob", entry->new_sha1);
303-
} else if (!strcmp(type, "tree"))
302+
303+
if (!strcmp(type, blob_type)) {
304+
write_sha1_file(buffer, size, blob_type, entry->new_sha1);
305+
} else if (!strcmp(type, tree_type))
304306
convert_tree(buffer, size, entry->new_sha1);
305-
else if (!strcmp(type, "commit"))
307+
else if (!strcmp(type, commit_type))
306308
convert_commit(buffer, size, entry->new_sha1);
307309
else
308310
die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));

diff-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static int diff_root_tree(const unsigned char *new, const char *base)
5252
void *tree;
5353
struct tree_desc empty, real;
5454

55-
tree = read_object_with_reference(new, "tree", &real.size, NULL);
55+
tree = read_object_with_reference(new, tree_type, &real.size, NULL);
5656
if (!tree)
5757
die("unable to read root tree (%s)", sha1_to_hex(new));
5858
real.buf = tree;

entry.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <sys/types.h>
22
#include <dirent.h>
33
#include "cache.h"
4+
#include "blob.h"
45

56
static void create_directories(const char *path, struct checkout *state)
67
{
@@ -72,7 +73,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat
7273
char type[20];
7374

7475
new = read_sha1_file(ce->sha1, type, &size);
75-
if (!new || strcmp(type, "blob")) {
76+
if (!new || strcmp(type, blob_type)) {
7677
if (new)
7778
free(new);
7879
return error("git-checkout-index: unable to read sha1 file of %s (%s)",

0 commit comments

Comments
 (0)