Merge branch 'ml/send-pack-transport-refactor'

* ml/send-pack-transport-refactor:
  refactor duplicated code in builtin-send-pack.c and transport.c
This commit is contained in:
Junio C Hamano 2010-03-02 12:44:09 -08:00
commit bd282f58ad
4 changed files with 37 additions and 207 deletions

View File

@ -11,6 +11,7 @@
#include "run-command.h" #include "run-command.h"
#include "parse-options.h" #include "parse-options.h"
#include "sigchain.h" #include "sigchain.h"
#include "transport.h"
static const char * const builtin_fetch_usage[] = { static const char * const builtin_fetch_usage[] = {
"git fetch [options] [<repository> <refspec>...]", "git fetch [options] [<repository> <refspec>...]",
@ -205,7 +206,6 @@ static int s_update_ref(const char *action,
return 0; return 0;
} }
#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
#define REFCOL_WIDTH 10 #define REFCOL_WIDTH 10
static int update_local_ref(struct ref *ref, static int update_local_ref(struct ref *ref,
@ -224,7 +224,7 @@ static int update_local_ref(struct ref *ref,
if (!hashcmp(ref->old_sha1, ref->new_sha1)) { if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
if (verbosity > 0) if (verbosity > 0)
sprintf(display, "= %-*s %-*s -> %s", SUMMARY_WIDTH, sprintf(display, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH,
"[up to date]", REFCOL_WIDTH, remote, "[up to date]", REFCOL_WIDTH, remote,
pretty_ref); pretty_ref);
return 0; return 0;
@ -239,7 +239,7 @@ static int update_local_ref(struct ref *ref,
* the head, and the old value of the head isn't empty... * the head, and the old value of the head isn't empty...
*/ */
sprintf(display, "! %-*s %-*s -> %s (can't fetch in current branch)", sprintf(display, "! %-*s %-*s -> %s (can't fetch in current branch)",
SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote, TRANSPORT_SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
pretty_ref); pretty_ref);
return 1; return 1;
} }
@ -249,7 +249,7 @@ static int update_local_ref(struct ref *ref,
int r; int r;
r = s_update_ref("updating tag", ref, 0); r = s_update_ref("updating tag", ref, 0);
sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-', sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-',
SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote, TRANSPORT_SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
pretty_ref, r ? " (unable to update local ref)" : ""); pretty_ref, r ? " (unable to update local ref)" : "");
return r; return r;
} }
@ -271,7 +271,7 @@ static int update_local_ref(struct ref *ref,
r = s_update_ref(msg, ref, 0); r = s_update_ref(msg, ref, 0);
sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*', sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*',
SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref, TRANSPORT_SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
r ? " (unable to update local ref)" : ""); r ? " (unable to update local ref)" : "");
return r; return r;
} }
@ -284,7 +284,7 @@ static int update_local_ref(struct ref *ref,
strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV)); strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
r = s_update_ref("fast-forward", ref, 1); r = s_update_ref("fast-forward", ref, 1);
sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ', sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ',
SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote, TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
pretty_ref, r ? " (unable to update local ref)" : ""); pretty_ref, r ? " (unable to update local ref)" : "");
return r; return r;
} else if (force || ref->force) { } else if (force || ref->force) {
@ -295,13 +295,13 @@ static int update_local_ref(struct ref *ref,
strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV)); strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
r = s_update_ref("forced-update", ref, 1); r = s_update_ref("forced-update", ref, 1);
sprintf(display, "%c %-*s %-*s -> %s (%s)", r ? '!' : '+', sprintf(display, "%c %-*s %-*s -> %s (%s)", r ? '!' : '+',
SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote, TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
pretty_ref, pretty_ref,
r ? "unable to update local ref" : "forced update"); r ? "unable to update local ref" : "forced update");
return r; return r;
} else { } else {
sprintf(display, "! %-*s %-*s -> %s (non-fast-forward)", sprintf(display, "! %-*s %-*s -> %s (non-fast-forward)",
SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote, TRANSPORT_SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
pretty_ref); pretty_ref);
return 1; return 1;
} }
@ -393,7 +393,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
rc |= update_local_ref(ref, what, note); rc |= update_local_ref(ref, what, note);
else else
sprintf(note, "* %-*s %-*s -> FETCH_HEAD", sprintf(note, "* %-*s %-*s -> FETCH_HEAD",
SUMMARY_WIDTH, *kind ? kind : "branch", TRANSPORT_SUMMARY_WIDTH, *kind ? kind : "branch",
REFCOL_WIDTH, *what ? what : "HEAD"); REFCOL_WIDTH, *what ? what : "HEAD");
if (*note) { if (*note) {
if (verbosity >= 0 && !shown_url) { if (verbosity >= 0 && !shown_url) {
@ -514,7 +514,7 @@ static int prune_refs(struct transport *transport, struct ref *ref_map)
result |= delete_ref(ref->name, NULL, 0); result |= delete_ref(ref->name, NULL, 0);
if (verbosity >= 0) { if (verbosity >= 0) {
fprintf(stderr, " x %-*s %-*s -> %s\n", fprintf(stderr, " x %-*s %-*s -> %s\n",
SUMMARY_WIDTH, "[deleted]", TRANSPORT_SUMMARY_WIDTH, "[deleted]",
REFCOL_WIDTH, "(none)", prettify_refname(ref->name)); REFCOL_WIDTH, "(none)", prettify_refname(ref->name));
warn_dangling_symref(stderr, dangling_msg, ref->name); warn_dangling_symref(stderr, dangling_msg, ref->name);
} }

View File

@ -7,6 +7,7 @@
#include "remote.h" #include "remote.h"
#include "send-pack.h" #include "send-pack.h"
#include "quote.h" #include "quote.h"
#include "transport.h"
static const char send_pack_usage[] = static const char send_pack_usage[] =
"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n" "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)
return ret; return ret;
} }
static void update_tracking_ref(struct remote *remote, struct ref *ref)
{
struct refspec rs;
if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
return;
rs.src = ref->name;
rs.dst = NULL;
if (!remote_find_tracking(remote, &rs)) {
if (args.verbose)
fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
if (ref->deletion) {
delete_ref(rs.dst, NULL, 0);
} else
update_ref("update by push", rs.dst,
ref->new_sha1, NULL, 0, 0);
free(rs.dst);
}
}
#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
{
fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
if (from)
fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
else
fputs(prettify_refname(to->name), stderr);
if (msg) {
fputs(" (", stderr);
fputs(msg, stderr);
fputc(')', stderr);
}
fputc('\n', stderr);
}
static const char *status_abbrev(unsigned char sha1[20])
{
return find_unique_abbrev(sha1, DEFAULT_ABBREV);
}
static void print_ok_ref_status(struct ref *ref)
{
if (ref->deletion)
print_ref_status('-', "[deleted]", ref, NULL, NULL);
else if (is_null_sha1(ref->old_sha1))
print_ref_status('*',
(!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
"[new branch]"),
ref, ref->peer_ref, NULL);
else {
char quickref[84];
char type;
const char *msg;
strcpy(quickref, status_abbrev(ref->old_sha1));
if (ref->nonfastforward) {
strcat(quickref, "...");
type = '+';
msg = "forced update";
} else {
strcat(quickref, "..");
type = ' ';
msg = NULL;
}
strcat(quickref, status_abbrev(ref->new_sha1));
print_ref_status(type, quickref, ref, ref->peer_ref, msg);
}
}
static int print_one_push_status(struct ref *ref, const char *dest, int count)
{
if (!count)
fprintf(stderr, "To %s\n", dest);
switch(ref->status) {
case REF_STATUS_NONE:
print_ref_status('X', "[no match]", ref, NULL, NULL);
break;
case REF_STATUS_REJECT_NODELETE:
print_ref_status('!', "[rejected]", ref, NULL,
"remote does not support deleting refs");
break;
case REF_STATUS_UPTODATE:
print_ref_status('=', "[up to date]", ref,
ref->peer_ref, NULL);
break;
case REF_STATUS_REJECT_NONFASTFORWARD:
print_ref_status('!', "[rejected]", ref, ref->peer_ref,
"non-fast-forward");
break;
case REF_STATUS_REMOTE_REJECT:
print_ref_status('!', "[remote rejected]", ref,
ref->deletion ? NULL : ref->peer_ref,
ref->remote_status);
break;
case REF_STATUS_EXPECTING_REPORT:
print_ref_status('!', "[remote failure]", ref,
ref->deletion ? NULL : ref->peer_ref,
"remote failed to report status");
break;
case REF_STATUS_OK:
print_ok_ref_status(ref);
break;
}
return 1;
}
static void print_push_status(const char *dest, struct ref *refs)
{
struct ref *ref;
int n = 0;
if (args.verbose) {
for (ref = refs; ref; ref = ref->next)
if (ref->status == REF_STATUS_UPTODATE)
n += print_one_push_status(ref, dest, n);
}
for (ref = refs; ref; ref = ref->next)
if (ref->status == REF_STATUS_OK)
n += print_one_push_status(ref, dest, n);
for (ref = refs; ref; ref = ref->next) {
if (ref->status != REF_STATUS_NONE &&
ref->status != REF_STATUS_UPTODATE &&
ref->status != REF_STATUS_OK)
n += print_one_push_status(ref, dest, n);
}
}
static int refs_pushed(struct ref *ref)
{
for (; ref; ref = ref->next) {
switch(ref->status) {
case REF_STATUS_NONE:
case REF_STATUS_UPTODATE:
break;
default:
return 1;
}
}
return 0;
}
static void print_helper_status(struct ref *ref) static void print_helper_status(struct ref *ref)
{ {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
@ -523,37 +374,6 @@ int send_pack(struct send_pack_args *args,
return 0; return 0;
} }
static void verify_remote_names(int nr_heads, const char **heads)
{
int i;
for (i = 0; i < nr_heads; i++) {
const char *local = heads[i];
const char *remote = strrchr(heads[i], ':');
if (*local == '+')
local++;
/* A matching refspec is okay. */
if (remote == local && remote[1] == '\0')
continue;
remote = remote ? (remote + 1) : local;
switch (check_ref_format(remote)) {
case 0: /* ok */
case CHECK_REF_FORMAT_ONELEVEL:
/* ok but a single level -- that is fine for
* a match pattern.
*/
case CHECK_REF_FORMAT_WILDCARD:
/* ok but ends with a pattern-match character */
continue;
}
die("remote part of refspec is not a valid name in %s",
heads[i]);
}
}
int cmd_send_pack(int argc, const char **argv, const char *prefix) int cmd_send_pack(int argc, const char **argv, const char *prefix)
{ {
int i, nr_refspecs = 0; int i, nr_refspecs = 0;
@ -570,6 +390,7 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
int send_all = 0; int send_all = 0;
const char *receivepack = "git-receive-pack"; const char *receivepack = "git-receive-pack";
int flags; int flags;
int nonfastforward = 0;
argv++; argv++;
for (i = 1; i < argc; i++, argv++) { for (i = 1; i < argc; i++, argv++) {
@ -662,7 +483,7 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL, get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
&extra_have); &extra_have);
verify_remote_names(nr_refspecs, refspecs); transport_verify_remote_names(nr_refspecs, refspecs);
local_refs = get_local_heads(); local_refs = get_local_heads();
@ -691,15 +512,15 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
ret |= finish_connect(conn); ret |= finish_connect(conn);
if (!helper_status) if (!helper_status)
print_push_status(dest, remote_refs); transport_print_push_status(dest, remote_refs, args.verbose, 0, &nonfastforward);
if (!args.dry_run && remote) { if (!args.dry_run && remote) {
struct ref *ref; struct ref *ref;
for (ref = remote_refs; ref; ref = ref->next) for (ref = remote_refs; ref; ref = ref->next)
update_tracking_ref(remote, ref); transport_update_tracking_ref(remote, ref, args.verbose);
} }
if (!ret && !refs_pushed(remote_refs)) if (!ret && !transport_refs_pushed(remote_refs))
fprintf(stderr, "Everything up-to-date\n"); fprintf(stderr, "Everything up-to-date\n");
return ret; return ret;

View File

@ -573,7 +573,7 @@ static int push_had_errors(struct ref *ref)
return 0; return 0;
} }
static int refs_pushed(struct ref *ref) int transport_refs_pushed(struct ref *ref)
{ {
for (; ref; ref = ref->next) { for (; ref; ref = ref->next) {
switch(ref->status) { switch(ref->status) {
@ -587,7 +587,7 @@ static int refs_pushed(struct ref *ref)
return 0; return 0;
} }
static void update_tracking_ref(struct remote *remote, struct ref *ref, int verbose) void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
{ {
struct refspec rs; struct refspec rs;
@ -609,8 +609,6 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref, int verb
} }
} }
#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain) static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
{ {
if (porcelain) { if (porcelain) {
@ -623,7 +621,7 @@ static void print_ref_status(char flag, const char *summary, struct ref *to, str
else else
fprintf(stdout, "%s\n", summary); fprintf(stdout, "%s\n", summary);
} else { } else {
fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary); fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
if (from) if (from)
fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name)); fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
else else
@ -711,7 +709,7 @@ static int print_one_push_status(struct ref *ref, const char *dest, int count, i
return 1; return 1;
} }
static void print_push_status(const char *dest, struct ref *refs, void transport_print_push_status(const char *dest, struct ref *refs,
int verbose, int porcelain, int *nonfastforward) int verbose, int porcelain, int *nonfastforward)
{ {
struct ref *ref; struct ref *ref;
@ -738,7 +736,7 @@ static void print_push_status(const char *dest, struct ref *refs,
} }
} }
static void verify_remote_names(int nr_heads, const char **heads) void transport_verify_remote_names(int nr_heads, const char **heads)
{ {
int i; int i;
@ -1019,7 +1017,7 @@ int transport_push(struct transport *transport,
int *nonfastforward) int *nonfastforward)
{ {
*nonfastforward = 0; *nonfastforward = 0;
verify_remote_names(refspec_nr, refspec); transport_verify_remote_names(refspec_nr, refspec);
if (transport->push) { if (transport->push) {
/* Maybe FIXME. But no important transport uses this case. */ /* Maybe FIXME. But no important transport uses this case. */
@ -1058,7 +1056,7 @@ int transport_push(struct transport *transport,
ret |= err; ret |= err;
if (!quiet || err) if (!quiet || err)
print_push_status(transport->url, remote_refs, transport_print_push_status(transport->url, remote_refs,
verbose | porcelain, porcelain, verbose | porcelain, porcelain,
nonfastforward); nonfastforward);
@ -1068,10 +1066,10 @@ int transport_push(struct transport *transport,
if (!(flags & TRANSPORT_PUSH_DRY_RUN)) { if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
struct ref *ref; struct ref *ref;
for (ref = remote_refs; ref; ref = ref->next) for (ref = remote_refs; ref; ref = ref->next)
update_tracking_ref(transport->remote, ref, verbose); transport_update_tracking_ref(transport->remote, ref, verbose);
} }
if (!quiet && !ret && !refs_pushed(remote_refs)) if (!quiet && !ret && !transport_refs_pushed(remote_refs))
fprintf(stderr, "Everything up-to-date\n"); fprintf(stderr, "Everything up-to-date\n");
return ret; return ret;
} }

View File

@ -98,6 +98,7 @@ struct transport {
#define TRANSPORT_PUSH_PORCELAIN 32 #define TRANSPORT_PUSH_PORCELAIN 32
#define TRANSPORT_PUSH_QUIET 64 #define TRANSPORT_PUSH_QUIET 64
#define TRANSPORT_PUSH_SET_UPSTREAM 128 #define TRANSPORT_PUSH_SET_UPSTREAM 128
#define TRANSPORT_SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
/* Returns a transport suitable for the url */ /* Returns a transport suitable for the url */
struct transport *transport_get(struct remote *, const char *); struct transport *transport_get(struct remote *, const char *);
@ -148,4 +149,14 @@ int transport_connect(struct transport *transport, const char *name,
/* Transport methods defined outside transport.c */ /* Transport methods defined outside transport.c */
int transport_helper_init(struct transport *transport, const char *name); int transport_helper_init(struct transport *transport, const char *name);
/* common methods used by transport.c and builtin-send-pack.c */
void transport_verify_remote_names(int nr_heads, const char **heads);
void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose);
int transport_refs_pushed(struct ref *ref);
void transport_print_push_status(const char *dest, struct ref *refs,
int verbose, int porcelain, int *nonfastforward);
#endif #endif