Skip to content

Commit

Permalink
Merge branch 'db/push-cleanup'
Browse files Browse the repository at this point in the history
* db/push-cleanup:
  Move push matching and reporting logic into transport.c
  Use a common function to get the pretty name of refs

Conflicts:
	transport.c
  • Loading branch information
Junio C Hamano committed Mar 26, 2009
2 parents 3fcee25 + 64fcef2 commit f23336f
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 107 deletions.
6 changes: 1 addition & 5 deletions builtin-fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,7 @@ static int update_local_ref(struct ref *ref,
struct commit *current = NULL, *updated;
enum object_type type;
struct branch *current_branch = branch_get(NULL);
const char *pretty_ref = ref->name + (
!prefixcmp(ref->name, "refs/heads/") ? 11 :
!prefixcmp(ref->name, "refs/tags/") ? 10 :
!prefixcmp(ref->name, "refs/remotes/") ? 13 :
0);
const char *pretty_ref = prettify_ref(ref);

*display = 0;
type = sha1_object_info(ref->new_sha1, NULL);
Expand Down
153 changes: 74 additions & 79 deletions builtin-send-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ static const char send_pack_usage[] =
" --all and explicit <ref> specification are mutually exclusive.";

static struct send_pack_args args = {
/* .receivepack = */ "git-receive-pack",
};

static int feed_object(const unsigned char *sha1, int fd, int negative)
Expand All @@ -31,7 +30,7 @@ static int feed_object(const unsigned char *sha1, int fd, int negative)
/*
* Make a pack stream and spit it out into file descriptor fd
*/
static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra)
static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
{
/*
* The child becomes pack-objects --revs; we feed
Expand All @@ -49,7 +48,7 @@ static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *ext
struct child_process po;
int i;

if (args.use_thin_pack)
if (args->use_thin_pack)
argv[4] = "--thin";
memset(&po, 0, sizeof(po));
po.argv = argv;
Expand Down Expand Up @@ -83,8 +82,6 @@ static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *ext
return 0;
}

static struct ref *remote_refs, **remote_tail;

static int receive_status(int in, struct ref *refs)
{
struct ref *hint;
Expand Down Expand Up @@ -172,16 +169,6 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref)
}
}

static const char *prettify_ref(const struct ref *ref)
{
const char *name = ref->name;
return name + (
!prefixcmp(name, "refs/heads/") ? 11 :
!prefixcmp(name, "refs/tags/") ? 10 :
!prefixcmp(name, "refs/remotes/") ? 13 :
0);
}

#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)
Expand Down Expand Up @@ -310,47 +297,29 @@ static int refs_pushed(struct ref *ref)
return 0;
}

static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec)
int send_pack(struct send_pack_args *args,
int fd[], struct child_process *conn,
struct ref *remote_refs,
struct extra_have_objects *extra_have)
{
struct ref *ref, *local_refs;
int in = fd[0];
int out = fd[1];
struct ref *ref;
int new_refs;
int ask_for_status_report = 0;
int allow_deleting_refs = 0;
int expect_status_report = 0;
int flags = MATCH_REFS_NONE;
int ret;
struct extra_have_objects extra_have;

memset(&extra_have, 0, sizeof(extra_have));
if (args.send_all)
flags |= MATCH_REFS_ALL;
if (args.send_mirror)
flags |= MATCH_REFS_MIRROR;

/* No funny business with the matcher */
remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL,
&extra_have);
local_refs = get_local_heads();

/* Does the other end support the reporting? */
if (server_supports("report-status"))
ask_for_status_report = 1;
if (server_supports("delete-refs"))
allow_deleting_refs = 1;

/* match them up */
if (!remote_tail)
remote_tail = &remote_refs;
if (match_refs(local_refs, remote_refs, &remote_tail,
nr_refspec, refspec, flags)) {
close(out);
return -1;
}

if (!remote_refs) {
fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
"Perhaps you should specify a branch such as 'master'.\n");
close(out);
return 0;
}

Expand All @@ -362,7 +331,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest

if (ref->peer_ref)
hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
else if (!args.send_mirror)
else if (!args->send_mirror)
continue;

ref->deletion = is_null_sha1(ref->new_sha1);
Expand Down Expand Up @@ -401,15 +370,15 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
(!has_sha1_file(ref->old_sha1)
|| !ref_newer(ref->new_sha1, ref->old_sha1));

if (ref->nonfastforward && !ref->force && !args.force_update) {
if (ref->nonfastforward && !ref->force && !args->force_update) {
ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
continue;
}

if (!ref->deletion)
new_refs++;

if (!args.dry_run) {
if (!args->dry_run) {
char *old_hex = sha1_to_hex(ref->old_sha1);
char *new_hex = sha1_to_hex(ref->new_sha1);

Expand All @@ -430,27 +399,19 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
}

packet_flush(out);
if (new_refs && !args.dry_run) {
if (pack_objects(out, remote_refs, &extra_have) < 0)
if (new_refs && !args->dry_run) {
if (pack_objects(out, remote_refs, extra_have, args) < 0) {
for (ref = remote_refs; ref; ref = ref->next)
ref->status = REF_STATUS_NONE;
return -1;
}
}
else
close(out);

if (expect_status_report)
ret = receive_status(in, remote_refs);
else
ret = 0;

print_push_status(dest, remote_refs);

if (!args.dry_run && remote) {
for (ref = remote_refs; ref; ref = ref->next)
update_tracking_ref(remote, ref);
}

if (!refs_pushed(remote_refs))
fprintf(stderr, "Everything up-to-date\n");
if (ret < 0)
return ret;
for (ref = remote_refs; ref; ref = ref->next) {
Expand Down Expand Up @@ -499,31 +460,39 @@ static void verify_remote_names(int nr_heads, const char **heads)

int cmd_send_pack(int argc, const char **argv, const char *prefix)
{
int i, nr_heads = 0;
const char **heads = NULL;
int i, nr_refspecs = 0;
const char **refspecs = NULL;
const char *remote_name = NULL;
struct remote *remote = NULL;
const char *dest = NULL;
int fd[2];
struct child_process *conn;
struct extra_have_objects extra_have;
struct ref *remote_refs, **remote_tail, *local_refs;
int ret;
int send_all = 0;
const char *receivepack = "git-receive-pack";
int flags;

argv++;
for (i = 1; i < argc; i++, argv++) {
const char *arg = *argv;

if (*arg == '-') {
if (!prefixcmp(arg, "--receive-pack=")) {
args.receivepack = arg + 15;
receivepack = arg + 15;
continue;
}
if (!prefixcmp(arg, "--exec=")) {
args.receivepack = arg + 7;
receivepack = arg + 7;
continue;
}
if (!prefixcmp(arg, "--remote=")) {
remote_name = arg + 9;
continue;
}
if (!strcmp(arg, "--all")) {
args.send_all = 1;
send_all = 1;
continue;
}
if (!strcmp(arg, "--dry-run")) {
Expand Down Expand Up @@ -552,8 +521,8 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
dest = arg;
continue;
}
heads = (const char **) argv;
nr_heads = argc - i;
refspecs = (const char **) argv;
nr_refspecs = argc - i;
break;
}
if (!dest)
Expand All @@ -562,8 +531,8 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
* --all and --mirror are incompatible; neither makes sense
* with any refspecs.
*/
if ((heads && (args.send_all || args.send_mirror)) ||
(args.send_all && args.send_mirror))
if ((refspecs && (send_all || args.send_mirror)) ||
(send_all && args.send_mirror))
usage(send_pack_usage);

if (remote_name) {
Expand All @@ -574,24 +543,50 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
}
}

return send_pack(&args, dest, remote, nr_heads, heads);
}
conn = git_connect(fd, dest, receivepack, args.verbose ? CONNECT_VERBOSE : 0);

int send_pack(struct send_pack_args *my_args,
const char *dest, struct remote *remote,
int nr_heads, const char **heads)
{
int fd[2], ret;
struct child_process *conn;
memset(&extra_have, 0, sizeof(extra_have));

get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
&extra_have);

memcpy(&args, my_args, sizeof(args));
verify_remote_names(nr_refspecs, refspecs);

local_refs = get_local_heads();

flags = MATCH_REFS_NONE;

if (send_all)
flags |= MATCH_REFS_ALL;
if (args.send_mirror)
flags |= MATCH_REFS_MIRROR;

/* match them up */
remote_tail = &remote_refs;
while (*remote_tail)
remote_tail = &((*remote_tail)->next);
if (match_refs(local_refs, remote_refs, &remote_tail,
nr_refspecs, refspecs, flags)) {
return -1;
}

verify_remote_names(nr_heads, heads);
ret = send_pack(&args, fd, conn, remote_refs, &extra_have);

conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0);
ret = do_send_pack(fd[0], fd[1], remote, dest, nr_heads, heads);
close(fd[1]);
close(fd[0]);
/* do_send_pack always closes fd[1] */

ret |= finish_connect(conn);
return !!ret;

print_push_status(dest, remote_refs);

if (!args.dry_run && remote) {
struct ref *ref;
for (ref = remote_refs; ref; ref = ref->next)
update_tracking_ref(remote, ref);
}

if (!ret && !refs_pushed(remote_refs))
fprintf(stderr, "Everything up-to-date\n");

return ret;
}
10 changes: 10 additions & 0 deletions refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,16 @@ int check_ref_format(const char *ref)
}
}

const char *prettify_ref(const struct ref *ref)
{
const char *name = ref->name;
return name + (
!prefixcmp(name, "refs/heads/") ? 11 :
!prefixcmp(name, "refs/tags/") ? 10 :
!prefixcmp(name, "refs/remotes/") ? 13 :
0);
}

const char *ref_rev_parse_rules[] = {
"%.*s",
"refs/%.*s",
Expand Down
2 changes: 2 additions & 0 deletions refs.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ extern int for_each_reflog(each_ref_fn, void *);
#define CHECK_REF_FORMAT_WILDCARD (-3)
extern int check_ref_format(const char *target);

extern const char *prettify_ref(const struct ref *ref);

/** rename ref, return 0 on success **/
extern int rename_ref(const char *oldref, const char *newref, const char *logmsg);

Expand Down
6 changes: 2 additions & 4 deletions send-pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
#define SEND_PACK_H

struct send_pack_args {
const char *receivepack;
unsigned verbose:1,
send_all:1,
send_mirror:1,
force_update:1,
use_thin_pack:1,
dry_run:1;
};

int send_pack(struct send_pack_args *args,
const char *dest, struct remote *remote,
int nr_heads, const char **heads);
int fd[], struct child_process *conn,
struct ref *remote_refs, struct extra_have_objects *extra_have);

#endif
Loading

0 comments on commit f23336f

Please sign in to comment.