Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the entry point for git?

Tags:

git

c

I was browsing through the git source code, and I was wondering where the entry point file is? I have gone through a couple files, that I thought would be it but could not find a main function.

like image 901
m.. Avatar asked Oct 26 '25 09:10

m..


1 Answers

I could be wrong, but I believe the entrypoint is main() in common-main.c.

int main(int argc, const char **argv)
{
    /*
     * Always open file descriptors 0/1/2 to avoid clobbering files
     * in die().  It also avoids messing up when the pipes are dup'ed
     * onto stdin/stdout/stderr in the child processes we spawn.
     */
    sanitize_stdfds();

    git_setup_gettext();

    git_extract_argv0_path(argv[0]);

    restore_sigpipe_to_default();

    return cmd_main(argc, argv);
}

At the end you can see it returns cmd_main(argc, argv). There are a number of definitions of cmd_main(), but I believe the one returned here is the one defined in git.c, which is a bit long to post here in its entirety, but is excerpted below:

int cmd_main(int argc, const char **argv)
{
    const char *cmd;

    cmd = argv[0];
    if (!cmd)
        cmd = "git-help";
    else {
        const char *slash = find_last_dir_sep(cmd);
        if (slash)
            cmd = slash + 1;
    }

    /*
     * "git-xxxx" is the same as "git xxxx", but we obviously:
     *
     *  - cannot take flags in between the "git" and the "xxxx".
     *  - cannot execute it externally (since it would just do
     *    the same thing over again)
     *
     * So we just directly call the builtin handler, and die if
     * that one cannot handle it.
     */
    if (skip_prefix(cmd, "git-", &cmd)) {
        argv[0] = cmd;
        handle_builtin(argc, argv);
        die("cannot handle %s as a builtin", cmd);
    }

handle_builtin() is also defined in git.c.

like image 96
Dan Lowe Avatar answered Oct 28 '25 23:10

Dan Lowe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!