Merge branch 'master' into lt/logopt
[gitweb.git] / git.c
diff --git a/git.c b/git.c
index 993cd0d4904be11edb6234e47d10376733928740..a6cfd1d8aaf13bf391c69c7f91b14fd68e0d36e9 100644 (file)
--- a/git.c
+++ b/git.c
 #include <sys/ioctl.h>
 #include "git-compat-util.h"
 #include "exec_cmd.h"
+#include "common-cmds.h"
+
+#include "cache.h"
+#include "commit.h"
+#include "diff.h"
+#include "revision.h"
+#include "log-tree.h"
 
 #ifndef PATH_MAX
 # define PATH_MAX 4096
@@ -167,11 +174,29 @@ static void list_commands(const char *exec_path, const char *pattern)
        putchar('\n');
 }
 
+static void list_common_cmds_help(void)
+{
+       int i, longest = 0;
+
+       for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
+               if (longest < strlen(common_cmds[i].name))
+                       longest = strlen(common_cmds[i].name);
+       }
+
+       puts("The most commonly used git commands are:");
+       for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
+               printf("    %s", common_cmds[i].name);
+               mput_char(' ', longest - strlen(common_cmds[i].name) + 4);
+               puts(common_cmds[i].help);
+       }
+       puts("(use 'git help -a' to get a list of all installed git commands)");
+}
+
 #ifdef __GNUC__
-static void cmd_usage(const char *exec_path, const char *fmt, ...)
-       __attribute__((__format__(__printf__, 2, 3), __noreturn__));
+static void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
+       __attribute__((__format__(__printf__, 3, 4), __noreturn__));
 #endif
-static void cmd_usage(const char *exec_path, const char *fmt, ...)
+static void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
 {
        if (fmt) {
                va_list ap;
@@ -185,10 +210,13 @@ static void cmd_usage(const char *exec_path, const char *fmt, ...)
        else
                puts(git_usage);
 
-       putchar('\n');
-
-       if(exec_path)
-               list_commands(exec_path, "git-*");
+       if (exec_path) {
+               putchar('\n');
+               if (show_all)
+                       list_commands(exec_path, "git-*");
+               else
+                       list_common_cmds_help();
+        }
 
        exit(1);
 }
@@ -212,53 +240,172 @@ static void prepend_to_path(const char *dir, int len)
        setenv("PATH", path, 1);
 }
 
-static void show_man_page(char *git_cmd)
+static void show_man_page(const char *git_cmd)
 {
-       char *page;
+       const char *page;
 
        if (!strncmp(git_cmd, "git", 3))
                page = git_cmd;
        else {
                int page_len = strlen(git_cmd) + 4;
-
-               page = malloc(page_len + 1);
-               strcpy(page, "git-");
-               strcpy(page + 4, git_cmd);
-               page[page_len] = 0;
+               char *p = malloc(page_len + 1);
+               strcpy(p, "git-");
+               strcpy(p + 4, git_cmd);
+               p[page_len] = 0;
+               page = p;
        }
 
        execlp("man", "man", page, NULL);
 }
 
-static int cmd_version(int argc, char **argv, char **envp)
+static int cmd_version(int argc, const char **argv, char **envp)
 {
        printf("git version %s\n", GIT_VERSION);
        return 0;
 }
 
-static int cmd_help(int argc, char **argv, char **envp)
+static int cmd_help(int argc, const char **argv, char **envp)
 {
-       char *help_cmd = argv[1];
+       const char *help_cmd = argv[1];
        if (!help_cmd)
-               cmd_usage(git_exec_path(), NULL);
-       show_man_page(help_cmd);
+               cmd_usage(0, git_exec_path(), NULL);
+       else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a"))
+               cmd_usage(1, git_exec_path(), NULL);
+       else
+               show_man_page(help_cmd);
        return 0;
 }
 
-#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
+#define LOGSIZE (65536)
 
-static void handle_internal_command(int argc, char **argv, char **envp)
+static int cmd_log_wc(int argc, const char **argv, char **envp,
+                     struct rev_info *rev)
+{
+       struct commit *commit;
+       char *buf = xmalloc(LOGSIZE);
+       const char *commit_prefix = "commit ";
+       int shown = 0;
+
+       if (argc > 1)
+               die("unrecognized argument: %s", argv[1]);
+       if (rev->commit_format == CMIT_FMT_ONELINE)
+               commit_prefix = "";
+
+       prepare_revision_walk(rev);
+       setup_pager();
+       while ((commit = get_revision(rev)) != NULL) {
+               if (shown && rev->diff &&
+                   rev->commit_format != CMIT_FMT_ONELINE)
+                       putchar('\n');
+               fputs(commit_prefix, stdout);
+               if (rev->abbrev_commit && rev->abbrev)
+                       fputs(find_unique_abbrev(commit->object.sha1,
+                                                rev->abbrev),
+                             stdout);
+               else
+                       fputs(sha1_to_hex(commit->object.sha1), stdout);
+               if (rev->parents) {
+                       struct commit_list *parents = commit->parents;
+                       while (parents) {
+                               struct object *o = &(parents->item->object);
+                               parents = parents->next;
+                               if (o->flags & TMP_MARK)
+                                       continue;
+                               printf(" %s", sha1_to_hex(o->sha1));
+                               o->flags |= TMP_MARK;
+                       }
+                       /* TMP_MARK is a general purpose flag that can
+                        * be used locally, but the user should clean
+                        * things up after it is done with them.
+                        */
+                       for (parents = commit->parents;
+                            parents;
+                            parents = parents->next)
+                               parents->item->object.flags &= ~TMP_MARK;
+               }
+               if (rev->commit_format == CMIT_FMT_ONELINE)
+                       putchar(' ');
+               else
+                       putchar('\n');
+               pretty_print_commit(rev->commit_format, commit, ~0, buf,
+                                   LOGSIZE, rev->abbrev);
+               printf("%s\n", buf);
+               if (rev->diff) {
+                       printf("---\n");
+                       log_tree_commit(rev, commit);
+               }
+               shown = 1;
+               free(commit->buffer);
+               commit->buffer = NULL;
+       }
+       free(buf);
+       return 0;
+}
+
+static int cmd_wc(int argc, const char **argv, char **envp)
+{
+       struct rev_info rev;
+
+       init_revisions(&rev);
+       rev.abbrev = DEFAULT_ABBREV;
+       rev.no_commit_id = 1;
+       rev.commit_format = CMIT_FMT_DEFAULT;
+       rev.diff = 1;
+       rev.diffopt.recursive = 1;
+       argc = setup_revisions(argc, argv, &rev, "HEAD");
+       return cmd_log_wc(argc, argv, envp, &rev);
+}
+
+static int cmd_show(int argc, const char **argv, char **envp)
+{
+       struct rev_info rev;
+
+       init_revisions(&rev);
+       rev.diff = 1;
+       rev.ignore_merges = 0;
+       rev.combine_merges = 1;
+       rev.dense_combined_merges = 1;
+       rev.abbrev = DEFAULT_ABBREV;
+       rev.commit_format = CMIT_FMT_DEFAULT;
+       rev.diffopt.recursive = 1;
+       rev.no_walk = 1;
+       argc = setup_revisions(argc, argv, &rev, "HEAD");
+       return cmd_log_wc(argc, argv, envp, &rev);
+}
+
+static int cmd_log(int argc, const char **argv, char **envp)
+{
+       struct rev_info rev;
+
+       init_revisions(&rev);
+       rev.abbrev = DEFAULT_ABBREV;
+       rev.no_commit_id = 1;
+       rev.commit_format = CMIT_FMT_DEFAULT;
+       argc = setup_revisions(argc, argv, &rev, "HEAD");
+       return cmd_log_wc(argc, argv, envp, &rev);
+}
+
+static void handle_internal_command(int argc, const char **argv, char **envp)
 {
        const char *cmd = argv[0];
        static struct cmd_struct {
                const char *cmd;
-               int (*fn)(int, char **, char **);
+               int (*fn)(int, const char **, char **);
        } commands[] = {
                { "version", cmd_version },
                { "help", cmd_help },
+               { "log", cmd_log },
+               { "whatchanged", cmd_wc },
+               { "show", cmd_show },
        };
        int i;
 
+       /* Turn "git cmd --help" into "git help cmd" */
+       if (argc > 1 && !strcmp(argv[1], "--help")) {
+               argv[1] = argv[0];
+               argv[0] = cmd = "help";
+       }
+
        for (i = 0; i < ARRAY_SIZE(commands); i++) {
                struct cmd_struct *p = commands+i;
                if (strcmp(p->cmd, cmd))
@@ -267,9 +414,9 @@ static void handle_internal_command(int argc, char **argv, char **envp)
        }
 }
 
-int main(int argc, char **argv, char **envp)
+int main(int argc, const char **argv, char **envp)
 {
-       char *cmd = argv[0];
+       const char *cmd = argv[0];
        char *slash = strrchr(cmd, '/');
        char git_command[PATH_MAX + 1];
        const char *exec_path = NULL;
@@ -341,7 +488,7 @@ int main(int argc, char **argv, char **envp)
                        puts(git_exec_path());
                        exit(0);
                }
-               cmd_usage(NULL, NULL);
+               cmd_usage(0, NULL, NULL);
        }
        argv[0] = cmd;
 
@@ -364,7 +511,7 @@ int main(int argc, char **argv, char **envp)
        execv_git_cmd(argv);
 
        if (errno == ENOENT)
-               cmd_usage(exec_path, "'%s' is not a git-command", cmd);
+               cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
 
        fprintf(stderr, "Failed to run command '%s': %s\n",
                git_command, strerror(errno));