Merge branch 'jk/output-prefix-cleanup'
[git/gitster.git] / diffcore-rotate.c
blob73ca20b331253c5282506747b8aca2fd145bfa3d
1 /*
2 * Copyright (C) 2021, Google LLC.
3 * Based on diffcore-order.c, which is Copyright (C) 2005, Junio C Hamano
4 */
5 #include "git-compat-util.h"
6 #include "gettext.h"
7 #include "diff.h"
8 #include "diffcore.h"
10 void diffcore_rotate(struct diff_options *opt)
12 struct diff_queue_struct *q = &diff_queued_diff;
13 struct diff_queue_struct outq = DIFF_QUEUE_INIT;
14 int rotate_to, i;
16 if (!q->nr)
17 return;
19 for (i = 0; i < q->nr; i++) {
20 int cmp = strcmp(opt->rotate_to, q->queue[i]->two->path);
21 if (!cmp)
22 break; /* exact match */
23 if (!opt->rotate_to_strict && cmp < 0)
24 break; /* q->queue[i] is now past the target pathname */
27 if (q->nr <= i) {
28 /* we did not find the specified path */
29 if (opt->rotate_to_strict)
30 die(_("No such path '%s' in the diff"), opt->rotate_to);
31 return;
34 rotate_to = i;
36 for (i = rotate_to; i < q->nr; i++)
37 diff_q(&outq, q->queue[i]);
38 for (i = 0; i < rotate_to; i++) {
39 if (opt->skip_instead_of_rotate)
40 diff_free_filepair(q->queue[i]);
41 else
42 diff_q(&outq, q->queue[i]);
44 free(q->queue);
45 *q = outq;