20
#include "trailer.h"
21
#include "log-tree.h"
22
#include "wt-status.h"
23
+#include "hashmap.h"
24
25
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
26
2749
2750
return 0;
2751
}
2752
+
2753
+struct subject2item_entry {
2754
+ struct hashmap_entry entry;
2755
+ int i;
2756
+ char subject[FLEX_ARRAY];
2757
+};
2758
+
2759
+static int subject2item_cmp(const void *fndata,
2760
+ const struct subject2item_entry *a,
2761
+ const struct subject2item_entry *b, const void *key)
2762
+{
2763
+ return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject);
2764
+}
2765
+
2766
+/*
2767
+ * Rearrange the todo list that has both "pick commit-id msg" and "pick
2768
+ * commit-id fixup!/squash! msg" in it so that the latter is put immediately
2769
+ * after the former, and change "pick" to "fixup"/"squash".
2770
+ *
2771
+ * Note that if the config has specified a custom instruction format, each log
2772
+ * message will have to be retrieved from the commit (as the oneline in the
2773
+ * script cannot be trusted) in order to normalize the autosquash arrangement.
2774
+ */
2775
+int rearrange_squash(void)
2776
+{
2777
+ const char *todo_file = rebase_path_todo();
2778
+ struct todo_list todo_list = TODO_LIST_INIT;
2779
+ struct hashmap subject2item;
2780
+ int res = 0, rearranged = 0, *next, *tail, fd, i;
2781
+ char **subjects;
2782
+
2783
+ fd = open(todo_file, O_RDONLY);
2784
+ if (fd < 0)
2785
+ return error_errno(_("could not open '%s'"), todo_file);
2786
+ if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2787
+ close(fd);
2788
+ return error(_("could not read '%s'."), todo_file);
2789
+ }
2790
+ close(fd);
2791
+ if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
2792
+ todo_list_release(&todo_list);
2793
+ return -1;
2794
+ }
2795
+
2796
+ /*
2797
+ * The hashmap maps onelines to the respective todo list index.
2798
+ *
2799
+ * If any items need to be rearranged, the next[i] value will indicate
2800
+ * which item was moved directly after the i'th.
2801
+ *
2802
+ * In that case, last[i] will indicate the index of the latest item to
2803
+ * be moved to appear after the i'th.
2804
+ */
2805
+ hashmap_init(&subject2item, (hashmap_cmp_fn) subject2item_cmp,
2806
+ NULL, todo_list.nr);
2807
+ ALLOC_ARRAY(next, todo_list.nr);
2808
+ ALLOC_ARRAY(tail, todo_list.nr);
2809
+ ALLOC_ARRAY(subjects, todo_list.nr);
2810
+ for (i = 0; i < todo_list.nr; i++) {
2811
+ struct strbuf buf = STRBUF_INIT;
2812
+ struct todo_item *item = todo_list.items + i;
2813
+ const char *commit_buffer, *subject, *p;
2814
+ size_t subject_len;
2815
+ int i2 = -1;
2816
+ struct subject2item_entry *entry;
2817
+
2818
+ next[i] = tail[i] = -1;
2819
+ if (item->command >= TODO_EXEC) {
2820
+ subjects[i] = NULL;
2821
+ continue;
2822
+ }
2823
+
2824
+ if (is_fixup(item->command)) {
2825
+ todo_list_release(&todo_list);
2826
+ return error(_("the script was already rearranged."));
2827
+ }
2828
+
2829
+ item->commit->util = item;
2830
+
2831
+ parse_commit(item->commit);
2832
+ commit_buffer = get_commit_buffer(item->commit, NULL);
2833
+ find_commit_subject(commit_buffer, &subject);
2834
+ format_subject(&buf, subject, " ");
2835
+ subject = subjects[i] = strbuf_detach(&buf, &subject_len);
2836
+ unuse_commit_buffer(item->commit, commit_buffer);
2837
+ if ((skip_prefix(subject, "fixup! ", &p) ||
2838
+ skip_prefix(subject, "squash! ", &p))) {
2839
+ struct commit *commit2;
2840
+
2841
+ for (;;) {
2842
+ while (isspace(*p))
2843
+ p++;
2844
+ if (!skip_prefix(p, "fixup! ", &p) &&
2845
+ !skip_prefix(p, "squash! ", &p))
2846
+ break;
2847
+ }
2848
+
2849
+ if ((entry = hashmap_get_from_hash(&subject2item,
2850
+ strhash(p), p)))
2851
+ /* found by title */
2852
+ i2 = entry->i;
2853
+ else if (!strchr(p, ' ') &&
2854
+ (commit2 =
2855
+ lookup_commit_reference_by_name(p)) &&
2856
+ commit2->util)
2857
+ /* found by commit name */
2858
+ i2 = (struct todo_item *)commit2->util
2859
+ - todo_list.items;
2860
+ else {
2861
+ /* copy can be a prefix of the commit subject */
2862
+ for (i2 = 0; i2 < i; i2++)
2863
+ if (subjects[i2] &&
2864
+ starts_with(subjects[i2], p))
2865
+ break;
2866
+ if (i2 == i)
2867
+ i2 = -1;
2868
+ }
2869
+ }
2870
+ if (i2 >= 0) {
2871
+ rearranged = 1;
2872
+ todo_list.items[i].command =
2873
+ starts_with(subject, "fixup!") ?
2874
+ TODO_FIXUP : TODO_SQUASH;
2875
+ if (next[i2] < 0)
2876
+ next[i2] = i;
2877
+ else
2878
+ next[tail[i2]] = i;
2879
+ tail[i2] = i;
2880
+ } else if (!hashmap_get_from_hash(&subject2item,
2881
+ strhash(subject), subject)) {
2882
+ FLEX_ALLOC_MEM(entry, subject, subject, subject_len);
2883
+ entry->i = i;
2884
+ hashmap_entry_init(entry, strhash(entry->subject));
2885
+ hashmap_put(&subject2item, entry);
2886
+ }
2887
+ }
2888
+
2889
+ if (rearranged) {
2890
+ struct strbuf buf = STRBUF_INIT;
2891
+
2892
+ for (i = 0; i < todo_list.nr; i++) {
2893
+ enum todo_command command = todo_list.items[i].command;
2894
+ int cur = i;
2895
+
2896
+ /*
2897
+ * Initially, all commands are 'pick's. If it is a
2898
+ * fixup or a squash now, we have rearranged it.
2899
+ */
2900
+ if (is_fixup(command))
2901
+ continue;
2902
+
2903
+ while (cur >= 0) {
2904
+ int offset = todo_list.items[cur].offset_in_buf;
2905
+ int end_offset = cur + 1 < todo_list.nr ?
2906
+ todo_list.items[cur + 1].offset_in_buf :
2907
+ todo_list.buf.len;
2908
+ char *bol = todo_list.buf.buf + offset;
2909
+ char *eol = todo_list.buf.buf + end_offset;
2910
+
2911
+ /* replace 'pick', by 'fixup' or 'squash' */
2912
+ command = todo_list.items[cur].command;
2913
+ if (is_fixup(command)) {
2914
+ strbuf_addstr(&buf,
2915
+ todo_command_info[command].str);
2916
+ bol += strcspn(bol, " \t");
2917
+ }
2918
+
2919
+ strbuf_add(&buf, bol, eol - bol);
2920
+
2921
+ cur = next[cur];
2922
+ }
2923
+ }
2924
+
2925
+ fd = open(todo_file, O_WRONLY);
2926
+ if (fd < 0)
2927
+ res = error_errno(_("could not open '%s'"), todo_file);
2928
+ else if (write(fd, buf.buf, buf.len) < 0)
2929
+ res = error_errno(_("could not read '%s'."), todo_file);
2930
+ else if (ftruncate(fd, buf.len) < 0)
2931
+ res = error_errno(_("could not finish '%s'"),
2932
+ todo_file);
2933
+ close(fd);
2934
+ strbuf_release(&buf);
2935
+ }
2936
+
2937
+ free(next);
2938
+ free(tail);
2939
+ for (i = 0; i < todo_list.nr; i++)
2940
+ free(subjects[i]);
2941
+ free(subjects);
2942
+ hashmap_free(&subject2item, 1);
2943
+ todo_list_release(&todo_list);
2944
+
2945
+ return res;
2946
+}