Merge branch 'ks/submodule-add-doc'
[gitweb.git] / line-log.c
index bbe31ed6fbb7c103c739f7e9a3c45cde5d36fcec..ab0709f9aecaacdfaf44aa31c88873b4ab5d8278 100644 (file)
@@ -34,18 +34,18 @@ void range_set_init(struct range_set *rs, size_t prealloc)
 
 void range_set_release(struct range_set *rs)
 {
-       free(rs->ranges);
+       FREE_AND_NULL(rs->ranges);
        rs->alloc = rs->nr = 0;
-       rs->ranges = NULL;
 }
 
 /* dst must be uninitialized! */
 static void range_set_copy(struct range_set *dst, struct range_set *src)
 {
        range_set_init(dst, src->nr);
-       memcpy(dst->ranges, src->ranges, src->nr*sizeof(struct range_set));
+       COPY_ARRAY(dst->ranges, src->ranges, src->nr);
        dst->nr = src->nr;
 }
+
 static void range_set_move(struct range_set *dst, struct range_set *src)
 {
        range_set_release(dst);
@@ -113,7 +113,7 @@ void sort_and_merge_range_set(struct range_set *rs)
        int i;
        int o = 0; /* output cursor */
 
-       qsort(rs->ranges, rs->nr, sizeof(struct range), range_cmp);
+       QSORT(rs->ranges, rs->nr, range_cmp);
 
        for (i = 0; i < rs->nr; i++) {
                if (rs->ranges[i].start == rs->ranges[i].end)
@@ -144,7 +144,7 @@ void sort_and_merge_range_set(struct range_set *rs)
 static void range_set_union(struct range_set *out,
                             struct range_set *a, struct range_set *b)
 {
-       int i = 0, j = 0, o = 0;
+       int i = 0, j = 0;
        struct range *ra = a->ranges;
        struct range *rb = b->ranges;
        /* cannot make an alias of out->ranges: it may change during grow */
@@ -167,16 +167,15 @@ static void range_set_union(struct range_set *out,
                        new = &rb[j++];
                if (new->start == new->end)
                        ; /* empty range */
-               else if (!o || out->ranges[o-1].end < new->start) {
+               else if (!out->nr || out->ranges[out->nr-1].end < new->start) {
                        range_set_grow(out, 1);
-                       out->ranges[o].start = new->start;
-                       out->ranges[o].end = new->end;
-                       o++;
-               } else if (out->ranges[o-1].end < new->end) {
-                       out->ranges[o-1].end = new->end;
+                       out->ranges[out->nr].start = new->start;
+                       out->ranges[out->nr].end = new->end;
+                       out->nr++;
+               } else if (out->ranges[out->nr-1].end < new->end) {
+                       out->ranges[out->nr-1].end = new->end;
                }
        }
-       out->nr = o;
 }
 
 /*
@@ -480,8 +479,7 @@ static struct commit *check_single_commit(struct rev_info *revs)
                struct object *obj = revs->pending.objects[i].item;
                if (obj->flags & UNINTERESTING)
                        continue;
-               while (obj->type == OBJ_TAG)
-                       obj = deref_tag(obj, NULL, 0);
+               obj = deref_tag(obj, NULL, 0);
                if (obj->type != OBJ_COMMIT)
                        die("Non commit %s?", revs->pending.objects[i].name);
                if (commit)
@@ -501,12 +499,12 @@ static struct commit *check_single_commit(struct rev_info *revs)
 static void fill_blob_sha1(struct commit *commit, struct diff_filespec *spec)
 {
        unsigned mode;
-       unsigned char sha1[20];
+       struct object_id oid;
 
        if (get_tree_entry(commit->object.oid.hash, spec->path,
-                          sha1, &mode))
+                          oid.hash, &mode))
                die("There is no path %s in the commit", spec->path);
-       fill_filespec(spec, sha1, 1, mode);
+       fill_filespec(spec, &oid, 1, mode);
 
        return;
 }
@@ -520,7 +518,7 @@ static void fill_line_ends(struct diff_filespec *spec, long *lines,
        char *data = NULL;
 
        if (diff_populate_filespec(spec, 0))
-               die("Cannot read blob %s", sha1_to_hex(spec->sha1));
+               die("Cannot read blob %s", oid_to_hex(&spec->oid));
 
        ALLOC_ARRAY(ends, size);
        ends[cur++] = 0;
@@ -611,8 +609,7 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
                line_log_data_insert(&ranges, full_name, begin, end);
 
                free_filespec(spec);
-               free(ends);
-               ends = NULL;
+               FREE_AND_NULL(ends);
        }
 
        for (p = ranges; p; p = p->next)
@@ -820,8 +817,8 @@ static void queue_diffs(struct line_log_data *range,
        assert(commit);
 
        DIFF_QUEUE_CLEAR(&diff_queued_diff);
-       diff_tree_sha1(parent ? parent->tree->object.oid.hash : NULL,
-                       commit->tree->object.oid.hash, "", opt);
+       diff_tree_oid(parent ? &parent->tree->object.oid : NULL,
+                     &commit->tree->object.oid, "", opt);
        if (opt->detect_rename) {
                filter_diffs_for_paths(range, 1);
                if (diff_might_be_rename())
@@ -841,7 +838,7 @@ static char *get_nth_line(long line, unsigned long *ends, void *data)
 
 static void print_line(const char *prefix, char first,
                       long line, unsigned long *ends, void *data,
-                      const char *color, const char *reset)
+                      const char *color, const char *reset, FILE *file)
 {
        char *begin = get_nth_line(line, ends, data);
        char *end = get_nth_line(line+1, ends, data);
@@ -852,14 +849,14 @@ static void print_line(const char *prefix, char first,
                had_nl = 1;
        }
 
-       fputs(prefix, stdout);
-       fputs(color, stdout);
-       putchar(first);
-       fwrite(begin, 1, end-begin, stdout);
-       fputs(reset, stdout);
-       putchar('\n');
+       fputs(prefix, file);
+       fputs(color, file);
+       putc(first, file);
+       fwrite(begin, 1, end-begin, file);
+       fputs(reset, file);
+       putc('\n', file);
        if (!had_nl)
-               fputs("\\ No newline at end of file\n", stdout);
+               fputs("\\ No newline at end of file\n", file);
 }
 
 static char *output_prefix(struct diff_options *opt)
@@ -894,16 +891,16 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
        if (!pair || !diff)
                return;
 
-       if (pair->one->sha1_valid)
+       if (pair->one->oid_valid)
                fill_line_ends(pair->one, &p_lines, &p_ends);
        fill_line_ends(pair->two, &t_lines, &t_ends);
 
-       printf("%s%sdiff --git a/%s b/%s%s\n", prefix, c_meta, pair->one->path, pair->two->path, c_reset);
-       printf("%s%s--- %s%s%s\n", prefix, c_meta,
-              pair->one->sha1_valid ? "a/" : "",
-              pair->one->sha1_valid ? pair->one->path : "/dev/null",
+       fprintf(opt->file, "%s%sdiff --git a/%s b/%s%s\n", prefix, c_meta, pair->one->path, pair->two->path, c_reset);
+       fprintf(opt->file, "%s%s--- %s%s%s\n", prefix, c_meta,
+              pair->one->oid_valid ? "a/" : "",
+              pair->one->oid_valid ? pair->one->path : "/dev/null",
               c_reset);
-       printf("%s%s+++ b/%s%s\n", prefix, c_meta, pair->two->path, c_reset);
+       fprintf(opt->file, "%s%s+++ b/%s%s\n", prefix, c_meta, pair->two->path, c_reset);
        for (i = 0; i < range->ranges.nr; i++) {
                long p_start, p_end;
                long t_start = range->ranges.ranges[i].start;
@@ -945,7 +942,7 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
                }
 
                /* Now output a diff hunk for this range */
-               printf("%s%s@@ -%ld,%ld +%ld,%ld @@%s\n",
+               fprintf(opt->file, "%s%s@@ -%ld,%ld +%ld,%ld @@%s\n",
                       prefix, c_frag,
                       p_start+1, p_end-p_start, t_start+1, t_end-t_start,
                       c_reset);
@@ -953,18 +950,18 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
                        int k;
                        for (; t_cur < diff->target.ranges[j].start; t_cur++)
                                print_line(prefix, ' ', t_cur, t_ends, pair->two->data,
-                                          c_context, c_reset);
+                                          c_context, c_reset, opt->file);
                        for (k = diff->parent.ranges[j].start; k < diff->parent.ranges[j].end; k++)
                                print_line(prefix, '-', k, p_ends, pair->one->data,
-                                          c_old, c_reset);
+                                          c_old, c_reset, opt->file);
                        for (; t_cur < diff->target.ranges[j].end && t_cur < t_end; t_cur++)
                                print_line(prefix, '+', t_cur, t_ends, pair->two->data,
-                                          c_new, c_reset);
+                                          c_new, c_reset, opt->file);
                        j++;
                }
                for (; t_cur < t_end; t_cur++)
                        print_line(prefix, ' ', t_cur, t_ends, pair->two->data,
-                                  c_context, c_reset);
+                                  c_context, c_reset, opt->file);
        }
 
        free(p_ends);
@@ -977,7 +974,7 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
  */
 static void dump_diff_hacky(struct rev_info *rev, struct line_log_data *range)
 {
-       puts(output_prefix(&rev->diffopt));
+       fprintf(rev->diffopt.file, "%s\n", output_prefix(&rev->diffopt));
        while (range) {
                dump_diff_hacky_one(rev, range);
                range = range->next;
@@ -1011,12 +1008,12 @@ static int process_diff_filepair(struct rev_info *rev,
        if (rg->ranges.nr == 0)
                return 0;
 
-       assert(pair->two->sha1_valid);
+       assert(pair->two->oid_valid);
        diff_populate_filespec(pair->two, 0);
        file_target.ptr = pair->two->data;
        file_target.size = pair->two->size;
 
-       if (pair->one->sha1_valid) {
+       if (pair->one->oid_valid) {
                diff_populate_filespec(pair->one, 0);
                file_parent.ptr = pair->one->data;
                file_parent.size = pair->one->size;
@@ -1126,6 +1123,7 @@ static int process_ranges_ordinary_commit(struct rev_info *rev, struct commit *c
        changed = process_all_files(&parent_range, rev, &queue, range);
        if (parent)
                add_line_range(rev, parent, parent_range);
+       free_line_log_data(parent_range);
        return changed;
 }