Skip to content

Commit 8c53f07

Browse files
peffgitster
authored andcommitted
use xstrdup_or_null to replace ternary conditionals
This replaces "x ? xstrdup(x) : NULL" with xstrdup_or_null(x). The change is fairly mechanical, with the exception of resolve_refdup, which can eliminate a temporary variable. There are still a few hits grepping for "?.*xstrdup", but these are of slightly different forms and cannot be converted (e.g., "x ? xstrdup(x->foo) : NULL"). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eaa541e commit 8c53f07

File tree

7 files changed

+9
-10
lines changed

7 files changed

+9
-10
lines changed

config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
13291329
string_list_init(&e->value_list, 1);
13301330
hashmap_add(&cs->config_hash, e);
13311331
}
1332-
si = string_list_append_nodup(&e->value_list, value ? xstrdup(value) : NULL);
1332+
si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
13331333

13341334
ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc);
13351335
l_item = &cs->list.items[cs->list.nr++];

grep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,8 +1646,8 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
16461646
const void *identifier)
16471647
{
16481648
gs->type = type;
1649-
gs->name = name ? xstrdup(name) : NULL;
1650-
gs->path = path ? xstrdup(path) : NULL;
1649+
gs->name = xstrdup_or_null(name);
1650+
gs->path = xstrdup_or_null(path);
16511651
gs->buf = NULL;
16521652
gs->size = 0;
16531653
gs->driver = NULL;

notes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
10061006
t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
10071007
t->first_non_note = NULL;
10081008
t->prev_non_note = NULL;
1009-
t->ref = notes_ref ? xstrdup(notes_ref) : NULL;
1009+
t->ref = xstrdup_or_null(notes_ref);
10101010
t->combine_notes = combine_notes;
10111011
t->initialized = 1;
10121012
t->dirty = 0;

refs.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,8 +1529,7 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
15291529

15301530
char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag)
15311531
{
1532-
const char *ret = resolve_ref_unsafe(ref, sha1, reading, flag);
1533-
return ret ? xstrdup(ret) : NULL;
1532+
return xstrdup_or_null(resolve_ref_unsafe(ref, sha1, reading, flag));
15341533
}
15351534

15361535
/* The argument to filter_refs */

remote.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,8 +975,8 @@ struct ref *copy_ref(const struct ref *ref)
975975
cpy = xmalloc(sizeof(struct ref) + len + 1);
976976
memcpy(cpy, ref, sizeof(struct ref) + len + 1);
977977
cpy->next = NULL;
978-
cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
979-
cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
978+
cpy->symref = xstrdup_or_null(ref->symref);
979+
cpy->remote_status = xstrdup_or_null(ref->remote_status);
980980
cpy->peer_ref = copy_ref(ref->peer_ref);
981981
return cpy;
982982
}

shallow.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void set_alternate_shallow_file(const char *path, int override)
2121
if (alternate_shallow_file && !override)
2222
return;
2323
free(alternate_shallow_file);
24-
alternate_shallow_file = path ? xstrdup(path) : NULL;
24+
alternate_shallow_file = xstrdup_or_null(path);
2525
}
2626

2727
int register_shallow(const unsigned char *sha1)

walker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ int walker_targets_stdin(char ***target, const char ***write_ref)
232232
REALLOC_ARRAY(*write_ref, targets_alloc);
233233
}
234234
(*target)[targets] = xstrdup(tg_one);
235-
(*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
235+
(*write_ref)[targets] = xstrdup_or_null(rf_one);
236236
targets++;
237237
}
238238
strbuf_release(&buf);

0 commit comments

Comments
 (0)