Merge branch 'sb/blame-color'
authorJunio C Hamano <gitster@pobox.com>
Tue, 24 Jul 2018 21:50:50 +0000 (14:50 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 24 Jul 2018 21:50:51 +0000 (14:50 -0700)
Code clean-up.

* sb/blame-color:
blame: prefer xsnprintf to strcpy for colors

1  2 
builtin/blame.c
diff --combined builtin/blame.c
index 921d127f29257facb5942e7b03304a69bafff175,758cd39dd17b428b780c8e6d336cba8b86a67ea4..468b17c30c3594f63c9c9b1eedc2f436b1f89337
@@@ -9,7 -9,6 +9,7 @@@
  #include "config.h"
  #include "color.h"
  #include "builtin.h"
 +#include "repository.h"
  #include "commit.h"
  #include "diff.h"
  #include "revision.h"
@@@ -24,7 -23,6 +24,7 @@@
  #include "line-log.h"
  #include "dir.h"
  #include "progress.h"
 +#include "object-store.h"
  #include "blame.h"
  #include "string-list.h"
  
@@@ -545,7 -543,7 +545,7 @@@ static void output(struct blame_scorebo
                        struct commit *commit = ent->suspect->commit;
                        if (commit->object.flags & MORE_THAN_ONE_PATH)
                                continue;
 -                      for (suspect = commit->util; suspect; suspect = suspect->next) {
 +                      for (suspect = get_blame_suspects(commit); suspect; suspect = suspect->next) {
                                if (suspect->guilty && count++) {
                                        commit->object.flags |= MORE_THAN_ONE_PATH;
                                        break;
@@@ -578,7 -576,7 +578,7 @@@ static int read_ancestry(const char *gr
                /* The format is just "Commit Parent1 Parent2 ...\n" */
                struct commit_graft *graft = read_graft_line(&buf);
                if (graft)
 -                      register_commit_graft(graft, 0);
 +                      register_commit_graft(the_repository, graft, 0);
        }
        fclose(fp);
        strbuf_release(&buf);
  
  static int update_auto_abbrev(int auto_abbrev, struct blame_origin *suspect)
  {
 -      const char *uniq = find_unique_abbrev(suspect->commit->object.oid.hash,
 +      const char *uniq = find_unique_abbrev(&suspect->commit->object.oid,
                                              auto_abbrev);
        int len = strlen(uniq);
        if (auto_abbrev < len)
@@@ -761,15 -759,6 +761,15 @@@ static int blame_move_callback(const st
        return 0;
  }
  
 +static int is_a_rev(const char *name)
 +{
 +      struct object_id oid;
 +
 +      if (get_oid(name, &oid))
 +              return 0;
 +      return OBJ_NONE < oid_object_info(the_repository, &oid, NULL);
 +}
 +
  int cmd_blame(int argc, const char **argv, const char *prefix)
  {
        struct rev_info revs;
        for (;;) {
                switch (parse_options_step(&ctx, options, blame_opt_usage)) {
                case PARSE_OPT_HELP:
 +              case PARSE_OPT_ERROR:
                        exit(129);
                case PARSE_OPT_DONE:
                        if (ctx.argv[0])
@@@ -970,15 -958,16 +970,15 @@@ parse_done
        } else {
                if (argc < 2)
                        usage_with_options(blame_opt_usage, options);
 -              path = add_prefix(prefix, argv[argc - 1]);
 -              if (argc == 3 && !file_exists(path)) { /* (2b) */
 +              if (argc == 3 && is_a_rev(argv[argc - 1])) { /* (2b) */
                        path = add_prefix(prefix, argv[1]);
                        argv[1] = argv[2];
 +              } else {        /* (2a) */
 +                      if (argc == 2 && is_a_rev(argv[1]) && !get_git_work_tree())
 +                              die("missing <path> to blame");
 +                      path = add_prefix(prefix, argv[argc - 1]);
                }
                argv[argc - 1] = "--";
 -
 -              setup_work_tree();
 -              if (!file_exists(path))
 -                      die_errno("cannot stat path '%s'", path);
        }
  
        revs.disable_stdin = 1;
                find_alignment(&sb, &output_option);
                if (!*repeated_meta_color &&
                    (output_option & OUTPUT_COLOR_LINE))
-                       strcpy(repeated_meta_color, GIT_COLOR_CYAN);
+                       xsnprintf(repeated_meta_color,
+                                 sizeof(repeated_meta_color),
+                                 "%s", GIT_COLOR_CYAN);
        }
        if (output_option & OUTPUT_ANNOTATE_COMPAT)
                output_option &= ~(OUTPUT_COLOR_LINE | OUTPUT_SHOW_AGE_WITH_COLOR);