@matheusbsbs / matheus-bagliano / commits / 808a95dcad
Fork 0

diff --submodule: split into bite-sized pieces

Introduce two functions: - prepare_submodule_summary prepares the revision walker to list changes in a submodule. That is, it: * finds merge bases between the commits pointed to this path from before ("left") and after ("right") the change; * checks whether this is a fast-forward or fast-backward; * prepares a revision walk to list commits in the symmetric difference between the commits at each endpoint. It returns nonzero on error. - print_submodule_summary runs the revision walk and saves the result to a strbuf in --left-right format. The goal is just readability. No functional change intended. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Acked-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Mar 16, 2011 at 02:14 UTC 808a95dcad957ce00dc6fde7cf7f53ca32200c34
1 file changed +61 -42
submodule.c
+61 -42
@@ -152,17 +152,69 @@ void handle_ignore_submodules_arg(struct diff_options *diffopt,
152 die("bad --ignore-submodules argument: %s", arg);
153 }
154
155 +static int prepare_submodule_summary(struct rev_info *rev, const char *path,
156 + struct commit *left, struct commit *right,
157 + int *fast_forward, int *fast_backward)
158 +{
159 + struct commit_list *merge_bases, *list;
160 +
161 + init_revisions(rev, NULL);
162 + setup_revisions(0, NULL, rev, NULL);
163 + rev->left_right = 1;
164 + rev->first_parent_only = 1;
165 + left->object.flags |= SYMMETRIC_LEFT;
166 + add_pending_object(rev, &left->object, path);
167 + add_pending_object(rev, &right->object, path);
168 + merge_bases = get_merge_bases(left, right, 1);
169 + if (merge_bases) {
170 + if (merge_bases->item == left)
171 + *fast_forward = 1;
172 + else if (merge_bases->item == right)
173 + *fast_backward = 1;
174 + }
175 + for (list = merge_bases; list; list = list->next) {
176 + list->item->object.flags |= UNINTERESTING;
177 + add_pending_object(rev, &list->item->object,
178 + sha1_to_hex(list->item->object.sha1));
179 + }
180 + return prepare_revision_walk(rev);
181 +}
182 +
183 +static void print_submodule_summary(struct rev_info *rev, FILE *f,
184 + const char *del, const char *add, const char *reset)
185 +{
186 + static const char format[] = " %m %s";
187 + struct strbuf sb = STRBUF_INIT;
188 + struct commit *commit;
189 +
190 + while ((commit = get_revision(rev))) {
191 + struct pretty_print_context ctx = {0};
192 + ctx.date_mode = rev->date_mode;
193 + strbuf_setlen(&sb, 0);
194 + if (commit->object.flags & SYMMETRIC_LEFT) {
195 + if (del)
196 + strbuf_addstr(&sb, del);
197 + }
198 + else if (add)
199 + strbuf_addstr(&sb, add);
200 + format_commit_message(commit, format, &sb, &ctx);
201 + if (reset)
202 + strbuf_addstr(&sb, reset);
203 + strbuf_addch(&sb, '\n');
204 + fprintf(f, "%s", sb.buf);
205 + }
206 + strbuf_release(&sb);
207 +}
208 +
209 void show_submodule_summary(FILE *f, const char *path,
210 unsigned char one[20], unsigned char two[20],
211 unsigned dirty_submodule,
212 const char *del, const char *add, const char *reset)
213 {
214 struct rev_info rev;
161 - struct commit *commit, *left = left, *right = right;
162 - struct commit_list *merge_bases, *list;
215 + struct commit *left = left, *right = right;
216 const char *message = NULL;
217 struct strbuf sb = STRBUF_INIT;
165 - static const char *format = " %m %s";
218 int fast_forward = 0, fast_backward = 0;
219
220 if (is_null_sha1(two))
@@ -175,29 +227,10 @@ void show_submodule_summary(FILE *f, const char *path,
227 !(right = lookup_commit_reference(two)))
228 message = "(commits not present)";
229
178 - if (!message) {
179 - init_revisions(&rev, NULL);
180 - setup_revisions(0, NULL, &rev, NULL);
181 - rev.left_right = 1;
182 - rev.first_parent_only = 1;
183 - left->object.flags |= SYMMETRIC_LEFT;
184 - add_pending_object(&rev, &left->object, path);
185 - add_pending_object(&rev, &right->object, path);
186 - merge_bases = get_merge_bases(left, right, 1);
187 - if (merge_bases) {
188 - if (merge_bases->item == left)
189 - fast_forward = 1;
190 - else if (merge_bases->item == right)
191 - fast_backward = 1;
192 - }
193 - for (list = merge_bases; list; list = list->next) {
194 - list->item->object.flags |= UNINTERESTING;
195 - add_pending_object(&rev, &list->item->object,
196 - sha1_to_hex(list->item->object.sha1));
197 - }
198 - if (prepare_revision_walk(&rev))
199 - message = "(revision walker failed)";
200 - }
230 + if (!message &&
231 + prepare_submodule_summary(&rev, path, left, right,
232 + &fast_forward, &fast_backward))
233 + message = "(revision walker failed)";
234
235 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
236 fprintf(f, "Submodule %s contains untracked content\n", path);
@@ -221,25 +254,11 @@ void show_submodule_summary(FILE *f, const char *path,
254 fwrite(sb.buf, sb.len, 1, f);
255
256 if (!message) {
224 - while ((commit = get_revision(&rev))) {
225 - struct pretty_print_context ctx = {0};
226 - ctx.date_mode = rev.date_mode;
227 - strbuf_setlen(&sb, 0);
228 - if (commit->object.flags & SYMMETRIC_LEFT) {
229 - if (del)
230 - strbuf_addstr(&sb, del);
231 - }
232 - else if (add)
233 - strbuf_addstr(&sb, add);
234 - format_commit_message(commit, format, &sb, &ctx);
235 - if (reset)
236 - strbuf_addstr(&sb, reset);
237 - strbuf_addch(&sb, '\n');
238 - fprintf(f, "%s", sb.buf);
239 - }
257 + print_submodule_summary(&rev, f, del, add, reset);
258 clear_commit_marks(left, ~0);
259 clear_commit_marks(right, ~0);
260 }
261 +
262 strbuf_release(&sb);
263 }
264