Merge branch 'jc/revision-dash-count-parsing'
authorJunio C Hamano <gitster@pobox.com>
Fri, 20 Jun 2014 20:10:25 +0000 (13:10 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 20 Jun 2014 20:10:25 +0000 (13:10 -0700)
"git log -2master" is a common typo that shows two commits starting
from whichever random branch that is not 'master' that happens to
be checked out currently.

* jc/revision-dash-count-parsing:
revision: parse "git log -<count>" more carefully

1  2 
revision.c
diff --combined revision.c
index 3818b4628d0b5b4fa74428decaa940bef2804856,d5e2f1a257dfd508bd2a2d1f1fde6c49a03541f5..8351e794df943ea6ea7acde40c01c97c20be4576
@@@ -16,7 -16,6 +16,7 @@@
  #include "line-log.h"
  #include "mailmap.h"
  #include "commit-slab.h"
 +#include "dir.h"
  
  volatile show_early_output_fn_t show_early_output;
  
@@@ -497,14 -496,24 +497,14 @@@ static int rev_compare_tree(struct rev_
  static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
  {
        int retval;
 -      void *tree;
 -      unsigned long size;
 -      struct tree_desc empty, real;
        struct tree *t1 = commit->tree;
  
        if (!t1)
                return 0;
  
 -      tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
 -      if (!tree)
 -              return 0;
 -      init_tree_desc(&real, tree, size);
 -      init_tree_desc(&empty, "", 0);
 -
        tree_difference = REV_TREE_SAME;
        DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
 -      retval = diff_tree(&empty, &real, "", &revs->pruning);
 -      free(tree);
 +      retval = diff_tree_sha1(NULL, t1->object.sha1, "", &revs->pruning);
  
        return retval >= 0 && (tree_difference == REV_TREE_SAME);
  }
@@@ -774,10 -783,6 +774,10 @@@ static int add_parents_to_list(struct r
                return 0;
        commit->object.flags |= ADDED;
  
 +      if (revs->include_check &&
 +          !revs->include_check(commit, revs->include_check_data))
 +              return 0;
 +
        /*
         * If the commit is uninteresting, don't try to
         * prune parents - we want the maximal uninteresting
@@@ -1179,28 -1184,11 +1179,28 @@@ struct all_refs_cb 
        const char *name_for_errormsg;
  };
  
 +int ref_excluded(struct string_list *ref_excludes, const char *path)
 +{
 +      struct string_list_item *item;
 +
 +      if (!ref_excludes)
 +              return 0;
 +      for_each_string_list_item(item, ref_excludes) {
 +              if (!wildmatch(item->string, path, 0, NULL))
 +                      return 1;
 +      }
 +      return 0;
 +}
 +
  static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
  {
        struct all_refs_cb *cb = cb_data;
 -      struct object *object = get_reference(cb->all_revs, path, sha1,
 -                                            cb->all_flags);
 +      struct object *object;
 +
 +      if (ref_excluded(cb->all_revs->ref_excludes, path))
 +          return 0;
 +
 +      object = get_reference(cb->all_revs, path, sha1, cb->all_flags);
        add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
        add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
        return 0;
@@@ -1213,24 -1201,6 +1213,24 @@@ static void init_all_refs_cb(struct all
        cb->all_flags = flags;
  }
  
 +void clear_ref_exclusion(struct string_list **ref_excludes_p)
 +{
 +      if (*ref_excludes_p) {
 +              string_list_clear(*ref_excludes_p, 0);
 +              free(*ref_excludes_p);
 +      }
 +      *ref_excludes_p = NULL;
 +}
 +
 +void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
 +{
 +      if (!*ref_excludes_p) {
 +              *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
 +              (*ref_excludes_p)->strdup_strings = 1;
 +      }
 +      string_list_append(*ref_excludes_p, exclude);
 +}
 +
  static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
                int (*for_each)(const char *, each_ref_fn, void *))
  {
@@@ -1395,7 -1365,7 +1395,7 @@@ static void prepare_show_merge(struct r
                const struct cache_entry *ce = active_cache[i];
                if (!ce_stage(ce))
                        continue;
 -              if (ce_path_match(ce, &revs->prune_data)) {
 +              if (ce_path_match(ce, &revs->prune_data, NULL)) {
                        prune_num++;
                        prune = xrealloc(prune, sizeof(*prune) * prune_num);
                        prune[prune_num-2] = ce->name;
@@@ -1575,10 -1545,6 +1575,10 @@@ static void read_revisions_from_stdin(s
  {
        struct strbuf sb;
        int seen_dashdash = 0;
 +      int save_warning;
 +
 +      save_warning = warn_on_object_refname_ambiguity;
 +      warn_on_object_refname_ambiguity = 0;
  
        strbuf_init(&sb, 1000);
        while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
        }
        if (seen_dashdash)
                read_pathspec_from_stdin(revs, &sb, prune);
 +
        strbuf_release(&sb);
 +      warn_on_object_refname_ambiguity = save_warning;
  }
  
  static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
@@@ -1632,10 -1596,9 +1632,10 @@@ static int handle_revision_opt(struct r
            !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
            !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
            !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
 -          !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
 -          !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
 -          !prefixcmp(arg, "--remotes=") || !prefixcmp(arg, "--no-walk="))
 +          !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
 +          starts_with(arg, "--exclude=") ||
 +          starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
 +          starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
        {
                unkv[(*unkc)++] = arg;
                return 1;
                revs->skip_count = atoi(optarg);
                return argcount;
        } else if ((*arg == '-') && isdigit(arg[1])) {
-       /* accept -<digit>, like traditional "head" */
-               revs->max_count = atoi(arg + 1);
+               /* accept -<digit>, like traditional "head" */
+               if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
+                   revs->max_count < 0)
+                       die("'%s': not a non-negative integer", arg + 1);
                revs->no_walk = 0;
        } else if (!strcmp(arg, "-n")) {
                if (argc <= 1)
                revs->max_count = atoi(argv[1]);
                revs->no_walk = 0;
                return 2;
 -      } else if (!prefixcmp(arg, "-n")) {
 +      } else if (starts_with(arg, "-n")) {
                revs->max_count = atoi(arg + 2);
                revs->no_walk = 0;
        } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
        } else if (!strcmp(arg, "--author-date-order")) {
                revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
                revs->topo_order = 1;
 -      } else if (!prefixcmp(arg, "--early-output")) {
 +      } else if (starts_with(arg, "--early-output")) {
                int count = 100;
                switch (arg[14]) {
                case '=':
                revs->min_parents = 2;
        } else if (!strcmp(arg, "--no-merges")) {
                revs->max_parents = 1;
 -      } else if (!prefixcmp(arg, "--min-parents=")) {
 +      } else if (starts_with(arg, "--min-parents=")) {
                revs->min_parents = atoi(arg+14);
 -      } else if (!prefixcmp(arg, "--no-min-parents")) {
 +      } else if (starts_with(arg, "--no-min-parents")) {
                revs->min_parents = 0;
 -      } else if (!prefixcmp(arg, "--max-parents=")) {
 +      } else if (starts_with(arg, "--max-parents=")) {
                revs->max_parents = atoi(arg+14);
 -      } else if (!prefixcmp(arg, "--no-max-parents")) {
 +      } else if (starts_with(arg, "--no-max-parents")) {
                revs->max_parents = -1;
        } else if (!strcmp(arg, "--boundary")) {
                revs->boundary = 1;
                revs->verify_objects = 1;
        } else if (!strcmp(arg, "--unpacked")) {
                revs->unpacked = 1;
 -      } else if (!prefixcmp(arg, "--unpacked=")) {
 +      } else if (starts_with(arg, "--unpacked=")) {
                die("--unpacked=<packfile> no longer supported.");
        } else if (!strcmp(arg, "-r")) {
                revs->diff = 1;
                revs->verbose_header = 1;
                revs->pretty_given = 1;
                get_commit_format(arg+8, revs);
 -      } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
 +      } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
                /*
                 * Detached form ("--pretty X" as opposed to "--pretty=X")
                 * not allowed, since the argument is optional.
                revs->notes_opt.use_default_notes = 1;
        } else if (!strcmp(arg, "--show-signature")) {
                revs->show_signature = 1;
 -      } else if (!prefixcmp(arg, "--show-notes=") ||
 -                 !prefixcmp(arg, "--notes=")) {
 +      } else if (!strcmp(arg, "--show-linear-break") ||
 +                 starts_with(arg, "--show-linear-break=")) {
 +              if (starts_with(arg, "--show-linear-break="))
 +                      revs->break_bar = xstrdup(arg + 20);
 +              else
 +                      revs->break_bar = "                    ..........";
 +              revs->track_linear = 1;
 +              revs->track_first_time = 1;
 +      } else if (starts_with(arg, "--show-notes=") ||
 +                 starts_with(arg, "--notes=")) {
                struct strbuf buf = STRBUF_INIT;
                revs->show_notes = 1;
                revs->show_notes_given = 1;
 -              if (!prefixcmp(arg, "--show-notes")) {
 +              if (starts_with(arg, "--show-notes")) {
                        if (revs->notes_opt.use_default_notes < 0)
                                revs->notes_opt.use_default_notes = 1;
                        strbuf_addstr(&buf, arg+13);
                revs->abbrev = 0;
        } else if (!strcmp(arg, "--abbrev")) {
                revs->abbrev = DEFAULT_ABBREV;
 -      } else if (!prefixcmp(arg, "--abbrev=")) {
 +      } else if (starts_with(arg, "--abbrev=")) {
                revs->abbrev = strtoul(arg + 9, NULL, 10);
                if (revs->abbrev < MINIMUM_ABBREV)
                        revs->abbrev = MINIMUM_ABBREV;
                        unkv[(*unkc)++] = arg;
                return opts;
        }
 +      if (revs->graph && revs->track_linear)
 +              die("--show-linear-break and --graph are incompatible");
  
        return 1;
  }
@@@ -2020,51 -1975,40 +2022,51 @@@ static int handle_revision_pseudo_opt(c
        if (!strcmp(arg, "--all")) {
                handle_refs(submodule, revs, *flags, for_each_ref_submodule);
                handle_refs(submodule, revs, *flags, head_ref_submodule);
 +              clear_ref_exclusion(&revs->ref_excludes);
        } else if (!strcmp(arg, "--branches")) {
                handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
 +              clear_ref_exclusion(&revs->ref_excludes);
        } else if (!strcmp(arg, "--bisect")) {
                handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
                handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
                revs->bisect = 1;
        } else if (!strcmp(arg, "--tags")) {
                handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
 +              clear_ref_exclusion(&revs->ref_excludes);
        } else if (!strcmp(arg, "--remotes")) {
                handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
 +              clear_ref_exclusion(&revs->ref_excludes);
        } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
                struct all_refs_cb cb;
                init_all_refs_cb(&cb, revs, *flags);
                for_each_glob_ref(handle_one_ref, optarg, &cb);
 +              clear_ref_exclusion(&revs->ref_excludes);
 +              return argcount;
 +      } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
 +              add_ref_exclusion(&revs->ref_excludes, optarg);
                return argcount;
 -      } else if (!prefixcmp(arg, "--branches=")) {
 +      } else if (starts_with(arg, "--branches=")) {
                struct all_refs_cb cb;
                init_all_refs_cb(&cb, revs, *flags);
                for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
 -      } else if (!prefixcmp(arg, "--tags=")) {
 +              clear_ref_exclusion(&revs->ref_excludes);
 +      } else if (starts_with(arg, "--tags=")) {
                struct all_refs_cb cb;
                init_all_refs_cb(&cb, revs, *flags);
                for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
 -      } else if (!prefixcmp(arg, "--remotes=")) {
 +              clear_ref_exclusion(&revs->ref_excludes);
 +      } else if (starts_with(arg, "--remotes=")) {
                struct all_refs_cb cb;
                init_all_refs_cb(&cb, revs, *flags);
                for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
 +              clear_ref_exclusion(&revs->ref_excludes);
        } else if (!strcmp(arg, "--reflog")) {
                handle_reflog(revs, *flags);
        } else if (!strcmp(arg, "--not")) {
                *flags ^= UNINTERESTING | BOTTOM;
        } else if (!strcmp(arg, "--no-walk")) {
                revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
 -      } else if (!prefixcmp(arg, "--no-walk=")) {
 +      } else if (starts_with(arg, "--no-walk=")) {
                /*
                 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
                 * not allowed, since the argument is optional.
@@@ -2913,27 -2857,6 +2915,27 @@@ enum commit_action simplify_commit(stru
        return action;
  }
  
 +static void track_linear(struct rev_info *revs, struct commit *commit)
 +{
 +      if (revs->track_first_time) {
 +              revs->linear = 1;
 +              revs->track_first_time = 0;
 +      } else {
 +              struct commit_list *p;
 +              for (p = revs->previous_parents; p; p = p->next)
 +                      if (p->item == NULL || /* first commit */
 +                          !hashcmp(p->item->object.sha1, commit->object.sha1))
 +                              break;
 +              revs->linear = p != NULL;
 +      }
 +      if (revs->reverse) {
 +              if (revs->linear)
 +                      commit->object.flags |= TRACK_LINEAR;
 +      }
 +      free_commit_list(revs->previous_parents);
 +      revs->previous_parents = copy_commit_list(commit->parents);
 +}
 +
  static struct commit *get_revision_1(struct rev_info *revs)
  {
        if (!revs->commits)
                        if (revs->max_age != -1 &&
                            (commit->date < revs->max_age))
                                continue;
 -                      if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
 -                              die("Failed to traverse parents of commit %s",
 -                                  sha1_to_hex(commit->object.sha1));
 +                      if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
 +                              if (!revs->ignore_missing_links)
 +                                      die("Failed to traverse parents of commit %s",
 +                                              sha1_to_hex(commit->object.sha1));
 +                      }
                }
  
                switch (simplify_commit(revs, commit)) {
                        die("Failed to simplify parents of commit %s",
                            sha1_to_hex(commit->object.sha1));
                default:
 +                      if (revs->track_linear)
 +                              track_linear(revs, commit);
                        return commit;
                }
        } while (revs->commits);
@@@ -3143,23 -3062,14 +3145,23 @@@ struct commit *get_revision(struct rev_
                revs->reverse_output_stage = 1;
        }
  
 -      if (revs->reverse_output_stage)
 -              return pop_commit(&revs->commits);
 +      if (revs->reverse_output_stage) {
 +              c = pop_commit(&revs->commits);
 +              if (revs->track_linear)
 +                      revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
 +              return c;
 +      }
  
        c = get_revision_internal(revs);
        if (c && revs->graph)
                graph_update(revs->graph, c);
 -      if (!c)
 +      if (!c) {
                free_saved_parents(revs);
 +              if (revs->previous_parents) {
 +                      free_commit_list(revs->previous_parents);
 +                      revs->previous_parents = NULL;
 +              }
 +      }
        return c;
  }