Skip to content

Commit

Permalink
Merge branch 'pe/cleanup'
Browse files Browse the repository at this point in the history
* pe/cleanup:
  Replace xmalloc+memset(0) with xcalloc.
  Use blob_, commit_, tag_, and tree_type throughout.
  • Loading branch information
Junio C Hamano committed Apr 4, 2006
2 parents 4c61b7d + 90321c1 commit 810e152
Show file tree
Hide file tree
Showing 30 changed files with 120 additions and 106 deletions.
14 changes: 6 additions & 8 deletions apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <fnmatch.h>
#include "cache.h"
#include "quote.h"
#include "blob.h"

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

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

ce = xmalloc(ce_size);
memset(ce, 0, ce_size);
ce = xcalloc(1, ce_size);
memcpy(ce->name, path, namelen);
ce->ce_mode = create_ce_mode(mode);
ce->ce_flags = htons(namelen);
if (lstat(path, &st) < 0)
die("unable to stat newly created file %s", path);
fill_stat_cache_info(ce, &st);
if (write_sha1_file(buf, size, "blob", ce->sha1) < 0)
if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
die("unable to create backing store for newly created file %s", path);
if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
die("unable to add cache entry for %s", path);
Expand Down Expand Up @@ -1808,8 +1807,7 @@ static int apply_patch(int fd, const char *filename)
struct patch *patch;
int nr;

patch = xmalloc(sizeof(*patch));
memset(patch, 0, sizeof(*patch));
patch = xcalloc(1, sizeof(*patch));
nr = parse_chunk(buffer + offset, size, patch);
if (nr < 0)
break;
Expand Down
2 changes: 1 addition & 1 deletion blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ static void get_blob(struct commit *commit)

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

assert(!strcmp(type, "blob"));
assert(!strcmp(type, blob_type));
}

/* For debugging only */
Expand Down
3 changes: 1 addition & 2 deletions blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ struct blob *lookup_blob(const unsigned char *sha1)
{
struct object *obj = lookup_object(sha1);
if (!obj) {
struct blob *ret = xmalloc(sizeof(struct blob));
memset(ret, 0, sizeof(struct blob));
struct blob *ret = xcalloc(1, sizeof(struct blob));
created_object(sha1, &ret->object);
ret->object.type = blob_type;
return ret;
Expand Down
6 changes: 4 additions & 2 deletions cat-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
#include "cache.h"
#include "exec_cmd.h"
#include "tag.h"
#include "tree.h"

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

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

buf = read_sha1_file(sha1, type, &size);
if (!buf)
die("Cannot read object %s", argv[2]);
if (!strcmp(type, "tag"))
if (!strcmp(type, tag_type))
return pprint_tag(sha1, buf, size);

/* otherwise just spit out the data */
Expand Down
3 changes: 2 additions & 1 deletion combine-diff.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "cache.h"
#include "commit.h"
#include "blob.h"
#include "diff.h"
#include "diffcore.h"
#include "quote.h"
Expand Down Expand Up @@ -104,7 +105,7 @@ static char *grab_blob(const unsigned char *sha1, unsigned long *size)
return xcalloc(1, 1);
}
blob = read_sha1_file(sha1, type, size);
if (strcmp(type, "blob"))
if (strcmp(type, blob_type))
die("object '%s' is not a blob!", sha1_to_hex(sha1));
return blob;
}
Expand Down
8 changes: 5 additions & 3 deletions commit-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Copyright (C) Linus Torvalds, 2005
*/
#include "cache.h"
#include "commit.h"
#include "tree.h"

#define BLOCKING (1ul << 14)

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

check_valid(tree_sha1, "tree");
check_valid(tree_sha1, tree_type);
for (i = 2; i < argc; i += 2) {
char *a, *b;
a = argv[i]; b = argv[i+1];
if (!b || strcmp(a, "-p") || get_sha1(b, parent_sha1[parents]))
usage(commit_tree_usage);
check_valid(parent_sha1[parents], "commit");
check_valid(parent_sha1[parents], commit_type);
if (new_parent(parents))
parents++;
}
Expand All @@ -125,7 +127,7 @@ int main(int argc, char **argv)
while (fgets(comment, sizeof(comment), stdin) != NULL)
add_buffer(&buffer, &size, "%s", comment);

if (!write_sha1_file(buffer, size, "commit", commit_sha1)) {
if (!write_sha1_file(buffer, size, commit_type, commit_sha1)) {
printf("%s\n", sha1_to_hex(commit_sha1));
return 0;
}
Expand Down
3 changes: 1 addition & 2 deletions commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ struct commit *lookup_commit(const unsigned char *sha1)
{
struct object *obj = lookup_object(sha1);
if (!obj) {
struct commit *ret = xmalloc(sizeof(struct commit));
memset(ret, 0, sizeof(struct commit));
struct commit *ret = xcalloc(1, sizeof(struct commit));
created_object(sha1, &ret->object);
ret->object.type = commit_type;
return ret;
Expand Down
22 changes: 12 additions & 10 deletions convert-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
#include <time.h>
#include "cache.h"
#include "blob.h"
#include "commit.h"
#include "tree.h"

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

static struct entry *insert_new(unsigned char *sha1, int pos)
{
struct entry *new = xmalloc(sizeof(struct entry));
memset(new, 0, sizeof(*new));
struct entry *new = xcalloc(1, sizeof(struct entry));
memcpy(new->old_sha1, sha1, 20);
memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
convert[pos] = new;
Expand Down Expand Up @@ -122,7 +124,7 @@ static int write_subdirectory(void *buffer, unsigned long size, const char *base
buffer += len;
}

write_sha1_file(new, newlen, "tree", result_sha1);
write_sha1_file(new, newlen, tree_type, result_sha1);
free(new);
return used;
}
Expand Down Expand Up @@ -262,8 +264,8 @@ static void convert_date(void *buffer, unsigned long size, unsigned char *result
memcpy(new + newlen, buffer, size);
newlen += size;

write_sha1_file(new, newlen, "commit", result_sha1);
free(new);
write_sha1_file(new, newlen, commit_type, result_sha1);
free(new);
}

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

buffer = xmalloc(size);
memcpy(buffer, data, size);
if (!strcmp(type, "blob")) {
write_sha1_file(buffer, size, "blob", entry->new_sha1);
} else if (!strcmp(type, "tree"))

if (!strcmp(type, blob_type)) {
write_sha1_file(buffer, size, blob_type, entry->new_sha1);
} else if (!strcmp(type, tree_type))
convert_tree(buffer, size, entry->new_sha1);
else if (!strcmp(type, "commit"))
else if (!strcmp(type, commit_type))
convert_commit(buffer, size, entry->new_sha1);
else
die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
Expand Down
2 changes: 1 addition & 1 deletion diff-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static int diff_root_tree(const unsigned char *new, const char *base)
void *tree;
struct tree_desc empty, real;

tree = read_object_with_reference(new, "tree", &real.size, NULL);
tree = read_object_with_reference(new, tree_type, &real.size, NULL);
if (!tree)
die("unable to read root tree (%s)", sha1_to_hex(new));
real.buf = tree;
Expand Down
3 changes: 2 additions & 1 deletion entry.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sys/types.h>
#include <dirent.h>
#include "cache.h"
#include "blob.h"

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

new = read_sha1_file(ce->sha1, type, &size);
if (!new || strcmp(type, "blob")) {
if (!new || strcmp(type, blob_type)) {
if (new)
free(new);
return error("git-checkout-index: unable to read sha1 file of %s (%s)",
Expand Down
5 changes: 3 additions & 2 deletions hash-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* GIT - The information manager from hell
*
* Copyright (C) Linus Torvalds, 2005
* Copyright (C) Junio C Hamano, 2005
* Copyright (C) Junio C Hamano, 2005
*/
#include "cache.h"
#include "blob.h"

static void hash_object(const char *path, const char *type, int write_object)
{
Expand Down Expand Up @@ -35,7 +36,7 @@ static const char hash_object_usage[] =
int main(int argc, char **argv)
{
int i;
const char *type = "blob";
const char *type = blob_type;
int write_object = 0;
const char *prefix = NULL;
int prefix_length = -1;
Expand Down
6 changes: 2 additions & 4 deletions http-push.c
Original file line number Diff line number Diff line change
Expand Up @@ -1008,8 +1008,7 @@ static int fetch_indices(void)
struct active_request_slot *slot;
struct slot_results results;

data = xmalloc(4096);
memset(data, 0, 4096);
data = xcalloc(1, 4096);
buffer.size = 4096;
buffer.posn = 0;
buffer.buffer = data;
Expand Down Expand Up @@ -2042,8 +2041,7 @@ static void update_remote_info_refs(struct remote_lock *lock)
char *if_header;
struct curl_slist *dav_headers = NULL;

buffer.buffer = xmalloc(4096);
memset(buffer.buffer, 0, 4096);
buffer.buffer = xcalloc(1, 4096);
buffer.size = 4096;
buffer.posn = 0;
remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
Expand Down
12 changes: 8 additions & 4 deletions index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include "delta.h"
#include "pack.h"
#include "csum-file.h"
#include "blob.h"
#include "commit.h"
#include "tag.h"
#include "tree.h"

static const char index_pack_usage[] =
"git-index-pack [-o index-file] pack-file";
Expand Down Expand Up @@ -224,10 +228,10 @@ static void sha1_object(const void *data, unsigned long size,
const char *type_str;

switch (type) {
case OBJ_COMMIT: type_str = "commit"; break;
case OBJ_TREE: type_str = "tree"; break;
case OBJ_BLOB: type_str = "blob"; break;
case OBJ_TAG: type_str = "tag"; break;
case OBJ_COMMIT: type_str = commit_type; break;
case OBJ_TREE: type_str = tree_type; break;
case OBJ_BLOB: type_str = blob_type; break;
case OBJ_TAG: type_str = tag_type; break;
default:
die("bad type %d", type);
}
Expand Down
4 changes: 2 additions & 2 deletions ls-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ static int show_tree(unsigned char *sha1, const char *base, int baselen,
const char *pathname, unsigned mode, int stage)
{
int retval = 0;
const char *type = "blob";
const char *type = blob_type;

if (S_ISDIR(mode)) {
if (show_recursive(base, baselen, pathname)) {
retval = READ_TREE_RECURSIVE;
if (!(ls_options & LS_SHOW_TREES))
return retval;
}
type = "tree";
type = tree_type;
}
else if (ls_options & LS_TREE_ONLY)
return 0;
Expand Down
3 changes: 2 additions & 1 deletion mktag.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cache.h"
#include "tag.h"

/*
* A signature file has a very simple fixed format: three lines
Expand Down Expand Up @@ -126,7 +127,7 @@ int main(int argc, char **argv)
if (verify_tag(buffer, size) < 0)
die("invalid tag signature file");

if (write_sha1_file(buffer, size, "tag", result_sha1) < 0)
if (write_sha1_file(buffer, size, tag_type, result_sha1) < 0)
die("unable to write tag file");
printf("%s\n", sha1_to_hex(result_sha1));
return 0;
Expand Down
3 changes: 2 additions & 1 deletion mktree.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "cache.h"
#include "strbuf.h"
#include "quote.h"
#include "tree.h"

static struct treeent {
unsigned mode;
Expand Down Expand Up @@ -67,7 +68,7 @@ static void write_tree(unsigned char *sha1)
memcpy(buffer + offset, ent->sha1, 20);
offset += 20;
}
write_sha1_file(buffer, offset, "tree", sha1);
write_sha1_file(buffer, offset, tree_type, sha1);
}

static const char mktree_usage[] = "mktree [-z]";
Expand Down
Loading

0 comments on commit 810e152

Please sign in to comment.