Merge branch 'jk/blame-commit-label' into maint

"git blame HEAD -- missing" failed to correctly say "HEAD" when it
tried to say "No such path 'missing' in HEAD".

* jk/blame-commit-label:
  blame.c: fix garbled error message
  use xstrdup_or_null to replace ternary conditionals
  builtin/commit.c: use xstrdup_or_null instead of envdup
  builtin/apply.c: use xstrdup_or_null instead of null_strdup
  git-compat-util: add xstrdup_or_null helper
This commit is contained in:
Junio C Hamano 2015-02-24 22:09:54 -08:00
commit faf723a631
11 changed files with 29 additions and 34 deletions

View File

@ -657,11 +657,6 @@ static size_t diff_timestamp_len(const char *line, size_t len)
return line + len - end; return line + len - end;
} }
static char *null_strdup(const char *s)
{
return s ? xstrdup(s) : NULL;
}
static char *find_name_common(const char *line, const char *def, static char *find_name_common(const char *line, const char *def,
int p_value, const char *end, int terminate) int p_value, const char *end, int terminate)
{ {
@ -684,10 +679,10 @@ static char *find_name_common(const char *line, const char *def,
start = line; start = line;
} }
if (!start) if (!start)
return squash_slash(null_strdup(def)); return squash_slash(xstrdup_or_null(def));
len = line - start; len = line - start;
if (!len) if (!len)
return squash_slash(null_strdup(def)); return squash_slash(xstrdup_or_null(def));
/* /*
* Generally we prefer the shorter name, especially * Generally we prefer the shorter name, especially
@ -909,7 +904,7 @@ static void parse_traditional_patch(const char *first, const char *second, struc
patch->old_name = name; patch->old_name = name;
} else { } else {
patch->old_name = name; patch->old_name = name;
patch->new_name = null_strdup(name); patch->new_name = xstrdup_or_null(name);
} }
} }
if (!name) if (!name)
@ -998,7 +993,7 @@ static int gitdiff_delete(const char *line, struct patch *patch)
{ {
patch->is_delete = 1; patch->is_delete = 1;
free(patch->old_name); free(patch->old_name);
patch->old_name = null_strdup(patch->def_name); patch->old_name = xstrdup_or_null(patch->def_name);
return gitdiff_oldmode(line, patch); return gitdiff_oldmode(line, patch);
} }
@ -1006,7 +1001,7 @@ static int gitdiff_newfile(const char *line, struct patch *patch)
{ {
patch->is_new = 1; patch->is_new = 1;
free(patch->new_name); free(patch->new_name);
patch->new_name = null_strdup(patch->def_name); patch->new_name = xstrdup_or_null(patch->def_name);
return gitdiff_newmode(line, patch); return gitdiff_newmode(line, patch);
} }

View File

@ -2390,7 +2390,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
return commit; return commit;
} }
static const char *prepare_final(struct scoreboard *sb) static char *prepare_final(struct scoreboard *sb)
{ {
int i; int i;
const char *final_commit_name = NULL; const char *final_commit_name = NULL;
@ -2415,10 +2415,10 @@ static const char *prepare_final(struct scoreboard *sb)
sb->final = (struct commit *) obj; sb->final = (struct commit *) obj;
final_commit_name = revs->pending.objects[i].name; final_commit_name = revs->pending.objects[i].name;
} }
return final_commit_name; return xstrdup_or_null(final_commit_name);
} }
static const char *prepare_initial(struct scoreboard *sb) static char *prepare_initial(struct scoreboard *sb)
{ {
int i; int i;
const char *final_commit_name = NULL; const char *final_commit_name = NULL;
@ -2445,7 +2445,7 @@ static const char *prepare_initial(struct scoreboard *sb)
} }
if (!final_commit_name) if (!final_commit_name)
die("No commit to dig down to?"); die("No commit to dig down to?");
return final_commit_name; return xstrdup(final_commit_name);
} }
static int blame_copy_callback(const struct option *option, const char *arg, int unset) static int blame_copy_callback(const struct option *option, const char *arg, int unset)
@ -2489,7 +2489,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
struct origin *o; struct origin *o;
struct blame_entry *ent = NULL; struct blame_entry *ent = NULL;
long dashdash_pos, lno; long dashdash_pos, lno;
const char *final_commit_name = NULL; char *final_commit_name = NULL;
enum object_type type; enum object_type type;
static struct string_list range_list; static struct string_list range_list;
@ -2786,6 +2786,8 @@ parse_done:
assign_blame(&sb, opt); assign_blame(&sb, opt);
free(final_commit_name);
if (incremental) if (incremental)
return 0; return 0;

View File

@ -559,20 +559,14 @@ static void set_ident_var(char **buf, char *val)
*buf = val; *buf = val;
} }
static char *envdup(const char *var)
{
const char *val = getenv(var);
return val ? xstrdup(val) : NULL;
}
static void determine_author_info(struct strbuf *author_ident) static void determine_author_info(struct strbuf *author_ident)
{ {
char *name, *email, *date; char *name, *email, *date;
struct ident_split author; struct ident_split author;
name = envdup("GIT_AUTHOR_NAME"); name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
email = envdup("GIT_AUTHOR_EMAIL"); email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
date = envdup("GIT_AUTHOR_DATE"); date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
if (author_message) { if (author_message) {
struct ident_split ident; struct ident_split ident;

View File

@ -1340,7 +1340,7 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
string_list_init(&e->value_list, 1); string_list_init(&e->value_list, 1);
hashmap_add(&cs->config_hash, e); hashmap_add(&cs->config_hash, e);
} }
si = string_list_append_nodup(&e->value_list, value ? xstrdup(value) : NULL); si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc); ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc);
l_item = &cs->list.items[cs->list.nr++]; l_item = &cs->list.items[cs->list.nr++];

View File

@ -678,6 +678,11 @@ extern char *xgetcwd(void);
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x))) #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x)))
static inline char *xstrdup_or_null(const char *str)
{
return str ? xstrdup(str) : NULL;
}
static inline size_t xsize_t(off_t len) static inline size_t xsize_t(off_t len)
{ {
if (len > (size_t) len) if (len > (size_t) len)

4
grep.c
View File

@ -1661,8 +1661,8 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
const void *identifier) const void *identifier)
{ {
gs->type = type; gs->type = type;
gs->name = name ? xstrdup(name) : NULL; gs->name = xstrdup_or_null(name);
gs->path = path ? xstrdup(path) : NULL; gs->path = xstrdup_or_null(path);
gs->buf = NULL; gs->buf = NULL;
gs->size = 0; gs->size = 0;
gs->driver = NULL; gs->driver = NULL;

View File

@ -1006,7 +1006,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node)); t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
t->first_non_note = NULL; t->first_non_note = NULL;
t->prev_non_note = NULL; t->prev_non_note = NULL;
t->ref = notes_ref ? xstrdup(notes_ref) : NULL; t->ref = xstrdup_or_null(notes_ref);
t->combine_notes = combine_notes; t->combine_notes = combine_notes;
t->initialized = 1; t->initialized = 1;
t->dirty = 0; t->dirty = 0;

3
refs.c
View File

@ -1618,8 +1618,7 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags, unsigned
char *resolve_refdup(const char *ref, int resolve_flags, unsigned char *sha1, int *flags) char *resolve_refdup(const char *ref, int resolve_flags, unsigned char *sha1, int *flags)
{ {
const char *ret = resolve_ref_unsafe(ref, resolve_flags, sha1, flags); return xstrdup_or_null(resolve_ref_unsafe(ref, resolve_flags, sha1, flags));
return ret ? xstrdup(ret) : NULL;
} }
/* The argument to filter_refs */ /* The argument to filter_refs */

View File

@ -975,8 +975,8 @@ struct ref *copy_ref(const struct ref *ref)
cpy = xmalloc(sizeof(struct ref) + len + 1); cpy = xmalloc(sizeof(struct ref) + len + 1);
memcpy(cpy, ref, sizeof(struct ref) + len + 1); memcpy(cpy, ref, sizeof(struct ref) + len + 1);
cpy->next = NULL; cpy->next = NULL;
cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL; cpy->symref = xstrdup_or_null(ref->symref);
cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL; cpy->remote_status = xstrdup_or_null(ref->remote_status);
cpy->peer_ref = copy_ref(ref->peer_ref); cpy->peer_ref = copy_ref(ref->peer_ref);
return cpy; return cpy;
} }

View File

@ -22,7 +22,7 @@ void set_alternate_shallow_file(const char *path, int override)
if (alternate_shallow_file && !override) if (alternate_shallow_file && !override)
return; return;
free(alternate_shallow_file); free(alternate_shallow_file);
alternate_shallow_file = path ? xstrdup(path) : NULL; alternate_shallow_file = xstrdup_or_null(path);
} }
int register_shallow(const unsigned char *sha1) int register_shallow(const unsigned char *sha1)

View File

@ -232,7 +232,7 @@ int walker_targets_stdin(char ***target, const char ***write_ref)
REALLOC_ARRAY(*write_ref, targets_alloc); REALLOC_ARRAY(*write_ref, targets_alloc);
} }
(*target)[targets] = xstrdup(tg_one); (*target)[targets] = xstrdup(tg_one);
(*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL; (*write_ref)[targets] = xstrdup_or_null(rf_one);
targets++; targets++;
} }
strbuf_release(&buf); strbuf_release(&buf);