git-rev-list: factor out the commit printing from "main()"
[git/haiku.git] / gitenv.c
blobab9396f969f6f3268e3bf0b5f8f8dea0e9153983
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "cache.h"
6 /*
7 * This array must be sorted by its canonical name, because
8 * we do look-up by binary search.
9 */
10 static struct backward_compatible_env {
11 const char *canonical;
12 const char *old;
13 } bc_name[] = {
14 { "GIT_ALTERNATE_OBJECT_DIRECTORIES", "SHA1_FILE_DIRECTORIES" },
15 { "GIT_AUTHOR_DATE", "AUTHOR_DATE" },
16 { "GIT_AUTHOR_EMAIL", "AUTHOR_EMAIL" },
17 { "GIT_AUTHOR_NAME", "AUTHOR_NAME" },
18 { "GIT_COMMITTER_EMAIL", "COMMIT_AUTHOR_EMAIL" },
19 { "GIT_COMMITTER_NAME", "COMMIT_AUTHOR_NAME" },
20 { "GIT_OBJECT_DIRECTORY", "SHA1_FILE_DIRECTORY" },
23 static void warn_old_environment(int pos)
25 int i;
26 static int warned = 0;
27 if (warned)
28 return;
30 warned = 1;
31 fprintf(stderr,
32 "warning: Attempting to use %s\n",
33 bc_name[pos].old);
34 fprintf(stderr,
35 "warning: GIT environment variables have been renamed.\n"
36 "warning: Please adjust your scripts and environment.\n");
37 for (i = 0; i < sizeof(bc_name) / sizeof(bc_name[0]); i++) {
38 /* warning is needed only when old name is there and
39 * new name is not.
41 if (!getenv(bc_name[i].canonical) && getenv(bc_name[i].old))
42 fprintf(stderr, "warning: old %s => new %s\n",
43 bc_name[i].old, bc_name[i].canonical);
47 char *gitenv_bc(const char *e)
49 int first, last;
50 char *val = getenv(e);
51 if (val)
52 die("gitenv_bc called on existing %s; fix the caller.", e);
54 first = 0;
55 last = sizeof(bc_name) / sizeof(bc_name[0]);
56 while (last > first) {
57 int next = (last + first) >> 1;
58 int cmp = strcmp(e, bc_name[next].canonical);
59 if (!cmp) {
60 val = getenv(bc_name[next].old);
61 /* If the user has only old name, warn.
62 * otherwise stay silent.
64 if (val)
65 warn_old_environment(next);
66 return val;
68 if (cmp < 0) {
69 last = next;
70 continue;
72 first = next+1;
74 return NULL;