@matheusbsbs / matheus-bagliano / commits / f1863d0d16
Fork 0

refactor duplicated code in builtin-send-pack.c and transport.c

The following functions are (almost) identical: verify_remote_names update_tracking_ref refs_pushed print_push_status Move common versions of these functions to transport.c and rename them, as suggested by Jeff King and Junio C Hamano. These functions have been removed entirely from builtin-send-pack.c, since they are only used internally by print_push_status(): print_ref_status status_abbrev print_ok_ref_status print_one_push_status Also, move #define SUMMARY_WIDTH to transport.h and rename it TRANSPORT_SUMMARY_WIDTH as it is used in builtin-fetch.c and transport.c Signed-off-by: Michael Lukashov <michael.lukashov@gmail.com> Acked-by: Tay Ray Chuan <rctay89@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Lukashov committed Feb 16, 2010 at 23:42 UTC f1863d0d16b9a5288671e17b7fa2eba8244ead2f
4 files changed +37 -207
builtin-fetch.c
+10 -10
@@ -11,6 +11,7 @@
11 #include "run-command.h"
12 #include "parse-options.h"
13 #include "sigchain.h"
14 +#include "transport.h"
15
16 static const char * const builtin_fetch_usage[] = {
17 "git fetch [options] [<repository> <refspec>...]",
@@ -205,7 +206,6 @@ static int s_update_ref(const char *action,
206 return 0;
207 }
208
208 -#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
209 #define REFCOL_WIDTH 10
210
211 static int update_local_ref(struct ref *ref,
@@ -224,7 +224,7 @@ static int update_local_ref(struct ref *ref,
224
225 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
226 if (verbosity > 0)
227 - sprintf(display, "= %-*s %-*s -> %s", SUMMARY_WIDTH,
227 + sprintf(display, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH,
228 "[up to date]", REFCOL_WIDTH, remote,
229 pretty_ref);
230 return 0;
@@ -239,7 +239,7 @@ static int update_local_ref(struct ref *ref,
239 * the head, and the old value of the head isn't empty...
240 */
241 sprintf(display, "! %-*s %-*s -> %s (can't fetch in current branch)",
242 - SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
242 + TRANSPORT_SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
243 pretty_ref);
244 return 1;
245 }
@@ -249,7 +249,7 @@ static int update_local_ref(struct ref *ref,
249 int r;
250 r = s_update_ref("updating tag", ref, 0);
251 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-',
252 - SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
252 + TRANSPORT_SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
253 pretty_ref, r ? " (unable to update local ref)" : "");
254 return r;
255 }
@@ -271,7 +271,7 @@ static int update_local_ref(struct ref *ref,
271
272 r = s_update_ref(msg, ref, 0);
273 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*',
274 - SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
274 + TRANSPORT_SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
275 r ? " (unable to update local ref)" : "");
276 return r;
277 }
@@ -284,7 +284,7 @@ static int update_local_ref(struct ref *ref,
284 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
285 r = s_update_ref("fast-forward", ref, 1);
286 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ',
287 - SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
287 + TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
288 pretty_ref, r ? " (unable to update local ref)" : "");
289 return r;
290 } else if (force || ref->force) {
@@ -295,13 +295,13 @@ static int update_local_ref(struct ref *ref,
295 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
296 r = s_update_ref("forced-update", ref, 1);
297 sprintf(display, "%c %-*s %-*s -> %s (%s)", r ? '!' : '+',
298 - SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
298 + TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
299 pretty_ref,
300 r ? "unable to update local ref" : "forced update");
301 return r;
302 } else {
303 sprintf(display, "! %-*s %-*s -> %s (non-fast-forward)",
304 - SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
304 + TRANSPORT_SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
305 pretty_ref);
306 return 1;
307 }
@@ -393,7 +393,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
393 rc |= update_local_ref(ref, what, note);
394 else
395 sprintf(note, "* %-*s %-*s -> FETCH_HEAD",
396 - SUMMARY_WIDTH, *kind ? kind : "branch",
396 + TRANSPORT_SUMMARY_WIDTH, *kind ? kind : "branch",
397 REFCOL_WIDTH, *what ? what : "HEAD");
398 if (*note) {
399 if (verbosity >= 0 && !shown_url) {
@@ -514,7 +514,7 @@ static int prune_refs(struct transport *transport, struct ref *ref_map)
514 result |= delete_ref(ref->name, NULL, 0);
515 if (verbosity >= 0) {
516 fprintf(stderr, " x %-*s %-*s -> %s\n",
517 - SUMMARY_WIDTH, "[deleted]",
517 + TRANSPORT_SUMMARY_WIDTH, "[deleted]",
518 REFCOL_WIDTH, "(none)", prettify_refname(ref->name));
519 warn_dangling_symref(stderr, dangling_msg, ref->name);
520 }
builtin-send-pack.c
+6 -185
@@ -7,6 +7,7 @@
7 #include "remote.h"
8 #include "send-pack.h"
9 #include "quote.h"
10 +#include "transport.h"
11
12 static const char send_pack_usage[] =
13 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
@@ -169,156 +170,6 @@ static int receive_status(int in, struct ref *refs)
170 return ret;
171 }
172
172 -static void update_tracking_ref(struct remote *remote, struct ref *ref)
173 -{
174 - struct refspec rs;
175 -
176 - if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
177 - return;
178 -
179 - rs.src = ref->name;
180 - rs.dst = NULL;
181 -
182 - if (!remote_find_tracking(remote, &rs)) {
183 - if (args.verbose)
184 - fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
185 - if (ref->deletion) {
186 - delete_ref(rs.dst, NULL, 0);
187 - } else
188 - update_ref("update by push", rs.dst,
189 - ref->new_sha1, NULL, 0, 0);
190 - free(rs.dst);
191 - }
192 -}
193 -
194 -#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
195 -
196 -static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
197 -{
198 - fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
199 - if (from)
200 - fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
201 - else
202 - fputs(prettify_refname(to->name), stderr);
203 - if (msg) {
204 - fputs(" (", stderr);
205 - fputs(msg, stderr);
206 - fputc(')', stderr);
207 - }
208 - fputc('\n', stderr);
209 -}
210 -
211 -static const char *status_abbrev(unsigned char sha1[20])
212 -{
213 - return find_unique_abbrev(sha1, DEFAULT_ABBREV);
214 -}
215 -
216 -static void print_ok_ref_status(struct ref *ref)
217 -{
218 - if (ref->deletion)
219 - print_ref_status('-', "[deleted]", ref, NULL, NULL);
220 - else if (is_null_sha1(ref->old_sha1))
221 - print_ref_status('*',
222 - (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
223 - "[new branch]"),
224 - ref, ref->peer_ref, NULL);
225 - else {
226 - char quickref[84];
227 - char type;
228 - const char *msg;
229 -
230 - strcpy(quickref, status_abbrev(ref->old_sha1));
231 - if (ref->nonfastforward) {
232 - strcat(quickref, "...");
233 - type = '+';
234 - msg = "forced update";
235 - } else {
236 - strcat(quickref, "..");
237 - type = ' ';
238 - msg = NULL;
239 - }
240 - strcat(quickref, status_abbrev(ref->new_sha1));
241 -
242 - print_ref_status(type, quickref, ref, ref->peer_ref, msg);
243 - }
244 -}
245 -
246 -static int print_one_push_status(struct ref *ref, const char *dest, int count)
247 -{
248 - if (!count)
249 - fprintf(stderr, "To %s\n", dest);
250 -
251 - switch(ref->status) {
252 - case REF_STATUS_NONE:
253 - print_ref_status('X', "[no match]", ref, NULL, NULL);
254 - break;
255 - case REF_STATUS_REJECT_NODELETE:
256 - print_ref_status('!', "[rejected]", ref, NULL,
257 - "remote does not support deleting refs");
258 - break;
259 - case REF_STATUS_UPTODATE:
260 - print_ref_status('=', "[up to date]", ref,
261 - ref->peer_ref, NULL);
262 - break;
263 - case REF_STATUS_REJECT_NONFASTFORWARD:
264 - print_ref_status('!', "[rejected]", ref, ref->peer_ref,
265 - "non-fast-forward");
266 - break;
267 - case REF_STATUS_REMOTE_REJECT:
268 - print_ref_status('!', "[remote rejected]", ref,
269 - ref->deletion ? NULL : ref->peer_ref,
270 - ref->remote_status);
271 - break;
272 - case REF_STATUS_EXPECTING_REPORT:
273 - print_ref_status('!', "[remote failure]", ref,
274 - ref->deletion ? NULL : ref->peer_ref,
275 - "remote failed to report status");
276 - break;
277 - case REF_STATUS_OK:
278 - print_ok_ref_status(ref);
279 - break;
280 - }
281 -
282 - return 1;
283 -}
284 -
285 -static void print_push_status(const char *dest, struct ref *refs)
286 -{
287 - struct ref *ref;
288 - int n = 0;
289 -
290 - if (args.verbose) {
291 - for (ref = refs; ref; ref = ref->next)
292 - if (ref->status == REF_STATUS_UPTODATE)
293 - n += print_one_push_status(ref, dest, n);
294 - }
295 -
296 - for (ref = refs; ref; ref = ref->next)
297 - if (ref->status == REF_STATUS_OK)
298 - n += print_one_push_status(ref, dest, n);
299 -
300 - for (ref = refs; ref; ref = ref->next) {
301 - if (ref->status != REF_STATUS_NONE &&
302 - ref->status != REF_STATUS_UPTODATE &&
303 - ref->status != REF_STATUS_OK)
304 - n += print_one_push_status(ref, dest, n);
305 - }
306 -}
307 -
308 -static int refs_pushed(struct ref *ref)
309 -{
310 - for (; ref; ref = ref->next) {
311 - switch(ref->status) {
312 - case REF_STATUS_NONE:
313 - case REF_STATUS_UPTODATE:
314 - break;
315 - default:
316 - return 1;
317 - }
318 - }
319 - return 0;
320 -}
321 -
173 static void print_helper_status(struct ref *ref)
174 {
175 struct strbuf buf = STRBUF_INIT;
@@ -489,37 +340,6 @@ int send_pack(struct send_pack_args *args,
340 return 0;
341 }
342
492 -static void verify_remote_names(int nr_heads, const char **heads)
493 -{
494 - int i;
495 -
496 - for (i = 0; i < nr_heads; i++) {
497 - const char *local = heads[i];
498 - const char *remote = strrchr(heads[i], ':');
499 -
500 - if (*local == '+')
501 - local++;
502 -
503 - /* A matching refspec is okay. */
504 - if (remote == local && remote[1] == '\0')
505 - continue;
506 -
507 - remote = remote ? (remote + 1) : local;
508 - switch (check_ref_format(remote)) {
509 - case 0: /* ok */
510 - case CHECK_REF_FORMAT_ONELEVEL:
511 - /* ok but a single level -- that is fine for
512 - * a match pattern.
513 - */
514 - case CHECK_REF_FORMAT_WILDCARD:
515 - /* ok but ends with a pattern-match character */
516 - continue;
517 - }
518 - die("remote part of refspec is not a valid name in %s",
519 - heads[i]);
520 - }
521 -}
522 -
343 int cmd_send_pack(int argc, const char **argv, const char *prefix)
344 {
345 int i, nr_refspecs = 0;
@@ -536,6 +356,7 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
356 int send_all = 0;
357 const char *receivepack = "git-receive-pack";
358 int flags;
359 + int nonfastforward = 0;
360
361 argv++;
362 for (i = 1; i < argc; i++, argv++) {
@@ -628,7 +449,7 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
449 get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
450 &extra_have);
451
631 - verify_remote_names(nr_refspecs, refspecs);
452 + transport_verify_remote_names(nr_refspecs, refspecs);
453
454 local_refs = get_local_heads();
455
@@ -657,15 +478,15 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
478 ret |= finish_connect(conn);
479
480 if (!helper_status)
660 - print_push_status(dest, remote_refs);
481 + transport_print_push_status(dest, remote_refs, args.verbose, 0, &nonfastforward);
482
483 if (!args.dry_run && remote) {
484 struct ref *ref;
485 for (ref = remote_refs; ref; ref = ref->next)
665 - update_tracking_ref(remote, ref);
486 + transport_update_tracking_ref(remote, ref, args.verbose);
487 }
488
668 - if (!ret && !refs_pushed(remote_refs))
489 + if (!ret && !transport_refs_pushed(remote_refs))
490 fprintf(stderr, "Everything up-to-date\n");
491
492 return ret;
transport.c
+10 -12
@@ -573,7 +573,7 @@ static int push_had_errors(struct ref *ref)
573 return 0;
574 }
575
576 -static int refs_pushed(struct ref *ref)
576 +int transport_refs_pushed(struct ref *ref)
577 {
578 for (; ref; ref = ref->next) {
579 switch(ref->status) {
@@ -587,7 +587,7 @@ static int refs_pushed(struct ref *ref)
587 return 0;
588 }
589
590 -static void update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
590 +void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
591 {
592 struct refspec rs;
593
@@ -609,8 +609,6 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref, int verb
609 }
610 }
611
612 -#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
613 -
612 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
613 {
614 if (porcelain) {
@@ -623,7 +621,7 @@ static void print_ref_status(char flag, const char *summary, struct ref *to, str
621 else
622 fprintf(stdout, "%s\n", summary);
623 } else {
626 - fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
624 + fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
625 if (from)
626 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
627 else
@@ -711,8 +709,8 @@ static int print_one_push_status(struct ref *ref, const char *dest, int count, i
709 return 1;
710 }
711
714 -static void print_push_status(const char *dest, struct ref *refs,
715 - int verbose, int porcelain, int * nonfastforward)
712 +void transport_print_push_status(const char *dest, struct ref *refs,
713 + int verbose, int porcelain, int *nonfastforward)
714 {
715 struct ref *ref;
716 int n = 0;
@@ -738,7 +736,7 @@ static void print_push_status(const char *dest, struct ref *refs,
736 }
737 }
738
741 -static void verify_remote_names(int nr_heads, const char **heads)
739 +void transport_verify_remote_names(int nr_heads, const char **heads)
740 {
741 int i;
742
@@ -1018,7 +1016,7 @@ int transport_push(struct transport *transport,
1016 int *nonfastforward)
1017 {
1018 *nonfastforward = 0;
1021 - verify_remote_names(refspec_nr, refspec);
1019 + transport_verify_remote_names(refspec_nr, refspec);
1020
1021 if (transport->push) {
1022 /* Maybe FIXME. But no important transport uses this case. */
@@ -1057,7 +1055,7 @@ int transport_push(struct transport *transport,
1055 ret |= err;
1056
1057 if (!quiet || err)
1060 - print_push_status(transport->url, remote_refs,
1058 + transport_print_push_status(transport->url, remote_refs,
1059 verbose | porcelain, porcelain,
1060 nonfastforward);
1061
@@ -1067,10 +1065,10 @@ int transport_push(struct transport *transport,
1065 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1066 struct ref *ref;
1067 for (ref = remote_refs; ref; ref = ref->next)
1070 - update_tracking_ref(transport->remote, ref, verbose);
1068 + transport_update_tracking_ref(transport->remote, ref, verbose);
1069 }
1070
1073 - if (!quiet && !ret && !refs_pushed(remote_refs))
1071 + if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1072 fprintf(stderr, "Everything up-to-date\n");
1073 return ret;
1074 }
transport.h
+11
@@ -92,6 +92,7 @@ struct transport {
92 #define TRANSPORT_PUSH_PORCELAIN 32
93 #define TRANSPORT_PUSH_QUIET 64
94 #define TRANSPORT_PUSH_SET_UPSTREAM 128
95 +#define TRANSPORT_SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
96
97 /* Returns a transport suitable for the url */
98 struct transport *transport_get(struct remote *, const char *);
@@ -142,4 +143,14 @@ int transport_connect(struct transport *transport, const char *name,
143 /* Transport methods defined outside transport.c */
144 int transport_helper_init(struct transport *transport, const char *name);
145
146 +/* common methods used by transport.c and builtin-send-pack.c */
147 +void transport_verify_remote_names(int nr_heads, const char **heads);
148 +
149 +void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose);
150 +
151 +int transport_refs_pushed(struct ref *ref);
152 +
153 +void transport_print_push_status(const char *dest, struct ref *refs,
154 + int verbose, int porcelain, int *nonfastforward);
155 +
156 #endif