Skip to content

Commit faf723a

Browse files
committed
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
2 parents 9874fca + a46442f commit faf723a

File tree

11 files changed

+29
-34
lines changed

11 files changed

+29
-34
lines changed

builtin/apply.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,6 @@ static size_t diff_timestamp_len(const char *line, size_t len)
657657
return line + len - end;
658658
}
659659

660-
static char *null_strdup(const char *s)
661-
{
662-
return s ? xstrdup(s) : NULL;
663-
}
664-
665660
static char *find_name_common(const char *line, const char *def,
666661
int p_value, const char *end, int terminate)
667662
{
@@ -684,10 +679,10 @@ static char *find_name_common(const char *line, const char *def,
684679
start = line;
685680
}
686681
if (!start)
687-
return squash_slash(null_strdup(def));
682+
return squash_slash(xstrdup_or_null(def));
688683
len = line - start;
689684
if (!len)
690-
return squash_slash(null_strdup(def));
685+
return squash_slash(xstrdup_or_null(def));
691686

692687
/*
693688
* Generally we prefer the shorter name, especially
@@ -909,7 +904,7 @@ static void parse_traditional_patch(const char *first, const char *second, struc
909904
patch->old_name = name;
910905
} else {
911906
patch->old_name = name;
912-
patch->new_name = null_strdup(name);
907+
patch->new_name = xstrdup_or_null(name);
913908
}
914909
}
915910
if (!name)
@@ -998,15 +993,15 @@ static int gitdiff_delete(const char *line, struct patch *patch)
998993
{
999994
patch->is_delete = 1;
1000995
free(patch->old_name);
1001-
patch->old_name = null_strdup(patch->def_name);
996+
patch->old_name = xstrdup_or_null(patch->def_name);
1002997
return gitdiff_oldmode(line, patch);
1003998
}
1004999

10051000
static int gitdiff_newfile(const char *line, struct patch *patch)
10061001
{
10071002
patch->is_new = 1;
10081003
free(patch->new_name);
1009-
patch->new_name = null_strdup(patch->def_name);
1004+
patch->new_name = xstrdup_or_null(patch->def_name);
10101005
return gitdiff_newmode(line, patch);
10111006
}
10121007

builtin/blame.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,7 +2390,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
23902390
return commit;
23912391
}
23922392

2393-
static const char *prepare_final(struct scoreboard *sb)
2393+
static char *prepare_final(struct scoreboard *sb)
23942394
{
23952395
int i;
23962396
const char *final_commit_name = NULL;
@@ -2415,10 +2415,10 @@ static const char *prepare_final(struct scoreboard *sb)
24152415
sb->final = (struct commit *) obj;
24162416
final_commit_name = revs->pending.objects[i].name;
24172417
}
2418-
return final_commit_name;
2418+
return xstrdup_or_null(final_commit_name);
24192419
}
24202420

2421-
static const char *prepare_initial(struct scoreboard *sb)
2421+
static char *prepare_initial(struct scoreboard *sb)
24222422
{
24232423
int i;
24242424
const char *final_commit_name = NULL;
@@ -2445,7 +2445,7 @@ static const char *prepare_initial(struct scoreboard *sb)
24452445
}
24462446
if (!final_commit_name)
24472447
die("No commit to dig down to?");
2448-
return final_commit_name;
2448+
return xstrdup(final_commit_name);
24492449
}
24502450

24512451
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)
24892489
struct origin *o;
24902490
struct blame_entry *ent = NULL;
24912491
long dashdash_pos, lno;
2492-
const char *final_commit_name = NULL;
2492+
char *final_commit_name = NULL;
24932493
enum object_type type;
24942494

24952495
static struct string_list range_list;
@@ -2786,6 +2786,8 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
27862786

27872787
assign_blame(&sb, opt);
27882788

2789+
free(final_commit_name);
2790+
27892791
if (incremental)
27902792
return 0;
27912793

builtin/commit.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -559,20 +559,14 @@ static void set_ident_var(char **buf, char *val)
559559
*buf = val;
560560
}
561561

562-
static char *envdup(const char *var)
563-
{
564-
const char *val = getenv(var);
565-
return val ? xstrdup(val) : NULL;
566-
}
567-
568562
static void determine_author_info(struct strbuf *author_ident)
569563
{
570564
char *name, *email, *date;
571565
struct ident_split author;
572566

573-
name = envdup("GIT_AUTHOR_NAME");
574-
email = envdup("GIT_AUTHOR_EMAIL");
575-
date = envdup("GIT_AUTHOR_DATE");
567+
name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
568+
email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
569+
date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
576570

577571
if (author_message) {
578572
struct ident_split ident;

config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
13401340
string_list_init(&e->value_list, 1);
13411341
hashmap_add(&cs->config_hash, e);
13421342
}
1343-
si = string_list_append_nodup(&e->value_list, value ? xstrdup(value) : NULL);
1343+
si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
13441344

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

git-compat-util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,11 @@ extern char *xgetcwd(void);
678678

679679
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x)))
680680

681+
static inline char *xstrdup_or_null(const char *str)
682+
{
683+
return str ? xstrdup(str) : NULL;
684+
}
685+
681686
static inline size_t xsize_t(off_t len)
682687
{
683688
if (len > (size_t) len)

grep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,8 +1661,8 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
16611661
const void *identifier)
16621662
{
16631663
gs->type = type;
1664-
gs->name = name ? xstrdup(name) : NULL;
1665-
gs->path = path ? xstrdup(path) : NULL;
1664+
gs->name = xstrdup_or_null(name);
1665+
gs->path = xstrdup_or_null(path);
16661666
gs->buf = NULL;
16671667
gs->size = 0;
16681668
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
@@ -1618,8 +1618,7 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags, unsigned
16181618

16191619
char *resolve_refdup(const char *ref, int resolve_flags, unsigned char *sha1, int *flags)
16201620
{
1621-
const char *ret = resolve_ref_unsafe(ref, resolve_flags, sha1, flags);
1622-
return ret ? xstrdup(ret) : NULL;
1621+
return xstrdup_or_null(resolve_ref_unsafe(ref, resolve_flags, sha1, flags));
16231622
}
16241623

16251624
/* 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
@@ -22,7 +22,7 @@ void set_alternate_shallow_file(const char *path, int override)
2222
if (alternate_shallow_file && !override)
2323
return;
2424
free(alternate_shallow_file);
25-
alternate_shallow_file = path ? xstrdup(path) : NULL;
25+
alternate_shallow_file = xstrdup_or_null(path);
2626
}
2727

2828
int register_shallow(const unsigned char *sha1)

0 commit comments

Comments
 (0)