Merge branch 'dl/stash-show-untracked'
[git] / diffcore-rotate.c
1 /*
2  * Copyright (C) 2021, Google LLC.
3  * Based on diffcore-order.c, which is Copyright (C) 2005, Junio C Hamano
4  */
5 #include "cache.h"
6 #include "diff.h"
7 #include "diffcore.h"
8
9 void diffcore_rotate(struct diff_options *opt)
10 {
11         struct diff_queue_struct *q = &diff_queued_diff;
12         struct diff_queue_struct outq;
13         int rotate_to, i;
14
15         if (!q->nr)
16                 return;
17
18         for (i = 0; i < q->nr; i++) {
19                 int cmp = strcmp(opt->rotate_to, q->queue[i]->two->path);
20                 if (!cmp)
21                         break; /* exact match */
22                 if (!opt->rotate_to_strict && cmp < 0)
23                         break; /* q->queue[i] is now past the target pathname */
24         }
25
26         if (q->nr <= i) {
27                 /* we did not find the specified path */
28                 if (opt->rotate_to_strict)
29                         die(_("No such path '%s' in the diff"), opt->rotate_to);
30                 return;
31         }
32
33         DIFF_QUEUE_CLEAR(&outq);
34         rotate_to = i;
35
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]);
43         }
44         free(q->queue);
45         *q = outq;
46 }