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>
This commit is contained in:

committed by
Junio C Hamano

parent
6d816301cd
commit
f1863d0d16
@ -7,6 +7,7 @@
|
||||
#include "remote.h"
|
||||
#include "send-pack.h"
|
||||
#include "quote.h"
|
||||
#include "transport.h"
|
||||
|
||||
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"
|
||||
@ -169,156 +170,6 @@ static int receive_status(int in, struct ref *refs)
|
||||
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)
|
||||
{
|
||||
struct strbuf buf = STRBUF_INIT;
|
||||
@ -489,37 +340,6 @@ int send_pack(struct send_pack_args *args,
|
||||
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 i, nr_refspecs = 0;
|
||||
@ -536,6 +356,7 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
|
||||
int send_all = 0;
|
||||
const char *receivepack = "git-receive-pack";
|
||||
int flags;
|
||||
int nonfastforward = 0;
|
||||
|
||||
argv++;
|
||||
for (i = 1; i < argc; i++, argv++) {
|
||||
@ -628,7 +449,7 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
|
||||
get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
|
||||
&extra_have);
|
||||
|
||||
verify_remote_names(nr_refspecs, refspecs);
|
||||
transport_verify_remote_names(nr_refspecs, refspecs);
|
||||
|
||||
local_refs = get_local_heads();
|
||||
|
||||
@ -657,15 +478,15 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
|
||||
ret |= finish_connect(conn);
|
||||
|
||||
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) {
|
||||
struct ref *ref;
|
||||
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");
|
||||
|
||||
return ret;
|
||||
|
Reference in New Issue
Block a user