Merge branch 'sb/misc-fixes'
authorJunio C Hamano <gitster@pobox.com>
Thu, 25 Jul 2013 02:20:58 +0000 (19:20 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 25 Jul 2013 02:20:59 +0000 (19:20 -0700)
Assorted code cleanups and a minor fix.

* sb/misc-fixes:
diff.c: Do not initialize a variable, which gets reassigned anyway.
commit: Fix a memory leak in determine_author_info
daemon.c:handle: Remove unneeded check for null pointer.

1  2 
builtin/commit.c
daemon.c
diff.c
diff --combined builtin/commit.c
index 65cf2a79b71021cc456634a52bc8ba6f73260bc1,0575ad8ecd3100246156ac107269bde955dc5efc..003bd7dbec1acfda36f4d4bc5e9fb64a276ad049
@@@ -29,7 -29,6 +29,7 @@@
  #include "gpg-interface.h"
  #include "column.h"
  #include "sequencer.h"
 +#include "notes-utils.h"
  
  static const char * const builtin_commit_usage[] = {
        N_("git commit [options] [--] <pathspec>..."),
@@@ -108,27 -107,23 +108,27 @@@ static const char *cleanup_arg
  
  static enum commit_whence whence;
  static int use_editor = 1, include_status = 1;
 -static int show_ignored_in_status;
 +static int show_ignored_in_status, have_option_m;
  static const char *only_include_assumed;
  static struct strbuf message = STRBUF_INIT;
  
 -static enum {
 +static enum status_format {
        STATUS_FORMAT_NONE = 0,
        STATUS_FORMAT_LONG,
        STATUS_FORMAT_SHORT,
 -      STATUS_FORMAT_PORCELAIN
 -} status_format;
 +      STATUS_FORMAT_PORCELAIN,
 +
 +      STATUS_FORMAT_UNSPECIFIED
 +} status_format = STATUS_FORMAT_UNSPECIFIED;
  
  static int opt_parse_m(const struct option *opt, const char *arg, int unset)
  {
        struct strbuf *buf = opt->value;
 -      if (unset)
 +      if (unset) {
 +              have_option_m = 0;
                strbuf_setlen(buf, 0);
 -      else {
 +      } else {
 +              have_option_m = 1;
                if (buf->len)
                        strbuf_addch(buf, '\n');
                strbuf_addstr(buf, arg);
@@@ -207,7 -202,7 +207,7 @@@ static int list_paths(struct string_lis
        }
  
        for (i = 0; i < active_nr; i++) {
 -              struct cache_entry *ce = active_cache[i];
 +              const struct cache_entry *ce = active_cache[i];
                struct string_list_item *item;
  
                if (ce->ce_flags & CE_UPDATE)
@@@ -462,9 -457,6 +462,9 @@@ static int run_status(FILE *fp, const c
        case STATUS_FORMAT_PORCELAIN:
                wt_porcelain_print(s);
                break;
 +      case STATUS_FORMAT_UNSPECIFIED:
 +              die("BUG: finalize_deferred_config() should have been called");
 +              break;
        case STATUS_FORMAT_NONE:
        case STATUS_FORMAT_LONG:
                wt_status_print(s);
@@@ -534,7 -526,6 +534,6 @@@ static void determine_author_info(struc
                                        (lb - strlen(" ") -
                                         (a + strlen("\nauthor "))));
                email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<")));
-               date = xmemdupz(rb + strlen("> "), eol - (rb + strlen("> ")));
                len = eol - (rb + strlen("> "));
                date = xmalloc(len + 2);
                *date = '@';
@@@ -710,7 -701,7 +709,7 @@@ static int prepare_to_commit(const cha
                        previous = eol;
                }
  
 -              append_signoff(&sb, ignore_footer);
 +              append_signoff(&sb, ignore_footer, 0);
        }
  
        if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
@@@ -963,43 -954,7 +962,43 @@@ static const char *read_commit_message(
        if (!commit)
                die(_("could not lookup commit %s"), name);
        out_enc = get_commit_output_encoding();
 -      return logmsg_reencode(commit, out_enc);
 +      return logmsg_reencode(commit, NULL, out_enc);
 +}
 +
 +/*
 + * Enumerate what needs to be propagated when --porcelain
 + * is not in effect here.
 + */
 +static struct status_deferred_config {
 +      enum status_format status_format;
 +      int show_branch;
 +} status_deferred_config = {
 +      STATUS_FORMAT_UNSPECIFIED,
 +      -1 /* unspecified */
 +};
 +
 +static void finalize_deferred_config(struct wt_status *s)
 +{
 +      int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
 +                                 !s->null_termination);
 +
 +      if (s->null_termination) {
 +              if (status_format == STATUS_FORMAT_NONE ||
 +                  status_format == STATUS_FORMAT_UNSPECIFIED)
 +                      status_format = STATUS_FORMAT_PORCELAIN;
 +              else if (status_format == STATUS_FORMAT_LONG)
 +                      die(_("--long and -z are incompatible"));
 +      }
 +
 +      if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
 +              status_format = status_deferred_config.status_format;
 +      if (status_format == STATUS_FORMAT_UNSPECIFIED)
 +              status_format = STATUS_FORMAT_NONE;
 +
 +      if (use_deferred_config && s->show_branch < 0)
 +              s->show_branch = status_deferred_config.show_branch;
 +      if (s->show_branch < 0)
 +              s->show_branch = 0;
  }
  
  static int parse_and_validate_options(int argc, const char *argv[],
        int f = 0;
  
        argc = parse_options(argc, argv, prefix, options, usage, 0);
 +      finalize_deferred_config(s);
  
        if (force_author && !strchr(force_author, '>'))
                force_author = find_author_by_nickname(force_author);
        if (force_author && renew_authorship)
                die(_("Using both --reset-author and --author does not make sense"));
  
 -      if (logfile || message.len || use_message || fixup_message)
 +      if (logfile || have_option_m || use_message || fixup_message)
                use_editor = 0;
        if (0 <= edit_flag)
                use_editor = edit_flag;
        if (all && argc > 0)
                die(_("Paths with -a does not make sense."));
  
 -      if (s->null_termination) {
 -              if (status_format == STATUS_FORMAT_NONE)
 -                      status_format = STATUS_FORMAT_PORCELAIN;
 -              else if (status_format == STATUS_FORMAT_LONG)
 -                      die(_("--long and -z are incompatible"));
 -      }
        if (status_format != STATUS_FORMAT_NONE)
                dry_run = 1;
  
@@@ -1149,17 -1109,6 +1148,17 @@@ static int git_status_config(const cha
                        s->submodule_summary = -1;
                return 0;
        }
 +      if (!strcmp(k, "status.short")) {
 +              if (git_config_bool(k, v))
 +                      status_deferred_config.status_format = STATUS_FORMAT_SHORT;
 +              else
 +                      status_deferred_config.status_format = STATUS_FORMAT_NONE;
 +              return 0;
 +      }
 +      if (!strcmp(k, "status.branch")) {
 +              status_deferred_config.show_branch = git_config_bool(k, v);
 +              return 0;
 +      }
        if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
                s->use_color = git_config_colorbool(k, v);
                return 0;
@@@ -1202,8 -1151,8 +1201,8 @@@ int cmd_status(int argc, const char **a
                OPT__VERBOSE(&verbose, N_("be verbose")),
                OPT_SET_INT('s', "short", &status_format,
                            N_("show status concisely"), STATUS_FORMAT_SHORT),
 -              OPT_BOOLEAN('b', "branch", &s.show_branch,
 -                          N_("show branch information")),
 +              OPT_BOOL('b', "branch", &s.show_branch,
 +                       N_("show branch information")),
                OPT_SET_INT(0, "porcelain", &status_format,
                            N_("machine-readable output"),
                            STATUS_FORMAT_PORCELAIN),
                             builtin_status_options,
                             builtin_status_usage, 0);
        finalize_colopts(&s.colopts, -1);
 -
 -      if (s.null_termination) {
 -              if (status_format == STATUS_FORMAT_NONE)
 -                      status_format = STATUS_FORMAT_PORCELAIN;
 -              else if (status_format == STATUS_FORMAT_LONG)
 -                      die(_("--long and -z are incompatible"));
 -      }
 +      finalize_deferred_config(&s);
  
        handle_untracked_files_arg(&s);
        if (show_ignored_in_status)
        case STATUS_FORMAT_PORCELAIN:
                wt_porcelain_print(&s);
                break;
 +      case STATUS_FORMAT_UNSPECIFIED:
 +              die("BUG: finalize_deferred_config() should have been called");
 +              break;
        case STATUS_FORMAT_NONE:
        case STATUS_FORMAT_LONG:
                s.verbose = verbose;
@@@ -1436,7 -1388,7 +1435,7 @@@ int cmd_commit(int argc, const char **a
                OPT_BOOLEAN(0, "dry-run", &dry_run, N_("show what would be committed")),
                OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
                            STATUS_FORMAT_SHORT),
 -              OPT_BOOLEAN(0, "branch", &s.show_branch, N_("show branch information")),
 +              OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
                OPT_SET_INT(0, "porcelain", &status_format,
                            N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
                OPT_SET_INT(0, "long", &status_format,
        wt_status_prepare(&s);
        gitmodules_config();
        git_config(git_commit_config, &s);
 +      status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
        determine_whence(&s);
        s.colopts = 0;
  
                if (cfg) {
                        /* we are amending, so current_head is not NULL */
                        copy_note_for_rewrite(cfg, current_head->object.sha1, sha1);
 -                      finish_copy_notes_for_rewrite(cfg);
 +                      finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'");
                }
                run_rewrite_hook(current_head->object.sha1, sha1);
        }
diff --combined daemon.c
index 973ec38fafd6e679eeebc7c013163fbefe57ec05,80df5cb54ef63620faa88096ca6b7a02a8c43bf8..34916c5e105812e36d659dfeeb8341e003e746bd
+++ b/daemon.c
@@@ -600,7 -600,7 +600,7 @@@ static void parse_host_arg(char *extra_
  
  static int execute(void)
  {
 -      static char line[1000];
 +      char *line = packet_buffer;
        int pktlen, len, i;
        char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT");
  
                loginfo("Connection from %s:%s", addr, port);
  
        alarm(init_timeout ? init_timeout : timeout);
 -      pktlen = packet_read_line(0, line, sizeof(line));
 +      pktlen = packet_read(0, NULL, NULL, packet_buffer, sizeof(packet_buffer), 0);
        alarm(0);
  
        len = strlen(line);
@@@ -760,7 -760,7 +760,7 @@@ static void handle(int incoming, struc
                snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d",
                    ntohs(sin_addr->sin_port));
  #ifndef NO_IPV6
-       } else if (addr && addr->sa_family == AF_INET6) {
+       } else if (addr->sa_family == AF_INET6) {
                struct sockaddr_in6 *sin6_addr = (void *) addr;
  
                char *buf = addrbuf + 12;
@@@ -1047,6 -1047,18 +1047,6 @@@ static int service_loop(struct socketli
        }
  }
  
 -/* if any standard file descriptor is missing open it to /dev/null */
 -static void sanitize_stdfds(void)
 -{
 -      int fd = open("/dev/null", O_RDWR, 0);
 -      while (fd != -1 && fd < 2)
 -              fd = dup(fd);
 -      if (fd == -1)
 -              die_errno("open /dev/null or dup failed");
 -      if (fd > 2)
 -              close(fd);
 -}
 -
  #ifdef NO_POSIX_GOODIES
  
  struct credentials;
diff --combined diff.c
index a4583f01107c4534c1939ffcd5effcb959ea732e,2747e6f72ce97e24ade88a620b4b95ae6f6b5cb3..4629de2092ee7eca1b6aadc892396acc8af46d48
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -1683,9 -1683,7 +1683,7 @@@ static void show_stats(struct diffstat_
                del = deleted;
  
                if (graph_width <= max_change) {
-                       int total = add + del;
-                       total = scale_linear(add + del, graph_width, max_change);
+                       int total = scale_linear(add + del, graph_width, max_change);
                        if (total < 2 && add && del)
                                /* width >= 2 due to the sanity check */
                                total = 2;
@@@ -2255,7 -2253,6 +2253,7 @@@ static void builtin_diff(const char *na
                const char *del = diff_get_color_opt(o, DIFF_FILE_OLD);
                const char *add = diff_get_color_opt(o, DIFF_FILE_NEW);
                show_submodule_summary(o->file, one ? one->path : two->path,
 +                              line_prefix,
                                one->sha1, two->sha1, two->dirty_submodule,
                                meta, del, add, reset);
                return;
@@@ -2586,7 -2583,7 +2584,7 @@@ void fill_filespec(struct diff_filespe
   */
  static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
  {
 -      struct cache_entry *ce;
 +      const struct cache_entry *ce;
        struct stat st;
        int pos, len;
  
@@@ -2677,14 -2674,6 +2675,14 @@@ static int diff_populate_gitlink(struc
  int diff_populate_filespec(struct diff_filespec *s, int size_only)
  {
        int err = 0;
 +      /*
 +       * demote FAIL to WARN to allow inspecting the situation
 +       * instead of refusing.
 +       */
 +      enum safe_crlf crlf_warn = (safe_crlf == SAFE_CRLF_FAIL
 +                                  ? SAFE_CRLF_WARN
 +                                  : safe_crlf);
 +
        if (!DIFF_FILE_VALID(s))
                die("internal error: asking to populate invalid file.");
        if (S_ISDIR(s->mode))
                /*
                 * Convert from working tree format to canonical git format
                 */
 -              if (convert_to_git(s->path, s->data, s->size, &buf, safe_crlf)) {
 +              if (convert_to_git(s->path, s->data, s->size, &buf, crlf_warn)) {
                        size_t size = 0;
                        munmap(s->data, s->size);
                        s->should_munmap = 0;
@@@ -3505,11 -3494,6 +3503,11 @@@ static int parse_submodule_opt(struct d
        return 1;
  }
  
 +static void enable_patch_output(int *fmt) {
 +      *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
 +      *fmt |= DIFF_FORMAT_PATCH;
 +}
 +
  int diff_opt_parse(struct diff_options *options, const char **av, int ac)
  {
        const char *arg = av[0];
        int argcount;
  
        /* Output format options */
 -      if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch"))
 -              options->output_format |= DIFF_FORMAT_PATCH;
 -      else if (opt_arg(arg, 'U', "unified", &options->context))
 -              options->output_format |= DIFF_FORMAT_PATCH;
 +      if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
 +          || opt_arg(arg, 'U', "unified", &options->context))
 +              enable_patch_output(&options->output_format);
        else if (!strcmp(arg, "--raw"))
                options->output_format |= DIFF_FORMAT_RAW;
 -      else if (!strcmp(arg, "--patch-with-raw"))
 -              options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
 -      else if (!strcmp(arg, "--numstat"))
 +      else if (!strcmp(arg, "--patch-with-raw")) {
 +              enable_patch_output(&options->output_format);
 +              options->output_format |= DIFF_FORMAT_RAW;
 +      } else if (!strcmp(arg, "--numstat"))
                options->output_format |= DIFF_FORMAT_NUMSTAT;
        else if (!strcmp(arg, "--shortstat"))
                options->output_format |= DIFF_FORMAT_SHORTSTAT;
                options->output_format |= DIFF_FORMAT_CHECKDIFF;
        else if (!strcmp(arg, "--summary"))
                options->output_format |= DIFF_FORMAT_SUMMARY;
 -      else if (!strcmp(arg, "--patch-with-stat"))
 -              options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
 -      else if (!strcmp(arg, "--name-only"))
 +      else if (!strcmp(arg, "--patch-with-stat")) {
 +              enable_patch_output(&options->output_format);
 +              options->output_format |= DIFF_FORMAT_DIFFSTAT;
 +      } else if (!strcmp(arg, "--name-only"))
                options->output_format |= DIFF_FORMAT_NAME;
        else if (!strcmp(arg, "--name-status"))
                options->output_format |= DIFF_FORMAT_NAME_STATUS;
 -      else if (!strcmp(arg, "-s"))
 +      else if (!strcmp(arg, "-s") || !strcmp(arg, "--no-patch"))
                options->output_format |= DIFF_FORMAT_NO_OUTPUT;
        else if (!prefixcmp(arg, "--stat"))
                /* --stat, --stat-width, --stat-name-width, or --stat-count */
                DIFF_XDL_SET(options, IGNORE_WHITESPACE_CHANGE);
        else if (!strcmp(arg, "--ignore-space-at-eol"))
                DIFF_XDL_SET(options, IGNORE_WHITESPACE_AT_EOL);
 +      else if (!strcmp(arg, "--ignore-blank-lines"))
 +              DIFF_XDL_SET(options, IGNORE_BLANK_LINES);
        else if (!strcmp(arg, "--patience"))
                options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
        else if (!strcmp(arg, "--histogram"))
  
        /* flags options */
        else if (!strcmp(arg, "--binary")) {
 -              options->output_format |= DIFF_FORMAT_PATCH;
 +              enable_patch_output(&options->output_format);
                DIFF_OPT_SET(options, BINARY);
        }
        else if (!strcmp(arg, "--full-index"))