Remove old generated files from .gitignore.
[gitweb.git] / builtin-commit.c
index 6c2dc390c451d5942521ddef9f046c5f98097dc3..96410de55ddbc52eb116049b4f6a2792a5386cb4 100644 (file)
@@ -46,7 +46,7 @@ static enum {
 static char *logfile, *force_author, *template_file;
 static char *edit_message, *use_message;
 static int all, edit_flag, also, interactive, only, amend, signoff;
-static int quiet, verbose, untracked_files, no_verify;
+static int quiet, verbose, untracked_files, no_verify, allow_empty;
 
 static int no_edit, initial_commit, in_merge;
 const char *only_include_assumed;
@@ -87,6 +87,7 @@ static struct option builtin_commit_options[] = {
        OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
        OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
        OPT_BOOLEAN(0, "untracked-files", &untracked_files, "show all untracked files"),
+       OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
 
        OPT_END()
 };
@@ -162,7 +163,7 @@ static void add_remove_files(struct path_list *list)
        }
 }
 
-static char *prepare_index(const char **files, const char *prefix)
+static char *prepare_index(int argc, const char **argv, const char *prefix)
 {
        int fd;
        struct tree *tree;
@@ -170,7 +171,7 @@ static char *prepare_index(const char **files, const char *prefix)
        const char **pathspec = NULL;
 
        if (interactive) {
-               interactive_add();
+               interactive_add(argc, argv, prefix);
                commit_style = COMMIT_AS_IS;
                return get_index_file();
        }
@@ -178,8 +179,8 @@ static char *prepare_index(const char **files, const char *prefix)
        if (read_cache() < 0)
                die("index file corrupt");
 
-       if (*files)
-               pathspec = get_pathspec(prefix, files);
+       if (*argv)
+               pathspec = get_pathspec(prefix, argv);
 
        /*
         * Non partial, non as-is commit.
@@ -279,12 +280,13 @@ static char *prepare_index(const char **files, const char *prefix)
        return false_lock.filename;
 }
 
-static int run_status(FILE *fp, const char *index_file, const char *prefix)
+static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn)
 {
        struct wt_status s;
 
        wt_status_prepare(&s);
-       s.prefix = prefix;
+       if (wt_status_relative_paths)
+               s.prefix = prefix;
 
        if (amend) {
                s.amend = 1;
@@ -294,6 +296,7 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix)
        s.untracked = untracked_files;
        s.index_file = index_file;
        s.fp = fp;
+       s.nowarn = nowarn;
 
        wt_status_print(&s);
 
@@ -372,6 +375,7 @@ static int prepare_log_message(const char *index_file, const char *prefix)
        if (no_edit) {
                struct rev_info rev;
                unsigned char sha1[40];
+               const char *parent = "HEAD";
 
                fclose(fp);
 
@@ -381,9 +385,12 @@ static int prepare_log_message(const char *index_file, const char *prefix)
                if (get_sha1("HEAD", sha1) != 0)
                        return !!active_nr;
 
+               if (amend)
+                       parent = "HEAD^1";
+
                init_revisions(&rev, "");
                rev.abbrev = 0;
-               setup_revisions(0, NULL, &rev, "HEAD");
+               setup_revisions(0, NULL, &rev, parent);
                DIFF_OPT_SET(&rev.diffopt, QUIET);
                DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
                run_diff_index(&rev, 1 /* cached */);
@@ -410,7 +417,7 @@ static int prepare_log_message(const char *index_file, const char *prefix)
 
        saved_color_setting = wt_status_use_color;
        wt_status_use_color = 0;
-       commitable = run_status(fp, index_file, prefix);
+       commitable = run_status(fp, index_file, prefix, 1);
        wt_status_use_color = saved_color_setting;
 
        fclose(fp);
@@ -495,7 +502,7 @@ static void determine_author_info(struct strbuf *sb)
                email = xstrndup(lb + 2, rb - (lb + 2));
        }
 
-       strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, 1));
+       strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, IDENT_ERROR_ON_NO_NAME));
 }
 
 static int parse_and_validate_options(int argc, const char *argv[],
@@ -534,7 +541,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
                die("Option -m cannot be combined with -c/-C/-F.");
        if (edit_message)
                use_message = edit_message;
-       if (amend)
+       if (amend && !use_message)
                use_message = "HEAD";
        if (use_message) {
                unsigned char sha1[20];
@@ -602,9 +609,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 
        argc = parse_and_validate_options(argc, argv, builtin_status_usage);
 
-       index_file = prepare_index(argv, prefix);
+       index_file = prepare_index(argc, argv, prefix);
 
-       commitable = run_status(stdout, index_file, prefix);
+       commitable = run_status(stdout, index_file, prefix, 0);
 
        rollback_index_files();
 
@@ -658,12 +665,20 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
        rev.verbose_header = 1;
        rev.show_root_diff = 1;
        rev.commit_format = get_commit_format("format:%h: %s");
-       rev.always_show_header = 1;
+       rev.always_show_header = 0;
+       rev.diffopt.detect_rename = 1;
+       rev.diffopt.rename_limit = 100;
+       rev.diffopt.break_opt = 0;
+       diff_setup_done(&rev.diffopt);
 
        printf("Created %scommit ", initial_commit ? "initial " : "");
 
-       log_tree_commit(&rev, commit);
-       printf("\n");
+       if (!log_tree_commit(&rev, commit)) {
+               struct strbuf buf = STRBUF_INIT;
+               format_commit_message(commit, "%h: %s", &buf);
+               printf("%s\n", buf.buf);
+               strbuf_release(&buf);
+       }
 }
 
 int git_commit_config(const char *k, const char *v)
@@ -702,7 +717,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
        argc = parse_and_validate_options(argc, argv, builtin_commit_usage);
 
-       index_file = prepare_index(argv, prefix);
+       index_file = prepare_index(argc, argv, prefix);
 
        if (!no_verify && run_hook(index_file, "pre-commit", NULL)) {
                rollback_index_files();
@@ -710,8 +725,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        }
 
        if (!prepare_log_message(index_file, prefix) && !in_merge &&
-           !(amend && is_a_merge(head_sha1))) {
-               run_status(stdout, index_file, prefix);
+           !allow_empty && !(amend && is_a_merge(head_sha1))) {
+               run_status(stdout, index_file, prefix, 0);
                rollback_index_files();
                unlink(commit_editmsg);
                return 1;
@@ -774,7 +789,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        }
 
        determine_author_info(&sb);
-       strbuf_addf(&sb, "committer %s\n", git_committer_info(1));
+       strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
        if (!is_encoding_utf8(git_commit_encoding))
                strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
        strbuf_addch(&sb, '\n');
@@ -785,15 +800,17 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
                char index[PATH_MAX];
                const char *env[2] = { index, NULL };
                snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
-               launch_editor(git_path(commit_editmsg), &sb, env);
-       } else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
-               rollback_index_files();
-               die("could not read commit message");
+               launch_editor(git_path(commit_editmsg), NULL, env);
        }
-       if (run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
+       if (!no_verify &&
+           run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
                rollback_index_files();
                exit(1);
        }
+       if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
+               rollback_index_files();
+               die("could not read commit message");
+       }
 
        /* Truncate the message just before the diff, if any. */
        p = strstr(sb.buf, "\ndiff --git a/");