git-commit-vandalism/gitenv.c

76 lines
1.8 KiB
C
Raw Normal View History

Rename environment variables. H. Peter Anvin mentioned that using SHA1_whatever as an environment variable name is not nice and we should instead use names starting with "GIT_" prefix to avoid conflicts. Here is what this patch does: * Renames the following environment variables: New name Old Name GIT_AUTHOR_DATE AUTHOR_DATE GIT_AUTHOR_EMAIL AUTHOR_EMAIL GIT_AUTHOR_NAME AUTHOR_NAME GIT_COMMITTER_EMAIL COMMIT_AUTHOR_EMAIL GIT_COMMITTER_NAME COMMIT_AUTHOR_NAME GIT_ALTERNATE_OBJECT_DIRECTORIES SHA1_FILE_DIRECTORIES GIT_OBJECT_DIRECTORY SHA1_FILE_DIRECTORY * Introduces a compatibility macro, gitenv(), which does an getenv() and if it fails calls gitenv_bc(), which in turn picks up the value from old name while giving a warning about using an old name. * Changes all users of the environment variable to fetch environment variable with the new name using gitenv(). * Updates the documentation and scripts shipped with Linus GIT distribution. The transition plan is as follows: * We will keep the backward compatibility list used by gitenv() for now, so the current scripts and user environments continue to work as before. The users will get warnings when they have old name but not new name in their environment to the stderr. * The Porcelain layers should start using new names. However, just in case it ends up calling old Plumbing layer implementation, they should also export old names, taking values from the corresponding new names, during the transition period. * After a transition period, we would drop the compatibility support and drop gitenv(). Revert the callers to directly call getenv() but keep using the new names. The last part is probably optional and the transition duration needs to be set to a reasonable value. Signed-off-by: Junio C Hamano <junkio@cox.net>
2005-05-10 02:57:56 +02:00
/*
* Copyright (C) 2005 Junio C Hamano
*/
#include "cache.h"
/*
* This array must be sorted by its canonical name, because
* we do look-up by binary search.
*/
static struct backward_compatible_env {
const char *canonical;
const char *old;
} bc_name[] = {
{ "GIT_ALTERNATE_OBJECT_DIRECTORIES", "SHA1_FILE_DIRECTORIES" },
{ "GIT_AUTHOR_DATE", "AUTHOR_DATE" },
{ "GIT_AUTHOR_EMAIL", "AUTHOR_EMAIL" },
{ "GIT_AUTHOR_NAME", "AUTHOR_NAME" },
{ "GIT_COMMITTER_EMAIL", "COMMIT_AUTHOR_EMAIL" },
{ "GIT_COMMITTER_NAME", "COMMIT_AUTHOR_NAME" },
{ "GIT_OBJECT_DIRECTORY", "SHA1_FILE_DIRECTORY" },
};
static void warn_old_environment(int pos)
{
int i;
static int warned = 0;
if (warned)
return;
warned = 1;
fprintf(stderr,
"warning: Attempting to use %s\n",
bc_name[pos].old);
fprintf(stderr,
"warning: GIT environment variables have been renamed.\n"
"warning: Please adjust your scripts and environment.\n");
for (i = 0; i < sizeof(bc_name) / sizeof(bc_name[0]); i++) {
/* warning is needed only when old name is there and
* new name is not.
*/
if (!getenv(bc_name[i].canonical) && getenv(bc_name[i].old))
fprintf(stderr, "warning: old %s => new %s\n",
bc_name[i].old, bc_name[i].canonical);
}
}
char *gitenv_bc(const char *e)
{
int first, last;
char *val = getenv(e);
if (val)
die("gitenv_bc called on existing %s; fix the caller.", e);
first = 0;
last = sizeof(bc_name) / sizeof(bc_name[0]);
while (last > first) {
int next = (last + first) >> 1;
int cmp = strcmp(e, bc_name[next].canonical);
if (!cmp) {
val = getenv(bc_name[next].old);
/* If the user has only old name, warn.
* otherwise stay silent.
*/
if (val)
warn_old_environment(next);
return val;
}
if (cmp < 0) {
last = next;
continue;
}
first = next+1;
}
return NULL;
}