aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2016-10-28 09:01:23 -0700
committerJunio C Hamano <gitster@pobox.com>2016-10-28 09:01:23 -0700
commit334c2a19593fa0dfb7c0636bfcd43c31bb7b4ad1 (patch)
tree56815247fc4394a10638a269cad009efa8655811
parentc8fd2201756f9cd10a796311a1b25d7a2ab2a3b9 (diff)
parent13092a916d7b8211fd828a6a7ee0d3cefff995e1 (diff)
downloadgit-334c2a19593fa0dfb7c0636bfcd43c31bb7b4ad1.tar.gz
git-334c2a19593fa0dfb7c0636bfcd43c31bb7b4ad1.tar.xz
Merge branch 'jc/cocci-xstrdup-or-null' into maint
Code cleanup. * jc/cocci-xstrdup-or-null: cocci: refactor common patterns to use xstrdup_or_null()
-rw-r--r--contrib/coccinelle/xstrdup_or_null.cocci7
-rw-r--r--git.c3
-rw-r--r--imap-send.c6
-rw-r--r--mailmap.c6
-rw-r--r--refs.c3
-rw-r--r--send-pack.c3
-rw-r--r--trailer.c9
7 files changed, 17 insertions, 20 deletions
diff --git a/contrib/coccinelle/xstrdup_or_null.cocci b/contrib/coccinelle/xstrdup_or_null.cocci
new file mode 100644
index 000000000..3fceef132
--- /dev/null
+++ b/contrib/coccinelle/xstrdup_or_null.cocci
@@ -0,0 +1,7 @@
+@@
+expression E;
+expression V;
+@@
+- if (E)
+- V = xstrdup(E);
++ V = xstrdup_or_null(E);
diff --git a/git.c b/git.c
index 0f1937fd0..f914490e1 100644
--- a/git.c
+++ b/git.c
@@ -35,8 +35,7 @@ static void save_env_before_alias(void)
orig_cwd = xgetcwd();
for (i = 0; i < ARRAY_SIZE(env_names); i++) {
orig_env[i] = getenv(env_names[i]);
- if (orig_env[i])
- orig_env[i] = xstrdup(orig_env[i]);
+ orig_env[i] = xstrdup_or_null(orig_env[i]);
}
}
diff --git a/imap-send.c b/imap-send.c
index adb9738c3..5c7e27a89 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1082,10 +1082,8 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, char *f
cred.protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap");
cred.host = xstrdup(srvc->host);
- if (srvc->user)
- cred.username = xstrdup(srvc->user);
- if (srvc->pass)
- cred.password = xstrdup(srvc->pass);
+ cred.username = xstrdup_or_null(srvc->user);
+ cred.password = xstrdup_or_null(srvc->pass);
credential_fill(&cred);
diff --git a/mailmap.c b/mailmap.c
index b5c521fde..c1a79c100 100644
--- a/mailmap.c
+++ b/mailmap.c
@@ -103,10 +103,8 @@ static void add_mapping(struct string_list *map,
} else {
struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
debug_mm("mailmap: adding (complex) entry for '%s'\n", old_email);
- if (new_name)
- mi->name = xstrdup(new_name);
- if (new_email)
- mi->email = xstrdup(new_email);
+ mi->name = xstrdup_or_null(new_name);
+ mi->email = xstrdup_or_null(new_email);
string_list_insert(&me->namemap, old_name)->util = mi;
}
diff --git a/refs.c b/refs.c
index b4e7cac7b..62055ab09 100644
--- a/refs.c
+++ b/refs.c
@@ -791,8 +791,7 @@ struct ref_update *ref_transaction_add_update(
hashcpy(update->new_sha1, new_sha1);
if (flags & REF_HAVE_OLD)
hashcpy(update->old_sha1, old_sha1);
- if (msg)
- update->msg = xstrdup(msg);
+ update->msg = xstrdup_or_null(msg);
return update;
}
diff --git a/send-pack.c b/send-pack.c
index 90f2ac51a..6195b43e9 100644
--- a/send-pack.c
+++ b/send-pack.c
@@ -181,8 +181,7 @@ static int receive_status(int in, struct ref *refs)
hint->status = REF_STATUS_REMOTE_REJECT;
ret = -1;
}
- if (msg)
- hint->remote_status = xstrdup(msg);
+ hint->remote_status = xstrdup_or_null(msg);
/* start our next search from the next ref */
hint = hint->next;
}
diff --git a/trailer.c b/trailer.c
index c6ea9ac64..aecaf9232 100644
--- a/trailer.c
+++ b/trailer.c
@@ -428,12 +428,9 @@ static int set_if_missing(struct conf_info *item, const char *value)
static void duplicate_conf(struct conf_info *dst, struct conf_info *src)
{
*dst = *src;
- if (src->name)
- dst->name = xstrdup(src->name);
- if (src->key)
- dst->key = xstrdup(src->key);
- if (src->command)
- dst->command = xstrdup(src->command);
+ dst->name = xstrdup_or_null(src->name);
+ dst->key = xstrdup_or_null(src->key);
+ dst->command = xstrdup_or_null(src->command);
}
static struct trailer_item *get_conf_item(const char *name)