Merge branch 'ab/unconditional-free-and-null'
authorJunio C Hamano <gitster@pobox.com>
Mon, 27 Aug 2018 21:33:42 +0000 (14:33 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 Aug 2018 21:33:42 +0000 (14:33 -0700)
Code clean-up.

* ab/unconditional-free-and-null:
  refactor various if (x) FREE_AND_NULL(x) to just FREE_AND_NULL(x)

1  2 
blame.c
branch.c

diff --combined blame.c
+++ b/blame.c
@@@ -1,31 -1,11 +1,31 @@@
  #include "cache.h"
  #include "refs.h"
 +#include "object-store.h"
  #include "cache-tree.h"
  #include "mergesort.h"
  #include "diff.h"
  #include "diffcore.h"
  #include "tag.h"
  #include "blame.h"
 +#include "alloc.h"
 +#include "commit-slab.h"
 +
 +define_commit_slab(blame_suspects, struct blame_origin *);
 +static struct blame_suspects blame_suspects;
 +
 +struct blame_origin *get_blame_suspects(struct commit *commit)
 +{
 +      struct blame_origin **result;
 +
 +      result = blame_suspects_peek(&blame_suspects, commit);
 +
 +      return result ? *result : NULL;
 +}
 +
 +static void set_blame_suspects(struct commit *commit, struct blame_origin *origin)
 +{
 +      *blame_suspects_at(&blame_suspects, commit) = origin;
 +}
  
  void blame_origin_decref(struct blame_origin *o)
  {
                        blame_origin_decref(o->previous);
                free(o->file.ptr);
                /* Should be present exactly once in commit chain */
 -              for (p = o->commit->util; p; l = p, p = p->next) {
 +              for (p = get_blame_suspects(o->commit); p; l = p, p = p->next) {
                        if (p == o) {
                                if (l)
                                        l->next = p->next;
                                else
 -                                      o->commit->util = p->next;
 +                                      set_blame_suspects(o->commit, p->next);
                                free(o);
                                return;
                        }
@@@ -61,8 -41,8 +61,8 @@@ static struct blame_origin *make_origin
        FLEX_ALLOC_STR(o, path, path);
        o->commit = commit;
        o->refcnt = 1;
 -      o->next = commit->util;
 -      commit->util = o;
 +      o->next = get_blame_suspects(commit);
 +      set_blame_suspects(commit, o);
        return o;
  }
  
@@@ -74,13 -54,13 +74,13 @@@ static struct blame_origin *get_origin(
  {
        struct blame_origin *o, *l;
  
 -      for (o = commit->util, l = NULL; o; l = o, o = o->next) {
 +      for (o = get_blame_suspects(commit), l = NULL; o; l = o, o = o->next) {
                if (!strcmp(o->path, path)) {
                        /* bump to front */
                        if (l) {
                                l->next = o->next;
 -                              o->next = commit->util;
 -                              commit->util = o;
 +                              o->next = get_blame_suspects(commit);
 +                              set_blame_suspects(commit, o);
                        }
                        return blame_origin_incref(o);
                }
@@@ -90,8 -70,7 +90,8 @@@
  
  
  
 -static void verify_working_tree_path(struct commit *work_tree, const char *path)
 +static void verify_working_tree_path(struct repository *repo,
 +                                   struct commit *work_tree, const char *path)
  {
        struct commit_list *parents;
        int pos;
                unsigned mode;
  
                if (!get_tree_entry(commit_oid, path, &blob_oid, &mode) &&
 -                  oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB)
 +                  oid_object_info(repo, &blob_oid, NULL) == OBJ_BLOB)
                        return;
        }
  
 -      pos = cache_name_pos(path, strlen(path));
 +      pos = index_name_pos(repo->index, path, strlen(path));
        if (pos >= 0)
                ; /* path is in the index */
 -      else if (-1 - pos < active_nr &&
 -               !strcmp(active_cache[-1 - pos]->name, path))
 +      else if (-1 - pos < repo->index->cache_nr &&
 +               !strcmp(repo->index->cache[-1 - pos]->name, path))
                ; /* path is in the index, unmerged */
        else
                die("no such path '%s' in HEAD", path);
@@@ -120,7 -99,7 +120,7 @@@ static struct commit_list **append_pare
  {
        struct commit *parent;
  
 -      parent = lookup_commit_reference(oid);
 +      parent = lookup_commit_reference(the_repository, oid);
        if (!parent)
                die("no such commit %s", oid_to_hex(oid));
        return &commit_list_insert(parent, tail)->next;
@@@ -131,19 -110,17 +131,19 @@@ static void append_merge_parents(struc
        int merge_head;
        struct strbuf line = STRBUF_INIT;
  
 -      merge_head = open(git_path_merge_head(), O_RDONLY);
 +      merge_head = open(git_path_merge_head(the_repository), O_RDONLY);
        if (merge_head < 0) {
                if (errno == ENOENT)
                        return;
 -              die("cannot open '%s' for reading", git_path_merge_head());
 +              die("cannot open '%s' for reading",
 +                  git_path_merge_head(the_repository));
        }
  
        while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
                struct object_id oid;
                if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid))
 -                      die("unknown line in '%s': %s", git_path_merge_head(), line.buf);
 +                      die("unknown line in '%s': %s",
 +                          git_path_merge_head(the_repository), line.buf);
                tail = append_parent(tail, &oid);
        }
        close(merge_head);
@@@ -159,15 -136,14 +159,15 @@@ static void set_commit_buffer_from_strb
  {
        size_t len;
        void *buf = strbuf_detach(sb, &len);
 -      set_commit_buffer(c, buf, len);
 +      set_commit_buffer(the_repository, c, buf, len);
  }
  
  /*
   * Prepare a dummy commit that represents the work tree (or staged) item.
   * Note that annotating work tree item never works in the reverse.
   */
 -static struct commit *fake_working_tree_commit(struct diff_options *opt,
 +static struct commit *fake_working_tree_commit(struct repository *repo,
 +                                             struct diff_options *opt,
                                               const char *path,
                                               const char *contents_from)
  {
        struct strbuf buf = STRBUF_INIT;
        const char *ident;
        time_t now;
 -      int size, len;
 +      int len;
        struct cache_entry *ce;
        unsigned mode;
        struct strbuf msg = STRBUF_INIT;
  
 -      read_cache();
 +      read_index(repo->index);
        time(&now);
 -      commit = alloc_commit_node();
 +      commit = alloc_commit_node(the_repository);
        commit->object.parsed = 1;
        commit->date = now;
        parent_tail = &commit->parents;
  
        parent_tail = append_parent(parent_tail, &head_oid);
        append_merge_parents(parent_tail);
 -      verify_working_tree_path(commit, path);
 +      verify_working_tree_path(repo, commit, path);
  
        origin = make_origin(commit, path);
  
                if (strbuf_read(&buf, 0, 0) < 0)
                        die_errno("failed to read from stdin");
        }
 -      convert_to_git(&the_index, path, buf.buf, buf.len, &buf, 0);
 +      convert_to_git(repo->index, path, buf.buf, buf.len, &buf, 0);
        origin->file.ptr = buf.buf;
        origin->file.size = buf.len;
        pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid);
         * bits; we are not going to write this index out -- we just
         * want to run "diff-index --cached".
         */
 -      discard_cache();
 -      read_cache();
 +      discard_index(repo->index);
 +      read_index(repo->index);
  
        len = strlen(path);
        if (!mode) {
 -              int pos = cache_name_pos(path, len);
 +              int pos = index_name_pos(repo->index, path, len);
                if (0 <= pos)
 -                      mode = active_cache[pos]->ce_mode;
 +                      mode = repo->index->cache[pos]->ce_mode;
                else
                        /* Let's not bother reading from HEAD tree */
                        mode = S_IFREG | 0644;
        }
 -      size = cache_entry_size(len);
 -      ce = xcalloc(1, size);
 +      ce = make_empty_cache_entry(repo->index, len);
        oidcpy(&ce->oid, &origin->blob_oid);
        memcpy(ce->name, path, len);
        ce->ce_flags = create_ce_flags(0);
        ce->ce_namelen = len;
        ce->ce_mode = create_ce_mode(mode);
 -      add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 +      add_index_entry(repo->index, ce,
 +                      ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
  
 -      cache_tree_invalidate_path(&the_index, path);
 +      cache_tree_invalidate_path(repo->index, path);
  
        return commit;
  }
@@@ -337,9 -313,7 +337,7 @@@ static void fill_origin_blob(struct dif
  
  static void drop_origin_blob(struct blame_origin *o)
  {
-       if (o->file.ptr) {
-               FREE_AND_NULL(o->file.ptr);
-       }
+       FREE_AND_NULL(o->file.ptr);
  }
  
  /*
@@@ -502,7 -476,7 +500,7 @@@ static void queue_blames(struct blame_s
                porigin->suspects = blame_merge(porigin->suspects, sorted);
        else {
                struct blame_origin *o;
 -              for (o = porigin->commit->util; o; o = o->next) {
 +              for (o = get_blame_suspects(porigin->commit); o; o = o->next) {
                        if (o->suspects) {
                                porigin->suspects = sorted;
                                return;
   *
   * This also fills origin->mode for corresponding tree path.
   */
 -static int fill_blob_sha1_and_mode(struct blame_origin *origin)
 +static int fill_blob_sha1_and_mode(struct repository *repo,
 +                                 struct blame_origin *origin)
  {
        if (!is_null_oid(&origin->blob_oid))
                return 0;
        if (get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode))
                goto error_out;
 -      if (oid_object_info(the_repository, &origin->blob_oid, NULL) != OBJ_BLOB)
 +      if (oid_object_info(repo, &origin->blob_oid, NULL) != OBJ_BLOB)
                goto error_out;
        return 0;
   error_out:
@@@ -550,7 -523,7 +548,7 @@@ static struct blame_origin *find_origin
        const char *paths[2];
  
        /* First check any existing origins */
 -      for (porigin = parent->util; porigin; porigin = porigin->next)
 +      for (porigin = get_blame_suspects(parent); porigin; porigin = porigin->next)
                if (!strcmp(porigin->path, origin->path)) {
                        /*
                         * The same path between origin and its parent
@@@ -1575,7 -1548,7 +1573,7 @@@ void assign_blame(struct blame_scoreboa
  
        while (commit) {
                struct blame_entry *ent;
 -              struct blame_origin *suspect = commit->util;
 +              struct blame_origin *suspect = get_blame_suspects(commit);
  
                /* find one suspect to break down */
                while (suspect && !suspect->suspects)
@@@ -1677,7 -1650,7 +1675,7 @@@ static struct commit *find_single_final
                struct object *obj = revs->pending.objects[i].item;
                if (obj->flags & UNINTERESTING)
                        continue;
 -              obj = deref_tag(obj, NULL, 0);
 +              obj = deref_tag(the_repository, obj, NULL, 0);
                if (obj->type != OBJ_COMMIT)
                        die("Non commit %s?", revs->pending.objects[i].name);
                if (found)
@@@ -1708,15 -1681,14 +1706,15 @@@ static struct commit *dwim_reverse_init
  
        /* Is that sole rev a committish? */
        obj = revs->pending.objects[0].item;
 -      obj = deref_tag(obj, NULL, 0);
 +      obj = deref_tag(the_repository, obj, NULL, 0);
        if (obj->type != OBJ_COMMIT)
                return NULL;
  
        /* Do we have HEAD? */
        if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
                return NULL;
 -      head_commit = lookup_commit_reference_gently(&head_oid, 1);
 +      head_commit = lookup_commit_reference_gently(the_repository,
 +                                                   &head_oid, 1);
        if (!head_commit)
                return NULL;
  
@@@ -1744,7 -1716,7 +1742,7 @@@ static struct commit *find_single_initi
                struct object *obj = revs->pending.objects[i].item;
                if (!(obj->flags & UNINTERESTING))
                        continue;
 -              obj = deref_tag(obj, NULL, 0);
 +              obj = deref_tag(the_repository, obj, NULL, 0);
                if (obj->type != OBJ_COMMIT)
                        die("Non commit %s?", revs->pending.objects[i].name);
                if (found)
@@@ -1771,23 -1743,16 +1769,23 @@@ void init_scoreboard(struct blame_score
        sb->copy_score = BLAME_DEFAULT_COPY_SCORE;
  }
  
 -void setup_scoreboard(struct blame_scoreboard *sb, const char *path, struct blame_origin **orig)
 +void setup_scoreboard(struct blame_scoreboard *sb,
 +                    const char *path,
 +                    struct blame_origin **orig)
  {
        const char *final_commit_name = NULL;
        struct blame_origin *o;
        struct commit *final_commit = NULL;
        enum object_type type;
  
 +      init_blame_suspects(&blame_suspects);
 +
        if (sb->reverse && sb->contents_from)
                die(_("--contents and --reverse do not blend well."));
  
 +      if (!sb->repo)
 +              BUG("repo is NULL");
 +
        if (!sb->reverse) {
                sb->final = find_single_final(sb->revs, &final_commit_name);
                sb->commits.compare = compare_commits_by_commit_date;
                 * or "--contents".
                 */
                setup_work_tree();
 -              sb->final = fake_working_tree_commit(&sb->revs->diffopt,
 +              sb->final = fake_working_tree_commit(sb->repo,
 +                                                   &sb->revs->diffopt,
                                                     path, sb->contents_from);
                add_pending_object(sb->revs, &(sb->final->object), ":");
        }
        }
  
        if (is_null_oid(&sb->final->object.oid)) {
 -              o = sb->final->util;
 +              o = get_blame_suspects(sb->final);
                sb->final_buf = xmemdupz(o->file.ptr, o->file.size);
                sb->final_buf_size = o->file.size;
        }
        else {
                o = get_origin(sb->final, path);
 -              if (fill_blob_sha1_and_mode(o))
 +              if (fill_blob_sha1_and_mode(sb->repo, o))
                        die(_("no such path %s in %s"), path, final_commit_name);
  
                if (sb->revs->diffopt.flags.allow_textconv &&
diff --combined branch.c
+++ b/branch.c
@@@ -25,9 -25,7 +25,7 @@@ static int find_tracked_branch(struct r
                        tracking->remote = remote->name;
                } else {
                        free(tracking->spec.src);
-                       if (tracking->src) {
-                               FREE_AND_NULL(tracking->src);
-                       }
+                       FREE_AND_NULL(tracking->src);
                }
                tracking->spec.src = NULL;
        }
@@@ -302,7 -300,7 +300,7 @@@ void create_branch(const char *name, co
                break;
        }
  
 -      if ((commit = lookup_commit_reference(&oid)) == NULL)
 +      if ((commit = lookup_commit_reference(the_repository, &oid)) == NULL)
                die(_("Not a valid branch point: '%s'."), start_name);
        oidcpy(&oid, &commit->object.oid);
  
  
  void remove_branch_state(void)
  {
 -      unlink(git_path_cherry_pick_head());
 -      unlink(git_path_revert_head());
 -      unlink(git_path_merge_head());
 -      unlink(git_path_merge_rr());
 -      unlink(git_path_merge_msg());
 -      unlink(git_path_merge_mode());
 -      unlink(git_path_squash_msg());
 +      unlink(git_path_cherry_pick_head(the_repository));
 +      unlink(git_path_revert_head(the_repository));
 +      unlink(git_path_merge_head(the_repository));
 +      unlink(git_path_merge_rr(the_repository));
 +      unlink(git_path_merge_msg(the_repository));
 +      unlink(git_path_merge_mode(the_repository));
 +      unlink(git_path_squash_msg(the_repository));
  }
  
  void die_if_checked_out(const char *branch, int ignore_current_worktree)