Skip to content

Commit 3f2e229

Browse files
peffgitster
authored andcommitted
add an extra level of indirection to main()
There are certain startup tasks that we expect every git process to do. In some cases this is just to improve the quality of the program (e.g., setting up gettext()). In others it is a requirement for using certain functions in libgit.a (e.g., system_path() expects that you have called git_extract_argv0_path()). Most commands are builtins and are covered by the git.c version of main(). However, there are still a few external commands that use their own main(). Each of these has to remember to include the correct startup sequence, and we are not always consistent. Rather than just fix the inconsistencies, let's make this harder to get wrong by providing a common main() that can run this standard startup. We basically have two options to do this: - the compat/mingw.h file already does something like this by adding a #define that replaces the definition of main with a wrapper that calls mingw_startup(). The upside is that the code in each program doesn't need to be changed at all; it's rewritten on the fly by the preprocessor. The downside is that it may make debugging of the startup sequence a bit more confusing, as the preprocessor is quietly inserting new code. - the builtin functions are all of the form cmd_foo(), and git.c's main() calls them. This is much more explicit, which may make things more obvious to somebody reading the code. It's also more flexible (because of course we have to figure out _which_ cmd_foo() to call). The downside is that each of the builtins must define cmd_foo(), instead of just main(). This patch chooses the latter option, preferring the more explicit approach, even though it is more invasive. We introduce a new file common-main.c, with the "real" main. It expects to call cmd_main() from whatever other objects it is linked against. We link common-main.o against anything that links against libgit.a, since we know that such programs will need to do this setup. Note that common-main.o can't actually go inside libgit.a, as the linker would not pick up its main() function automatically (it has no callers). The rest of the patch is just adjusting all of the various external programs (mostly in t/helper) to use cmd_main(). I've provided a global declaration for cmd_main(), which means that all of the programs also need to match its signature. In particular, many functions need to switch to "const char **" instead of "char **" for argv. This effect ripples out to a few other variables and functions, as well. This makes the patch even more invasive, but the end result is much better. We should be treating argv strings as const anyway, and now all programs conform to the same signature (which also matches the way builtins are defined). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0b65a8d commit 3f2e229

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+91
-69
lines changed

Makefile

+13-4
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ BUILTIN_OBJS += builtin/verify-tag.o
943943
BUILTIN_OBJS += builtin/worktree.o
944944
BUILTIN_OBJS += builtin/write-tree.o
945945

946-
GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
946+
GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB)
947947
EXTLIBS =
948948

949949
GIT_USER_AGENT = git/$(GIT_VERSION)
@@ -1572,7 +1572,15 @@ TCLTK_PATH_SQ = $(subst ','\'',$(TCLTK_PATH))
15721572
DIFF_SQ = $(subst ','\'',$(DIFF))
15731573
PERLLIB_EXTRA_SQ = $(subst ','\'',$(PERLLIB_EXTRA))
15741574

1575-
LIBS = $(GITLIBS) $(EXTLIBS)
1575+
# We must filter out any object files from $(GITLIBS),
1576+
# as it is typically used like:
1577+
#
1578+
# foo: foo.o $(GITLIBS)
1579+
# $(CC) $(filter %.o,$^) $(LIBS)
1580+
#
1581+
# where we use it as a dependency. Since we also pull object files
1582+
# from the dependency list, that would make each entry appear twice.
1583+
LIBS = $(filter-out %.o, $(GITLIBS)) $(EXTLIBS)
15761584

15771585
BASIC_CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER_SQ)' \
15781586
$(COMPAT_CFLAGS)
@@ -1708,8 +1716,8 @@ git.sp git.s git.o: EXTRA_CPPFLAGS = \
17081716
'-DGIT_INFO_PATH="$(infodir_relative_SQ)"'
17091717

17101718
git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS)
1711-
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) git.o \
1712-
$(BUILTIN_OBJS) $(LIBS)
1719+
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) \
1720+
$(filter %.o,$^) $(LIBS)
17131721

17141722
help.sp help.s help.o: common-cmds.h
17151723

@@ -1902,6 +1910,7 @@ TEST_OBJS := $(patsubst test-%$X,test-%.o,$(TEST_PROGRAMS))
19021910
OBJECTS := $(LIB_OBJS) $(BUILTIN_OBJS) $(PROGRAM_OBJS) $(TEST_OBJS) \
19031911
$(XDIFF_OBJS) \
19041912
$(VCSSVN_OBJS) \
1913+
common-main.o \
19051914
git.o
19061915
ifndef NO_CURL
19071916
OBJECTS += http.o http-walker.o remote-curl.o

common-main.c

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "git-compat-util.h"
2+
3+
int main(int argc, char **av)
4+
{
5+
/*
6+
* This const trickery is explained in
7+
* 84d32bf7678259c08406571cd6ce4b7a6724dcba
8+
*/
9+
const char **argv = (const char **)av;
10+
11+
return cmd_main(argc, argv);
12+
}

credential-cache--daemon.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ static void init_socket_directory(const char *path)
257257
free(path_copy);
258258
}
259259

260-
int main(int argc, const char **argv)
260+
int cmd_main(int argc, const char **argv)
261261
{
262262
const char *socket_path;
263263
int ignore_sighup = 0;

credential-cache.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static void do_cache(const char *socket, const char *action, int timeout,
8383
strbuf_release(&buf);
8484
}
8585

86-
int main(int argc, const char **argv)
86+
int cmd_main(int argc, const char **argv)
8787
{
8888
char *socket_path = NULL;
8989
int timeout = 900;

credential-store.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static void lookup_credential(const struct string_list *fns, struct credential *
142142
return; /* Found credential */
143143
}
144144

145-
int main(int argc, char **argv)
145+
int cmd_main(int argc, const char **argv)
146146
{
147147
const char * const usage[] = {
148148
"git credential-store [<options>] <action>",

daemon.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static const char daemon_usage[] =
3232
" [<directory>...]";
3333

3434
/* List of acceptable pathname prefixes */
35-
static char **ok_paths;
35+
static const char **ok_paths;
3636
static int strict_paths;
3737

3838
/* If this is set, git-daemon-export-ok is not required */
@@ -240,7 +240,7 @@ static const char *path_ok(const char *directory, struct hostinfo *hi)
240240
}
241241

242242
if ( ok_paths && *ok_paths ) {
243-
char **pp;
243+
const char **pp;
244244
int pathlen = strlen(path);
245245

246246
/* The validation is done on the paths after enter_repo
@@ -1178,7 +1178,7 @@ static int serve(struct string_list *listen_addr, int listen_port,
11781178
return service_loop(&socklist);
11791179
}
11801180

1181-
int main(int argc, char **argv)
1181+
int cmd_main(int argc, const char **argv)
11821182
{
11831183
int listen_port = 0;
11841184
struct string_list listen_addr = STRING_LIST_INIT_NODUP;
@@ -1193,7 +1193,7 @@ int main(int argc, char **argv)
11931193
git_extract_argv0_path(argv[0]);
11941194

11951195
for (i = 1; i < argc; i++) {
1196-
char *arg = argv[i];
1196+
const char *arg = argv[i];
11971197
const char *v;
11981198

11991199
if (skip_prefix(arg, "--listen=", &v)) {

fast-import.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ static int failure;
300300
static FILE *pack_edges;
301301
static unsigned int show_stats = 1;
302302
static int global_argc;
303-
static char **global_argv;
303+
static const char **global_argv;
304304

305305
/* Memory pools */
306306
static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
@@ -3381,7 +3381,7 @@ static void parse_argv(void)
33813381
read_marks();
33823382
}
33833383

3384-
int main(int argc, char **argv)
3384+
int cmd_main(int argc, const char **argv)
33853385
{
33863386
unsigned int i;
33873387

git-compat-util.h

+2
Original file line numberDiff line numberDiff line change
@@ -1043,3 +1043,5 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);
10431043
#endif
10441044

10451045
#endif
1046+
1047+
extern int cmd_main(int, const char **);

git.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,8 @@ static void restore_sigpipe_to_default(void)
630630
signal(SIGPIPE, SIG_DFL);
631631
}
632632

633-
int main(int argc, char **av)
633+
int cmd_main(int argc, const char **argv)
634634
{
635-
const char **argv = (const char **) av;
636635
const char *cmd;
637636
int done_help = 0;
638637

http-backend.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ static struct service_cmd {
632632
{"POST", "/git-receive-pack$", service_rpc}
633633
};
634634

635-
int main(int argc, char **argv)
635+
int cmd_main(int argc, const char **argv)
636636
{
637637
char *method = getenv("REQUEST_METHOD");
638638
char *dir;

http-fetch.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
static const char http_fetch_usage[] = "git http-fetch "
77
"[-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin] commit-id url";
88

9-
int main(int argc, const char **argv)
9+
int cmd_main(int argc, const char **argv)
1010
{
1111
struct walker *walker;
1212
int commits_on_stdin = 0;

http-push.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1692,12 +1692,12 @@ static void run_request_queue(void)
16921692
#endif
16931693
}
16941694

1695-
int main(int argc, char **argv)
1695+
int cmd_main(int argc, const char **argv)
16961696
{
16971697
struct transfer_request *request;
16981698
struct transfer_request *next_request;
16991699
int nr_refspec = 0;
1700-
char **refspec = NULL;
1700+
const char **refspec = NULL;
17011701
struct remote_lock *ref_lock = NULL;
17021702
struct remote_lock *info_ref_lock = NULL;
17031703
struct rev_info revs;
@@ -1717,7 +1717,7 @@ int main(int argc, char **argv)
17171717

17181718
argv++;
17191719
for (i = 1; i < argc; i++, argv++) {
1720-
char *arg = *argv;
1720+
const char *arg = *argv;
17211721

17221722
if (*arg == '-') {
17231723
if (!strcmp(arg, "--all")) {

imap-send.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ static int curl_append_msgs_to_imap(struct imap_server_conf *server,
14941494
}
14951495
#endif
14961496

1497-
int main(int argc, char **argv)
1497+
int cmd_main(int argc, const char **argv)
14981498
{
14991499
struct strbuf all_msgs = STRBUF_INIT;
15001500
int total;

remote-curl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ static void parse_push(struct strbuf *buf)
984984
free(specs);
985985
}
986986

987-
int main(int argc, const char **argv)
987+
int cmd_main(int argc, const char **argv)
988988
{
989989
struct strbuf buf = STRBUF_INIT;
990990
int nongit;

remote-testsvn.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ static int do_command(struct strbuf *line)
284284
return 0;
285285
}
286286

287-
int main(int argc, char **argv)
287+
int cmd_main(int argc, const char **argv)
288288
{
289289
struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
290290
private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT,

sh-i18n--envsubst.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static void note_variables (const char *string);
6464
static void subst_from_stdin (void);
6565

6666
int
67-
main (int argc, char *argv[])
67+
cmd_main (int argc, const char *argv[])
6868
{
6969
/* Default values for command line options. */
7070
/* unsigned short int show_variables = 0; */

shell.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static struct commands {
138138
{ NULL },
139139
};
140140

141-
int main(int argc, char **argv)
141+
int cmd_main(int argc, const char **argv)
142142
{
143143
char *prog;
144144
const char **user_argv;

show-index.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
static const char show_index_usage[] =
55
"git show-index";
66

7-
int main(int argc, char **argv)
7+
int cmd_main(int argc, const char **argv)
88
{
99
int i;
1010
unsigned nr;

test-chmtime.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static int timespec_arg(const char *arg, long int *set_time, int *set_eq)
5656
return 1;
5757
}
5858

59-
int main(int argc, char *argv[])
59+
int cmd_main(int argc, const char **argv)
6060
{
6161
static int verbose;
6262

test-config.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434

3535

36-
int main(int argc, char **argv)
36+
int cmd_main(int argc, const char **argv)
3737
{
3838
int i, val;
3939
const char *v;

test-ctype.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static int is_in(const char *s, int ch)
2828
#define LOWER "abcdefghijklmnopqrstuvwxyz"
2929
#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3030

31-
int main(int argc, char **argv)
31+
int cmd_main(int argc, const char **argv)
3232
{
3333
TEST_CLASS(isdigit, DIGIT);
3434
TEST_CLASS(isspace, " \n\r\t");

test-date.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ static const char *usage_msg = "\n"
55
" test-date parse [date]...\n"
66
" test-date approxidate [date]...\n";
77

8-
static void show_dates(char **argv, struct timeval *now)
8+
static void show_dates(const char **argv, struct timeval *now)
99
{
1010
struct strbuf buf = STRBUF_INIT;
1111

@@ -17,7 +17,7 @@ static void show_dates(char **argv, struct timeval *now)
1717
strbuf_release(&buf);
1818
}
1919

20-
static void parse_dates(char **argv, struct timeval *now)
20+
static void parse_dates(const char **argv, struct timeval *now)
2121
{
2222
struct strbuf result = STRBUF_INIT;
2323

@@ -36,7 +36,7 @@ static void parse_dates(char **argv, struct timeval *now)
3636
strbuf_release(&result);
3737
}
3838

39-
static void parse_approxidate(char **argv, struct timeval *now)
39+
static void parse_approxidate(const char **argv, struct timeval *now)
4040
{
4141
for (; *argv; argv++) {
4242
time_t t;
@@ -45,7 +45,7 @@ static void parse_approxidate(char **argv, struct timeval *now)
4545
}
4646
}
4747

48-
int main(int argc, char **argv)
48+
int cmd_main(int argc, const char **argv)
4949
{
5050
struct timeval now;
5151
const char *x;

test-delta.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
static const char usage_str[] =
1616
"test-delta (-d|-p) <from_file> <data_file> <out_file>";
1717

18-
int main(int argc, char *argv[])
18+
int cmd_main(int argc, const char **argv)
1919
{
2020
int fd;
2121
struct stat st;

test-dump-cache-tree.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static int dump_cache_tree(struct cache_tree *it,
5454
return errs;
5555
}
5656

57-
int main(int ac, char **av)
57+
int cmd_main(int ac, const char **av)
5858
{
5959
struct index_state istate;
6060
struct cache_tree *another = cache_tree();

test-dump-split-index.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ static void show_bit(size_t pos, void *data)
77
printf(" %d", (int)pos);
88
}
99

10-
int main(int ac, char **av)
10+
int cmd_main(int ac, const char **av)
1111
{
1212
struct split_index *si;
1313
int i;

test-dump-untracked-cache.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void dump(struct untracked_cache_dir *ucd, struct strbuf *base)
4040
strbuf_setlen(base, len);
4141
}
4242

43-
int main(int ac, char **av)
43+
int cmd_main(int ac, const char **av)
4444
{
4545
struct untracked_cache *uc;
4646
struct strbuf base = STRBUF_INIT;

test-fake-ssh.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "run-command.h"
33
#include "strbuf.h"
44

5-
int main(int argc, char **argv)
5+
int cmd_main(int argc, const char **argv)
66
{
77
const char *trash_directory = getenv("TRASH_DIRECTORY");
88
struct strbuf buf = STRBUF_INIT;

test-genrandom.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "git-compat-util.h"
88

9-
int main(int argc, char *argv[])
9+
int cmd_main(int argc, const char **argv)
1010
{
1111
unsigned long count, next = 0;
1212
unsigned char *c;

test-hashmap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
138138
*
139139
* perfhashmap method rounds -> test hashmap.[ch] performance
140140
*/
141-
int main(int argc, char *argv[])
141+
int cmd_main(int argc, const char **argv)
142142
{
143143
char line[1024];
144144
struct hashmap map;

test-index-version.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "cache.h"
22

3-
int main(int argc, char **argv)
3+
int cmd_main(int argc, const char **argv)
44
{
55
struct cache_header hdr;
66
int version;

test-line-buffer.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void handle_line(const char *line, struct line_buffer *stdin_buf)
5050
handle_command(line, arg + 1, stdin_buf);
5151
}
5252

53-
int main(int argc, char *argv[])
53+
int cmd_main(int argc, const char **argv)
5454
{
5555
struct line_buffer stdin_buf = LINE_BUFFER_INIT;
5656
struct line_buffer file_buf = LINE_BUFFER_INIT;

0 commit comments

Comments
 (0)