remote-helpers: exporting
cover remote-helpers: exporting
Hopefully this is in time for 1.7.1, it'd be good to have the 'export' command in asap. I'm fairly sure that the first two patches are good (all tests pass, and I've spent a lot of time debugging the relevant code), but I'd still appreciate if the clone experts could give it a look.
I'm really glad to finally get this working, hopefully I'll also have time to finish up the remote-hg implementation, but probably not before the 1.7.1 window closes (assuming that will be somewhere in the next few days).
Sverre Rabbelier (7):
clone: pass the remote name to remote_get
clone: also configure url for bare clones
fast-import: always create marks_file directories
remote-helpers: allow requesing the path to the .git directory
remote-helpers: add support for an export command
remote-helpers: add testgit helper
remote-helpers: add tests for testgit helper
.gitignore | 1 + Makefile | 2 + builtin/clone.c | 16 ++-- fast-import.c | 2 + git-remote-testgit.py | 233 +++++++++++++++++++++++++++++++++++ git_remote_helpers/git/exporter.py | 51 ++++++++ git_remote_helpers/git/importer.py | 38 ++++++ git_remote_helpers/git/non_local.py | 61 +++++++++ git_remote_helpers/git/repo.py | 70 +++++++++++ t/t5800-remote-helpers.sh | 70 +++++++++++ transport-helper.c | 141 +++++++++++++++++++-- 11 files changed, 665 insertions(+), 20 deletions(-)
PATCH 1/7 clone: pass the remote name to remote_get
Currently when using a remote helper to clone a repository, the remote helper will be passed the url of the target repository as first argument (which represents the name of the remote). This name is extracted from transport->remote->name, which is set by builtin/clone.c when it calls remote_get with argv[0] as argument.
Fix this by passing the name remote will be set up as instead.
However, setup_reference calls remote_get before the remote is added to the config file. This will result in an improperly configured remote (in memory) if later on remote_get is called with an argument that is not equal to the initial remote_get call in setup_reference. Fix this by delaying the remote_get call until after the remote has been added to the config file.
---
This is a combination of the earlier two-patch fix I sent.
builtin/clone.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
builtin/clone.c +4 -4
@@ -470,9 +470,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)*/ unsetenv (CONFIG_ENVIRONMENT ); -if (option_reference) -setup_reference (git_dir); -git_config (git_default_config,NULL );if (option_bare) {
@@ -504,6 +501,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)strbuf_reset (&key); } +if (option_reference) +setup_reference (git_dir); + fetch_pattern = value.buf; refspec =parse_fetch_refspec (1 , &fetch_pattern);
@@ -513,7 +513,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix) refs =clone_local (path, git_dir); mapped_refs =wanted_peer_refs (refs, refspec); }else { -struct remote *remote =remote_get (argv[0 ]); +struct remote *remote =remote_get (option_origin); transport =transport_get (remote, remote->url[0 ]);if (!transport->get_refs_list || !transport->fetch)
Signature · 1 line
1.7.0.3.317.gbb04ec
PATCH 2/7 clone: also configure url for bare clones
In reply to Sverre RabbelierWithout this the 'origin' remote would not be configured, so when calling remote_get with 'origin' as argument we would get an unconfigured remote.
---
Turns out there was one use-case that is not exercised by the test suite. The remote-helper tests later in the series do exercised that path, but we might want a seperate test.
builtin/clone.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
builtin/clone.c +4 -4
@@ -495,12 +495,12 @@ int cmd_clone(int argc, const char **argv, const char *prefix)git_config_set (key.buf,"true" );strbuf_reset (&key); } - -strbuf_addf (&key,"remote.%s.url" , option_origin); -git_config_set (key.buf, repo); -strbuf_reset (&key); } +strbuf_addf (&key,"remote.%s.url" , option_origin); +git_config_set (key.buf, repo); +strbuf_reset (&key); +if (option_reference)setup_reference (git_dir);
Signature · 1 line
1.7.0.3.317.gbb04ec
PATCH 3/7 fast-import: always create marks_file directories
In reply to Sverre Rabbelier---
Not stricly neccesary (one could argue that it is the responsibility of the caller to make sure the directory exists), but it might make sense to do this one (in fast-import) rather than having to do it in all helpers.
fast-import.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
fast-import.c +2 -0
@@ -2707,6 +2707,7 @@ static void option_import_marks(const char *marks, int from_stream) } import_marks_file =make_fast_import_path (marks); +safe_create_leading_directories_const (import_marks_file); import_marks_file_from_stream = from_stream; }
@@ -2737,6 +2738,7 @@ static void option_active_branches(const char *branches)static void option_export_marks (const char *marks) { export_marks_file =make_fast_import_path (marks); +safe_create_leading_directories_const (export_marks_file); }static void option_export_pack_edges (const char *edges)
Signature · 1 line
1.7.0.3.317.gbb04ec
PATCH 4/7 remote-helpers: allow requesing the path to the .git directory
In reply to Sverre RabbelierThe 'gitdir' capability is reported by the remote helper if it requires the location of the .git directory. The location of the .git directory can then be used by the helper to store status files even when the current directory is not a git repository (such as is the case when cloning).
The location of the .git dir is specified as an absolute path.
---
This should probably be advertised as a required capability, but I haven't looked into those yet.
transport-helper.c | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-)
transport-helper.c +5 -0
@@ -170,6 +170,11 @@ static struct child_process *get_helper(struct transport *transport) refspecs[refspec_nr++] =strdup (buf.buf +strlen ("refspec " )); }else if (!strcmp (capname,"connect" )) { data->connect =1 ; + }else if (!strcmp (buf.buf,"gitdir" )) { +struct strbuf gitdir =STRBUF_INIT ; +strbuf_addf (&gitdir,"gitdir %s\n" ,get_git_dir ()); +sendline (data, &gitdir); +strbuf_release (&gitdir); }else if (mandatory) { die("Unknown mandatory capability %s. This remote " "helper probably needs newer version of Git.\n",
Signature · 1 line
1.7.0.3.317.gbb04ec
PATCH 5/7 remote-helpers: add support for an export command
In reply to Sverre Rabbelier---
This complements the 'import' capability, making it possible to use remote-helpers with only fast-import/fast-exports streams.
transport-helper.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 124 insertions(+), 12 deletions(-)
transport-helper.c +124 -12
@@ -7,6 +7,7 @@#include "revision.h" #include "quote.h" #include "remote.h" +#include "string-list.h" static int debug;
@@ -17,6 +18,7 @@ struct helper_dataFILE *out; unsigned fetch : 1, import : 1, + export : 1, option : 1, push : 1, connect : 1,
@@ -163,6 +165,8 @@ static struct child_process *get_helper(struct transport *transport) data->push =1 ; else if (!strcmp(capname, "import")) data->import =1 ; + else if (!strcmp(capname, "export")) + data->export =1 ; else if (!data->refspecs && !prefixcmp(capname, "refspec ")) { ALLOC_GROW(refspecs, refspec_nr + 1,
@@ -356,6 +360,33 @@ static int get_importer(struct transport *transport, struct child_process *fastireturn start_command (fastimport); } +static int get_exporter (struct transport *transport, +struct child_process *fastexport, +const char *export_marks, +const char *import_marks, +struct string_list * revlist_args) +{ +struct child_process *helper =get_helper (transport); +int argc =0 , i; +memset (fastexport,0 ,sizeof (*fastexport)); + +/* we need to duplicate helper->in because we want to use it after +* fastexport is done with it. */ + fastexport->out =dup (helper->in); + fastexport->argv =xcalloc (4 + revlist_args->nr,sizeof (*fastexport->argv)); + fastexport->argv[argc++] ="fast-export" ; +if (export_marks) + fastexport->argv[argc++] = export_marks; +if (import_marks) + fastexport->argv[argc++] = import_marks; + +for (i =0 ; i < revlist_args->nr; i++) + fastexport->argv[argc++] = revlist_args->items[i].string; + + fastexport->git_cmd =1 ; +return start_command (fastexport); +} +static int fetch_with_import (struct transport *transport,int nr_heads,struct ref **to_fetch) {
@@ -523,7 +554,7 @@ static int fetch(struct transport *transport,return -1 ; } -static int push_refs (struct transport *transport, +static int push_refs_with_push (struct transport *transport,struct ref *remote_refs,int flags) {int force_all = flags &TRANSPORT_PUSH_FORCE ;
@@ -533,17 +564,6 @@ static int push_refs(struct transport *transport,struct child_process *helper;struct ref *ref; -if (process_connect (transport,1 )) { -do_take_over (transport); -return transport->push_refs (transport, remote_refs, flags); - } - -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" ); -return 0 ; - } - helper =get_helper (transport);if (!data->push)return 1 ;
@@ -662,6 +682,98 @@ static int push_refs(struct transport *transport,return 0 ; } +static int push_refs_with_export (struct transport *transport, +struct ref *remote_refs,int flags) +{ +struct ref *ref; +struct child_process *helper, exporter; +struct helper_data *data = transport->data; +char *export_marks =NULL , *import_marks =NULL ; +struct string_list revlist_args = {NULL ,0 ,0 }; +struct strbuf buf =STRBUF_INIT ; + + helper =get_helper (transport); + +write_constant (helper->in,"export\n" ); + +recvline (data, &buf); +if (debug) +fprintf (stderr,"Debug: Got export_marks '%s'\n" , buf.buf); +if (buf.len) + { +struct strbuf arg =STRBUF_INIT ; +strbuf_addstr (&arg,"--export-marks=" ); +strbuf_addbuf (&arg, &buf); + export_marks =strbuf_detach (&arg,NULL ); + } + +recvline (data, &buf); +if (debug) +fprintf (stderr,"Debug: Got import_marks '%s'\n" , buf.buf); +if (buf.len) + { +struct strbuf arg =STRBUF_INIT ; +strbuf_addstr (&arg,"--import-marks=" ); +strbuf_addbuf (&arg, &buf); + import_marks =strbuf_detach (&arg,NULL ); + } + +strbuf_reset (&buf); + +for (ref = remote_refs; ref; ref = ref->next) { +char * private; +unsigned char sha1[20 ]; + + +if (data->refspecs) + private =apply_refspecs (data->refspecs, data->refspec_nr, ref->name); + +if (private && !get_sha1 (private, sha1)) + { +strbuf_addf (&buf,"^%s" , private); +string_list_append (strbuf_detach (&buf,NULL ), &revlist_args); + } + +string_list_append (ref->name, &revlist_args); + + } + +if (get_exporter (transport, &exporter, + export_marks, import_marks, &revlist_args)) +die ("Couldn't run fast-export" ); + + data->no_disconnect_req =1 ; +finish_command (&exporter); +disconnect_helper (transport); +return 0 ; +} + +static int push_refs (struct transport *transport, +struct ref *remote_refs,int flags) +{ +struct helper_data *data = transport->data; + +if (process_connect (transport,1 )) { +do_take_over (transport); +return transport->push_refs (transport, remote_refs, flags); + } + +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" ); +return 0 ; + } + +if (data->push) +return push_refs_with_push (transport, remote_refs, flags); + +if (data->export) +return push_refs_with_export (transport, remote_refs, flags); + +return -1 ; +} + +static int has_attribute (const char *attrs,const char *attr) {int len;if (!attrs)
Signature · 1 line
1.7.0.3.317.gbb04ec
Re: [PATCH 5/7] remote-helpers: add support for an export command
Quoted 5 lines · Sverre Rabbelier
---
This complements the 'import' capability, making it possible to use remote-helpers with only fast-import/fast-exports streams.
It would be good to have documentation on what the helper should do doing here; it's relatively difficult to tell from this code.
I think that it's getting some options (import and export marks) back from the helper in a kind of ad-hoc and unclear fashion, and it should be doing something more appropriate. (I'm not sure what the more appropriate thing is, or really what the requirements are here as far as getting information around, but I think it needs to be discussed.)
-Daniel
*This .sig left intentionally blank*
Re: [PATCH 5/7] remote-helpers: add support for an export command
Heya,
Quoted 2 lines · Daniel Barkalow
It would be good to have documentation on what the helper should do doing here; it's relatively difficult to tell from this code.
Yes, I plan to send a documentation patch as well but wanted to get this out the door first (since Documentation patches don't have to cook in next).
Quoted 3 lines
I think that it's getting some options (import and export marks) back from the helper in a kind of ad-hoc and unclear fashion, and it should be doing something more appropriate.
It's not really that ad-hoc or unclear. When the helper receives the export command it should write two lines, the first being the location where it should write the marks to, the second being the location where it should import marks from. Either line may be empty, meaning marks should not be written or read respectively.
Quoted 3 lines
(I'm not sure what the more appropriate thing
is, or really what the requirements are here as far as getting information around, but I think it needs to be discussed.)
The import command solves this by printing 'feature import-marks' and 'feature export-marks' lines on the stream, but that doesn't work here since fast-export doesn't read from stdin. I don't think this needs a separate command.
Signature · 3 lines
Cheers, Sverre Rabbelier
PATCH 6/7 remote-helpers: add testgit helper
In reply to Sverre RabbelierCurrently the remote helper infrastructure is only used by the curl helper, which does not give a good impression of how remote helpers can be used to interact with foreign repositories. Since implementing such a helper is non-trivial it would be good to have at least one easy-to-follow example demonstrating how to implement a helper that interacts with a foreign vcs using fast-import/fast-export.
The testgit helper can be used to interact with remote git repositories by prefixing the url with "testgit::".
---
While working on git-remote-hg I tested the export code by just writing the fast-export stream to a file, and then manually importing it in a repo.
I then had the crazy idea to "just write a native git helper" that would do just that. Turns out it's quite hard to get everything right. I guess that only shows that this example is really needed so that others can more easily create a helper.
.gitignore | 1 + Makefile | 2 + git-remote-testgit.py | 233 +++++++++++++++++++++++++++++++++++ git_remote_helpers/git/exporter.py | 51 ++++++++ git_remote_helpers/git/importer.py | 38 ++++++ git_remote_helpers/git/non_local.py | 61 +++++++++ git_remote_helpers/git/repo.py | 70 +++++++++++ 7 files changed, 456 insertions(+), 0 deletions(-) create mode 100644 git-remote-testgit.py create mode 100644 git_remote_helpers/git/exporter.py create mode 100644 git_remote_helpers/git/importer.py create mode 100644 git_remote_helpers/git/non_local.py create mode 100644 git_remote_helpers/git/repo.py
.gitignore +1 -0
@@ -112,6 +112,7 @@ /git-remote-https /git-remote-ftp /git-remote-ftps +/git-remote-testgit /git-repack /git-replace /git-repo-config
Makefile +2 -0
@@ -366,6 +366,8 @@ SCRIPT_PERL += git-relink.perl SCRIPT_PERL += git-send-email.perl SCRIPT_PERL += git-svn.perl +SCRIPT_PYTHON += git-remote-testgit.py + SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)) \ $(patsubst %.perl,%,$(SCRIPT_PERL)) \ $(patsubst %.py,%,$(SCRIPT_PYTHON)) \
git-remote-testgit.py new file +233 -0
233 lines not on this page · raw message
git_remote_helpers/git/exporter.py new file +51 -0
51 lines not on this page · raw message
git_remote_helpers/git/importer.py new file +38 -0
@@ -0,0 +1,38 @@ +import os +import subprocess + + +class GitImporter(object): + """An importer for testgit repositories. + + This importer simply delegates to git fast-import. + """ + + def __init__(self, repo): + """Creates a new importer for the specified repo. + """ + + self.repo = repo + + def do_import(self, base): + """Imports a fast-import stream to the given directory. + + Simply delegates to git fast-import. + """ + + dirname = self.repo.get_base_path(base) + if self.repo.local: + gitdir = self.repo.gitpath + else: + gitdir = os.path.abspath(os.path.join(dirname, '.git')) + path = os.path.abspath(os.path.join(dirname, 'git.marks')) + + if not os.path.exists(dirname): + os.makedirs(dirname) + + args = ["git", "--git-dir=" + gitdir, "fast-import", "--quiet", "--export-marks=" + path] + + if os.path.exists(path): + args.append("--import-marks=" + path) + + subprocess.check_call(args)
git_remote_helpers/git/non_local.py new file +61 -0
61 lines not on this page · raw message
git_remote_helpers/git/repo.py new file +70 -0
70 lines not on this page · raw message
Signature · 1 line
1.7.0.3.317.gbb04ec
PATCH 7/7 remote-helpers: add tests for testgit helper
In reply to Sverre Rabbelier---
I used a httpd server for the remote at first, but figured we should probably not depend on those tests being run. It was fairly trivial to make the testgit helper treat file:// urls as 'remote', so I figured that'd work just as well.
t/t5800-remote-helpers.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 70 insertions(+), 0 deletions(-) create mode 100755 t/t5800-remote-helpers.sh
t/t5800-remote-helpers.sh new file +70 -0
@@ -0,0 +1,70 @@ +#!/bin/sh +# +# Copyright (c) 2010 Sverre Rabbelier +# + +test_description='Test remote-helper import and export commands' + +. ./test-lib.sh + +test_expect_success 'setup repository' ' + git init --bare server/.git && + git clone server public + (cd public && + echo content >file && + git add file && + git commit -m one && + git push origin master) +' + +test_expect_success 'cloning from local repo' ' + git clone "testgit::${PWD}/server" localclone + test_cmp public/file localclone/file +' + +test_expect_success 'cloning from remote repo' ' + git clone "testgit::file://${PWD}/server" clone && + test_cmp public/file clone/file +' + +test_expect_success 'create new commit on remote' ' + (cd public && + echo content >>file && + git commit -a -m two && + git push) +' + +test_expect_success 'pulling from local repo' ' + (cd localclone && git pull) && + test_cmp public/file localclone/file +' + +test_expect_success 'pulling from remote remote' ' + (cd clone && git pull) && + test_cmp public/file clone/file +' + +test_expect_success 'pushing to local repo' ' + (cd localclone && + echo content >>file && + git commit -a -m three && + git push) && + HEAD=$(git --git-dir=localclone/.git rev-parse --verify HEAD) && + test $HEAD = $(git --git-dir=server/.git rev-parse --verify HEAD) +' + +test_expect_success 'synch with changes from localclone' ' + (cd clone && + git pull) +' + +test_expect_success 'pushing remote local repo' ' + (cd clone && + echo content >>file && + git commit -a -m four && + git push) && + HEAD=$(git --git-dir=clone/.git rev-parse --verify HEAD) && + test $HEAD = $(git --git-dir=server/.git rev-parse --verify HEAD) +' + +test_done
Signature · 1 line
1.7.0.3.317.gbb04ec
Re: [PATCH 5/7] remote-helpers: add support for an export command
In reply to Sverre RabbelierQuoted 5 lines · Sverre Rabbelier
---
This complements the 'import' capability, making it possible to use remote-helpers with only fast-import/fast-exports streams.
I've queued this in 'pu' only to say "I've seen it", but I suspect that this has to wait until after 1.7.1. I've been trying to shoot for 6-8 week cycle as originally envisioned, and if things go as planned, post 1.7.1 cycle opens around mid April.
Quoted 7 lines
transport-helper.c +7
-0+for (ref = remote_refs; ref; ref = ref->next) { +char * private; +unsigned char sha1[20 ]; + + +if (data->refspecs) + private =apply_refspecs (data->refspecs, data->refspec_nr, ref->name);
transport-helper.c static int push_refs(struct transport *transport, · line 739
Quoted 10 lines
transport-helper.c +10
-0+ +if (private && !get_sha1 (private, sha1)) + { +strbuf_addf (&buf,"^%s" , private); +string_list_append (strbuf_detach (&buf,NULL ), &revlist_args); + } + +string_list_append (ref->name, &revlist_args); + + }
What's happening here? Style violations aside, it appears that "private" is accessed uninitialized when data->refspecs is NULL.
Re: [PATCH 5/7] remote-helpers: add support for an export command
Heya,
Quoted 4 lines · Junio C Hamano
I've queued this in 'pu' only to say "I've seen it", but I suspect that this has to wait until after 1.7.1. I've been trying to shoot for 6-8 week cycle as originally envisioned, and if things go as planned, post 1.7.1 cycle opens around mid April.
Okay, fair enough.
Quoted 2 lines
What's happening here? Style violations aside, it appears that "private" is accessed uninitialized when
data->refspecsis NULL.
Ah, you are right, it should have been "char *private = NULL".
Signature · 3 lines
Cheers, Sverre Rabbelier
Re: [PATCH 7/7] remote-helpers: add tests for testgit helper
In reply to Sverre RabbelierHi Sverre,
Quoted 3 lines · Sverre Rabbelier
t/t5800-remote-helpers.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 70 insertions(+), 0 deletions(-) create mode 100755 t/t5800-remote-helpers.sh
Sorry I missed this before. Maybe it would make sense to squash something like this in.
-- 8< --
Subject: t5800: testgit helper requires Python support
git remote-testgit is written in Python. In a NO_PYTHON build, tests using it would fail, so skip them.
--- t/t5800-remote-helpers.sh | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
t/t5800-remote-helpers.sh +6 -0
@@ -7,6 +7,12 @@ test_description='Test remote-helper import and export commands' . ./test-lib.sh +if ! test_have_prereq PYTHON +then + say 'skipping git remote-testgit tests: requires Python support' + test_done +fi + test_expect_success 'setup repository' ' git init --bare server/.git && git clone server public &&
Signature · 1 line
1.7.0.4
Re: [PATCH 7/7] remote-helpers: add tests for testgit helper
Heya,
Quoted 2 lines · Jonathan Nieder
Sorry I missed this before. Maybe it would make sense to squash something like this in.
Sorry for not thinking of this, thanks for noticing.
Signature · 3 lines
Cheers, Sverre Rabbelier