Skip to content

Commit

Permalink
Merge branch 'db/remote-builtin' into jk/send-pack
Browse files Browse the repository at this point in the history
* db/remote-builtin:
  Reteach builtin-ls-remote to understand remotes
  Build in ls-remote
  Use built-in send-pack.
  Build-in send-pack, with an API for other programs to call.
  Build-in peek-remote, using transport infrastructure.
  Miscellaneous const changes and utilities

Conflicts:

	transport.c
  • Loading branch information
Junio C Hamano committed Nov 14, 2007
2 parents 481424e + 7c2c6ee commit a108e53
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 187 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ BASIC_LDFLAGS =
SCRIPT_SH = \
git-bisect.sh git-checkout.sh \
git-clean.sh git-clone.sh git-commit.sh \
git-ls-remote.sh \
git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
git-pull.sh git-rebase.sh git-rebase--interactive.sh \
git-repack.sh git-request-pull.sh \
Expand Down Expand Up @@ -241,7 +240,7 @@ PROGRAMS = \
git-fast-import$X \
git-daemon$X \
git-merge-index$X git-mktag$X git-mktree$X git-patch-id$X \
git-peek-remote$X git-receive-pack$X \
git-receive-pack$X \
git-send-pack$X git-shell$X \
git-show-index$X \
git-unpack-file$X \
Expand Down Expand Up @@ -347,6 +346,7 @@ BUILTIN_OBJS = \
builtin-log.o \
builtin-ls-files.o \
builtin-ls-tree.o \
builtin-ls-remote.o \
builtin-mailinfo.o \
builtin-mailsplit.o \
builtin-merge-base.o \
Expand All @@ -359,6 +359,7 @@ BUILTIN_OBJS = \
builtin-push.o \
builtin-read-tree.o \
builtin-reflog.o \
builtin-send-pack.o \
builtin-config.o \
builtin-rerere.o \
builtin-reset.o \
Expand Down
10 changes: 5 additions & 5 deletions builtin-fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static void unlock_pack_on_signal(int signo)
}

static void add_merge_config(struct ref **head,
struct ref *remote_refs,
const struct ref *remote_refs,
struct branch *branch,
struct ref ***tail)
{
Expand Down Expand Up @@ -77,7 +77,7 @@ static struct ref *get_ref_map(struct transport *transport,
struct ref *ref_map = NULL;
struct ref **tail = &ref_map;

struct ref *remote_refs = transport_get_remote_refs(transport);
const struct ref *remote_refs = transport_get_remote_refs(transport);

if (ref_count || tags) {
for (i = 0; i < ref_count; i++) {
Expand Down Expand Up @@ -354,12 +354,12 @@ static struct ref *find_non_local_tags(struct transport *transport,
struct path_list new_refs = { NULL, 0, 0, 1 };
char *ref_name;
int ref_name_len;
unsigned char *ref_sha1;
struct ref *tag_ref;
const unsigned char *ref_sha1;
const struct ref *tag_ref;
struct ref *rm = NULL;
struct ref *ref_map = NULL;
struct ref **tail = &ref_map;
struct ref *ref;
const struct ref *ref;

for_each_ref(add_existing, &existing_refs);
for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
Expand Down
74 changes: 74 additions & 0 deletions builtin-ls-remote.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "builtin.h"
#include "cache.h"
#include "transport.h"
#include "remote.h"

static const char ls_remote_usage[] =
"git-ls-remote [--upload-pack=<git-upload-pack>] [<host>:]<directory>";

int cmd_ls_remote(int argc, const char **argv, const char *prefix)
{
int i;
const char *dest = NULL;
int nongit = 0;
unsigned flags = 0;
const char *uploadpack = NULL;

struct remote *remote;
struct transport *transport;
const struct ref *ref;

setup_git_directory_gently(&nongit);

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

if (*arg == '-') {
if (!prefixcmp(arg, "--upload-pack=")) {
uploadpack = arg + 14;
continue;
}
if (!prefixcmp(arg, "--exec=")) {
uploadpack = arg + 7;
continue;
}
if (!strcmp("--tags", arg)) {
flags |= REF_TAGS;
continue;
}
if (!strcmp("--heads", arg)) {
flags |= REF_HEADS;
continue;
}
if (!strcmp("--refs", arg)) {
flags |= REF_NORMAL;
continue;
}
usage(ls_remote_usage);
}
dest = arg;
break;
}

if (!dest || i != argc - 1)
usage(ls_remote_usage);

remote = nongit ? NULL : remote_get(dest);
if (remote && !remote->url_nr)
die("remote %s has no configured URL", dest);
transport = transport_get(remote, remote ? remote->url[0] : dest);
if (uploadpack != NULL)
transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);

ref = transport_get_remote_refs(transport);

if (!ref)
return 1;

while (ref) {
if (check_ref_type(ref, flags))
printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name);
ref = ref->next;
}
return 0;
}
87 changes: 47 additions & 40 deletions send-pack.c → builtin-send-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
#include "pkt-line.h"
#include "run-command.h"
#include "remote.h"
#include "send-pack.h"

static const char send_pack_usage[] =
"git-send-pack [--all] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
" --all and explicit <ref> specification are mutually exclusive.";
static const char *receivepack = "git-receive-pack";
static int verbose;
static int send_all;
static int force_update;
static int use_thin_pack;
static int dry_run;

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

/*
* Make a pack stream and spit it out into file descriptor fd
Expand All @@ -26,7 +25,7 @@ static int pack_objects(int fd, struct ref *refs)
* the revision parameters to it via its stdin and
* let its stdout go back to the other end.
*/
const char *args[] = {
const char *argv[] = {
"pack-objects",
"--all-progress",
"--revs",
Expand All @@ -36,10 +35,10 @@ static int pack_objects(int fd, struct ref *refs)
};
struct child_process po;

if (use_thin_pack)
args[4] = "--thin";
if (args.use_thin_pack)
argv[4] = "--thin";
memset(&po, 0, sizeof(po));
po.argv = args;
po.argv = argv;
po.in = -1;
po.out = fd;
po.git_cmd = 1;
Expand Down Expand Up @@ -207,7 +206,7 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref)
}
}

static int send_pack(int in, int out, struct remote *remote, int nr_refspec, char **refspec)
static int do_send_pack(int in, int out, struct remote *remote, int nr_refspec, const char **refspec)
{
struct ref *ref;
int new_refs;
Expand All @@ -230,7 +229,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
if (!remote_tail)
remote_tail = &remote_refs;
if (match_refs(local_refs, remote_refs, &remote_tail,
nr_refspec, refspec, send_all))
nr_refspec, refspec, args.send_all))
return -1;

if (!remote_refs) {
Expand Down Expand Up @@ -259,7 +258,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
}
if (!will_delete_ref &&
!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
if (verbose)
if (args.verbose)
fprintf(stderr, "'%s': up-to-date\n", ref->name);
continue;
}
Expand All @@ -283,7 +282,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
* always allowed.
*/

if (!force_update &&
if (!args.force_update &&
!will_delete_ref &&
!is_null_sha1(ref->old_sha1) &&
!ref->force) {
Expand Down Expand Up @@ -313,7 +312,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
strcpy(old_hex, sha1_to_hex(ref->old_sha1));
new_hex = sha1_to_hex(ref->new_sha1);

if (!dry_run) {
if (!args.dry_run) {
if (ask_for_status_report) {
packet_write(out, "%s %s %s%c%s",
old_hex, new_hex, ref->name, 0,
Expand All @@ -338,7 +337,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
}

packet_flush(out);
if (new_refs && !dry_run)
if (new_refs && !args.dry_run)
ret = pack_objects(out, remote_refs);
close(out);

Expand All @@ -347,7 +346,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
ret = -4;
}

if (!dry_run && remote && ret == 0) {
if (!args.dry_run && remote && ret == 0) {
for (ref = remote_refs; ref; ref = ref->next)
update_tracking_ref(remote, ref);
}
Expand All @@ -357,7 +356,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
return ret;
}

static void verify_remote_names(int nr_heads, char **heads)
static void verify_remote_names(int nr_heads, const char **heads)
{
int i;

Expand All @@ -378,54 +377,49 @@ static void verify_remote_names(int nr_heads, char **heads)
}
}

int main(int argc, char **argv)
int cmd_send_pack(int argc, const char **argv, const char *prefix)
{
int i, nr_heads = 0;
char *dest = NULL;
char **heads = NULL;
int fd[2], ret;
struct child_process *conn;
char *remote_name = NULL;
const char **heads = NULL;
const char *remote_name = NULL;
struct remote *remote = NULL;

setup_git_directory();
git_config(git_default_config);
const char *dest = NULL;

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

if (*arg == '-') {
if (!prefixcmp(arg, "--receive-pack=")) {
receivepack = arg + 15;
args.receivepack = arg + 15;
continue;
}
if (!prefixcmp(arg, "--exec=")) {
receivepack = arg + 7;
args.receivepack = arg + 7;
continue;
}
if (!prefixcmp(arg, "--remote=")) {
remote_name = arg + 9;
continue;
}
if (!strcmp(arg, "--all")) {
send_all = 1;
args.send_all = 1;
continue;
}
if (!strcmp(arg, "--dry-run")) {
dry_run = 1;
args.dry_run = 1;
continue;
}
if (!strcmp(arg, "--force")) {
force_update = 1;
args.force_update = 1;
continue;
}
if (!strcmp(arg, "--verbose")) {
verbose = 1;
args.verbose = 1;
continue;
}
if (!strcmp(arg, "--thin")) {
use_thin_pack = 1;
args.use_thin_pack = 1;
continue;
}
usage(send_pack_usage);
Expand All @@ -434,15 +428,14 @@ int main(int argc, char **argv)
dest = arg;
continue;
}
heads = argv;
heads = (const char **) argv;
nr_heads = argc - i;
break;
}
if (!dest)
usage(send_pack_usage);
if (heads && send_all)
if (heads && args.send_all)
usage(send_pack_usage);
verify_remote_names(nr_heads, heads);

if (remote_name) {
remote = remote_get(remote_name);
Expand All @@ -452,8 +445,22 @@ int main(int argc, char **argv)
}
}

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

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;

memcpy(&args, my_args, sizeof(args));

verify_remote_names(nr_heads, heads);

conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0);
ret = do_send_pack(fd[0], fd[1], remote, nr_heads, heads);
close(fd[0]);
close(fd[1]);
ret |= finish_connect(conn);
Expand Down
2 changes: 2 additions & 0 deletions builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ extern int cmd_log(int argc, const char **argv, const char *prefix);
extern int cmd_log_reflog(int argc, const char **argv, const char *prefix);
extern int cmd_ls_files(int argc, const char **argv, const char *prefix);
extern int cmd_ls_tree(int argc, const char **argv, const char *prefix);
extern int cmd_ls_remote(int argc, const char **argv, const char *prefix);
extern int cmd_mailinfo(int argc, const char **argv, const char *prefix);
extern int cmd_mailsplit(int argc, const char **argv, const char *prefix);
extern int cmd_merge_base(int argc, const char **argv, const char *prefix);
Expand All @@ -69,6 +70,7 @@ extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
extern int cmd_revert(int argc, const char **argv, const char *prefix);
extern int cmd_rm(int argc, const char **argv, const char *prefix);
extern int cmd_runstatus(int argc, const char **argv, const char *prefix);
extern int cmd_send_pack(int argc, const char **argv, const char *prefix);
extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
extern int cmd_show(int argc, const char **argv, const char *prefix);
extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
Expand Down
2 changes: 1 addition & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ struct ref {
#define REF_TAGS (1u << 2)

#define CONNECT_VERBOSE (1u << 0)
extern struct child_process *git_connect(int fd[2], char *url, const char *prog, int flags);
extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);
extern int finish_connect(struct child_process *conn);
extern int path_match(const char *path, int nr, char **match);
extern int get_ack(int fd, unsigned char *result_sha1);
Expand Down
Loading

0 comments on commit a108e53

Please sign in to comment.