]> git.ipfire.org Git - thirdparty/git.git/blobdiff - ref-filter.c
Merge branch 'pb/ref-filter-with-crlf'
[thirdparty/git.git] / ref-filter.c
index 110bcd741a9414bd90262b8e1ae9933e3198ca1c..6476686fea1d03d9cc43d5c03f7c0b3fe3052030 100644 (file)
@@ -1097,14 +1097,19 @@ static const char *copy_email(const char *buf, struct used_atom *atom)
 
 static char *copy_subject(const char *buf, unsigned long len)
 {
-       char *r = xmemdupz(buf, len);
+       struct strbuf sb = STRBUF_INIT;
        int i;
 
-       for (i = 0; i < len; i++)
-               if (r[i] == '\n')
-                       r[i] = ' ';
+       for (i = 0; i < len; i++) {
+               if (buf[i] == '\r' && i + 1 < len && buf[i + 1] == '\n')
+                       continue; /* ignore CR in CRLF */
 
-       return r;
+               if (buf[i] == '\n')
+                       strbuf_addch(&sb, ' ');
+               else
+                       strbuf_addch(&sb, buf[i]);
+       }
+       return strbuf_detach(&sb, NULL);
 }
 
 static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
@@ -1228,20 +1233,23 @@ static void find_subpos(const char *buf,
 
        /* subject is first non-empty line */
        *sub = buf;
-       /* subject goes to first empty line */
-       while (buf < *sig && *buf && *buf != '\n') {
-               eol = strchrnul(buf, '\n');
-               if (*eol)
-                       eol++;
-               buf = eol;
-       }
+       /* subject goes to first empty line before signature begins */
+       if ((eol = strstr(*sub, "\n\n"))) {
+               eol = eol < *sig ? eol : *sig;
+       /* check if message uses CRLF */
+       } else if (! (eol = strstr(*sub, "\r\n\r\n"))) {
+               /* treat whole message as subject */
+               eol = strrchr(*sub, '\0');
+       }
+       buf = eol;
        *sublen = buf - *sub;
        /* drop trailing newline, if present */
-       if (*sublen && (*sub)[*sublen - 1] == '\n')
+       while (*sublen && ((*sub)[*sublen - 1] == '\n' ||
+                          (*sub)[*sublen - 1] == '\r'))
                *sublen -= 1;
 
        /* skip any empty lines */
-       while (*buf == '\n')
+       while (*buf == '\n' || *buf == '\r')
                buf++;
        *body = buf;
        *bodylen = strlen(buf);
@@ -1557,9 +1565,7 @@ char *get_head_description(void)
                strbuf_addstr(&desc, _("no branch"));
        strbuf_addch(&desc, ')');
 
-       free(state.branch);
-       free(state.onto);
-       free(state.detached_from);
+       wt_status_state_free_buffers(&state);
        return strbuf_detach(&desc, NULL);
 }
 
@@ -2167,9 +2173,9 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
         * obtain the commit using the 'oid' available and discard all
         * non-commits early. The actual filtering is done later.
         */
-       if (filter->merge_commit || filter->with_commit || filter->no_commit || filter->verbose) {
-               commit = lookup_commit_reference_gently(the_repository, oid,
-                                                       1);
+       if (filter->reachable_from || filter->unreachable_from ||
+           filter->with_commit || filter->no_commit || filter->verbose) {
+               commit = lookup_commit_reference_gently(the_repository, oid, 1);
                if (!commit)
                        return 0;
                /* We perform the filtering for the '--contains' option... */
@@ -2231,13 +2237,21 @@ void ref_array_clear(struct ref_array *array)
        }
 }
 
-static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata)
+#define EXCLUDE_REACHED 0
+#define INCLUDE_REACHED 1
+static void reach_filter(struct ref_array *array,
+                        struct commit_list *check_reachable,
+                        int include_reached)
 {
        struct rev_info revs;
        int i, old_nr;
-       struct ref_filter *filter = ref_cbdata->filter;
-       struct ref_array *array = ref_cbdata->array;
-       struct commit **to_clear = xcalloc(sizeof(struct commit *), array->nr);
+       struct commit **to_clear;
+       struct commit_list *cr;
+
+       if (!check_reachable)
+               return;
+
+       to_clear = xcalloc(sizeof(struct commit *), array->nr);
 
        repo_init_revisions(the_repository, &revs, NULL);
 
@@ -2247,8 +2261,11 @@ static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata)
                to_clear[i] = item->commit;
        }
 
-       filter->merge_commit->object.flags |= UNINTERESTING;
-       add_pending_object(&revs, &filter->merge_commit->object, "");
+       for (cr = check_reachable; cr; cr = cr->next) {
+               struct commit *merge_commit = cr->item;
+               merge_commit->object.flags |= UNINTERESTING;
+               add_pending_object(&revs, &merge_commit->object, "");
+       }
 
        revs.limited = 1;
        if (prepare_revision_walk(&revs))
@@ -2263,14 +2280,19 @@ static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata)
 
                int is_merged = !!(commit->object.flags & UNINTERESTING);
 
-               if (is_merged == (filter->merge == REF_FILTER_MERGED_INCLUDE))
+               if (is_merged == include_reached)
                        array->items[array->nr++] = array->items[i];
                else
                        free_array_item(item);
        }
 
        clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS);
-       clear_commit_marks(filter->merge_commit, ALL_REV_FLAGS);
+
+       while (check_reachable) {
+               struct commit *merge_commit = pop_commit(&check_reachable);
+               clear_commit_marks(merge_commit, ALL_REV_FLAGS);
+       }
+
        free(to_clear);
 }
 
@@ -2322,8 +2344,8 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
        clear_contains_cache(&ref_cbdata.no_contains_cache);
 
        /*  Filters that need revision walking */
-       if (filter->merge_commit)
-               do_merge_filter(&ref_cbdata);
+       reach_filter(array, filter->reachable_from, INCLUDE_REACHED);
+       reach_filter(array, filter->unreachable_from, EXCLUDE_REACHED);
 
        return ret;
 }
@@ -2541,31 +2563,22 @@ int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
 {
        struct ref_filter *rf = opt->value;
        struct object_id oid;
-       int no_merged = starts_with(opt->long_name, "no");
+       struct commit *merge_commit;
 
        BUG_ON_OPT_NEG(unset);
 
-       if (rf->merge) {
-               if (no_merged) {
-                       return error(_("option `%s' is incompatible with --merged"),
-                                    opt->long_name);
-               } else {
-                       return error(_("option `%s' is incompatible with --no-merged"),
-                                    opt->long_name);
-               }
-       }
-
-       rf->merge = no_merged
-               ? REF_FILTER_MERGED_OMIT
-               : REF_FILTER_MERGED_INCLUDE;
-
        if (get_oid(arg, &oid))
                die(_("malformed object name %s"), arg);
 
-       rf->merge_commit = lookup_commit_reference_gently(the_repository,
-                                                         &oid, 0);
-       if (!rf->merge_commit)
+       merge_commit = lookup_commit_reference_gently(the_repository, &oid, 0);
+
+       if (!merge_commit)
                return error(_("option `%s' must point to a commit"), opt->long_name);
 
+       if (starts_with(opt->long_name, "no"))
+               commit_list_insert(merge_commit, &rf->unreachable_from);
+       else
+               commit_list_insert(merge_commit, &rf->reachable_from);
+
        return 0;
 }