2012-09-19 23:21:15 +08:00
|
|
|
#include "cache.h"
|
2015-06-22 22:03:05 +08:00
|
|
|
#include "refs.h"
|
2012-09-19 23:21:15 +08:00
|
|
|
#include "remote.h"
|
|
|
|
#include "strbuf.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "exec_cmd.h"
|
|
|
|
#include "run-command.h"
|
|
|
|
#include "vcs-svn/svndump.h"
|
|
|
|
#include "notes.h"
|
|
|
|
#include "argv-array.h"
|
|
|
|
|
|
|
|
static const char *url;
|
2012-09-19 23:21:23 +08:00
|
|
|
static int dump_from_file;
|
2012-09-19 23:21:15 +08:00
|
|
|
static const char *private_ref;
|
|
|
|
static const char *remote_ref = "refs/heads/master";
|
2012-09-19 23:21:27 +08:00
|
|
|
static const char *marksfilename, *notes_ref;
|
|
|
|
struct rev_note { unsigned int rev_nr; };
|
2012-09-19 23:21:15 +08:00
|
|
|
|
|
|
|
static int cmd_capabilities(const char *line);
|
|
|
|
static int cmd_import(const char *line);
|
|
|
|
static int cmd_list(const char *line);
|
|
|
|
|
|
|
|
typedef int (*input_command_handler)(const char *);
|
|
|
|
struct input_command_entry {
|
|
|
|
const char *name;
|
|
|
|
input_command_handler fn;
|
|
|
|
unsigned char batchable; /* whether the command starts or is part of a batch */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct input_command_entry input_command_list[] = {
|
|
|
|
{ "capabilities", cmd_capabilities, 0 },
|
|
|
|
{ "import", cmd_import, 1 },
|
|
|
|
{ "list", cmd_list, 0 },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cmd_capabilities(const char *line)
|
|
|
|
{
|
|
|
|
printf("import\n");
|
|
|
|
printf("bidi-import\n");
|
|
|
|
printf("refspec %s:%s\n\n", remote_ref, private_ref);
|
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void terminate_batch(void)
|
|
|
|
{
|
|
|
|
/* terminate a current batch's fast-import stream */
|
|
|
|
printf("done\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2012-09-19 23:21:27 +08:00
|
|
|
/* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */
|
2017-05-31 01:30:43 +08:00
|
|
|
static char *read_ref_note(const struct object_id *oid)
|
2012-09-19 23:21:27 +08:00
|
|
|
{
|
2017-05-31 01:30:40 +08:00
|
|
|
const struct object_id *note_oid;
|
2012-09-19 23:21:27 +08:00
|
|
|
char *msg = NULL;
|
|
|
|
unsigned long msglen;
|
|
|
|
enum object_type type;
|
|
|
|
|
|
|
|
init_notes(NULL, notes_ref, NULL, 0);
|
2017-05-31 01:30:43 +08:00
|
|
|
if (!(note_oid = get_note(NULL, oid)))
|
2012-09-19 23:21:27 +08:00
|
|
|
return NULL; /* note tree not found */
|
2017-05-31 01:30:40 +08:00
|
|
|
if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen)))
|
2012-09-19 23:21:27 +08:00
|
|
|
error("Empty notes tree. %s", notes_ref);
|
|
|
|
else if (!msglen || type != OBJ_BLOB) {
|
|
|
|
error("Note contains unusable content. "
|
|
|
|
"Is something else using this notes tree? %s", notes_ref);
|
2017-06-16 07:15:46 +08:00
|
|
|
FREE_AND_NULL(msg);
|
2012-09-19 23:21:27 +08:00
|
|
|
}
|
|
|
|
free_notes(NULL);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_rev_note(const char *msg, struct rev_note *res)
|
|
|
|
{
|
|
|
|
const char *key, *value, *end;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
while (*msg) {
|
2014-03-08 14:48:31 +08:00
|
|
|
end = strchrnul(msg, '\n');
|
|
|
|
len = end - msg;
|
2012-09-19 23:21:27 +08:00
|
|
|
|
|
|
|
key = "Revision-number: ";
|
2013-12-01 04:55:40 +08:00
|
|
|
if (starts_with(msg, key)) {
|
2012-09-19 23:21:27 +08:00
|
|
|
long i;
|
|
|
|
char *end;
|
|
|
|
value = msg + strlen(key);
|
|
|
|
i = strtol(value, &end, 0);
|
|
|
|
if (end == value || i < 0 || i > UINT32_MAX)
|
|
|
|
return -1;
|
|
|
|
res->rev_nr = i;
|
remote-testsvn: fix unitialized variable
In remote-test-svn, there is a parse_rev_note function to
parse lines of the form "Revision-number" from notes. If it
finds such a line and parses it, it returns 0, copying the
value into a "struct rev_note". If it finds an entry that is
garbled or out of range, it returns -1 to signal an error.
However, if it does not find any "Revision-number" line at
all, it returns success but does not put anything into the
rev_note. So upon a successful return, the rev_note may or
may not be initialized, and the caller has no way of
knowing.
gcc does not usually catch the use of the unitialized
variable because the conditional assignment happens in a
separate function from the point of use. However, when
compiling with -O3, gcc will inline parse_rev_note and
notice the problem.
We can fix it by returning "-1" when no note is found (so on
a zero return, we always found a valid value).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-12-15 06:11:44 +08:00
|
|
|
return 0;
|
2012-09-19 23:21:27 +08:00
|
|
|
}
|
|
|
|
msg += len + 1;
|
|
|
|
}
|
remote-testsvn: fix unitialized variable
In remote-test-svn, there is a parse_rev_note function to
parse lines of the form "Revision-number" from notes. If it
finds such a line and parses it, it returns 0, copying the
value into a "struct rev_note". If it finds an entry that is
garbled or out of range, it returns -1 to signal an error.
However, if it does not find any "Revision-number" line at
all, it returns success but does not put anything into the
rev_note. So upon a successful return, the rev_note may or
may not be initialized, and the caller has no way of
knowing.
gcc does not usually catch the use of the unitialized
variable because the conditional assignment happens in a
separate function from the point of use. However, when
compiling with -O3, gcc will inline parse_rev_note and
notice the problem.
We can fix it by returning "-1" when no note is found (so on
a zero return, we always found a valid value).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-12-15 06:11:44 +08:00
|
|
|
/* didn't find it */
|
|
|
|
return -1;
|
2012-09-19 23:21:27 +08:00
|
|
|
}
|
|
|
|
|
2017-05-31 01:30:39 +08:00
|
|
|
static int note2mark_cb(const struct object_id *object_oid,
|
|
|
|
const struct object_id *note_oid, char *note_path,
|
2012-09-19 23:21:29 +08:00
|
|
|
void *cb_data)
|
|
|
|
{
|
|
|
|
FILE *file = (FILE *)cb_data;
|
|
|
|
char *msg;
|
|
|
|
unsigned long msglen;
|
|
|
|
enum object_type type;
|
|
|
|
struct rev_note note;
|
|
|
|
|
2017-05-31 01:30:39 +08:00
|
|
|
if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen)) ||
|
2012-09-19 23:21:29 +08:00
|
|
|
!msglen || type != OBJ_BLOB) {
|
|
|
|
free(msg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (parse_rev_note(msg, ¬e))
|
|
|
|
return 2;
|
2017-05-31 01:30:39 +08:00
|
|
|
if (fprintf(file, ":%d %s\n", note.rev_nr, oid_to_hex(object_oid)) < 1)
|
2012-09-19 23:21:29 +08:00
|
|
|
return 3;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void regenerate_marks(void)
|
|
|
|
{
|
|
|
|
int ret;
|
2017-05-03 18:16:46 +08:00
|
|
|
FILE *marksfile = xfopen(marksfilename, "w+");
|
2012-09-19 23:21:29 +08:00
|
|
|
|
|
|
|
ret = for_each_note(NULL, 0, note2mark_cb, marksfile);
|
|
|
|
if (ret)
|
|
|
|
die("Regeneration of marks failed, returned %d.", ret);
|
|
|
|
fclose(marksfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_or_regenerate_marks(int latestrev)
|
|
|
|
{
|
|
|
|
FILE *marksfile;
|
|
|
|
struct strbuf sb = STRBUF_INIT;
|
|
|
|
struct strbuf line = STRBUF_INIT;
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
if (latestrev < 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
init_notes(NULL, notes_ref, NULL, 0);
|
|
|
|
marksfile = fopen(marksfilename, "r");
|
|
|
|
if (!marksfile) {
|
|
|
|
regenerate_marks();
|
2017-05-03 18:16:46 +08:00
|
|
|
marksfile = xfopen(marksfilename, "r");
|
2012-09-19 23:21:29 +08:00
|
|
|
fclose(marksfile);
|
|
|
|
} else {
|
|
|
|
strbuf_addf(&sb, ":%d ", latestrev);
|
2016-01-14 07:31:17 +08:00
|
|
|
while (strbuf_getline_lf(&line, marksfile) != EOF) {
|
2013-12-01 04:55:40 +08:00
|
|
|
if (starts_with(line.buf, sb.buf)) {
|
2012-09-19 23:21:29 +08:00
|
|
|
found++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(marksfile);
|
|
|
|
if (!found)
|
|
|
|
regenerate_marks();
|
|
|
|
}
|
|
|
|
free_notes(NULL);
|
|
|
|
strbuf_release(&sb);
|
|
|
|
strbuf_release(&line);
|
|
|
|
}
|
|
|
|
|
2012-09-19 23:21:15 +08:00
|
|
|
static int cmd_import(const char *line)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
int dumpin_fd;
|
2012-09-19 23:21:27 +08:00
|
|
|
char *note_msg;
|
2017-05-31 01:30:43 +08:00
|
|
|
struct object_id head_oid;
|
2012-09-19 23:21:27 +08:00
|
|
|
unsigned int startrev;
|
2014-08-20 03:09:35 +08:00
|
|
|
struct child_process svndump_proc = CHILD_PROCESS_INIT;
|
2014-07-18 23:20:19 +08:00
|
|
|
const char *command = "svnrdump";
|
2012-09-19 23:21:15 +08:00
|
|
|
|
2017-05-31 01:30:43 +08:00
|
|
|
if (read_ref(private_ref, head_oid.hash))
|
2012-09-19 23:21:27 +08:00
|
|
|
startrev = 0;
|
|
|
|
else {
|
2017-05-31 01:30:43 +08:00
|
|
|
note_msg = read_ref_note(&head_oid);
|
2012-09-19 23:21:27 +08:00
|
|
|
if(note_msg == NULL) {
|
|
|
|
warning("No note found for %s.", private_ref);
|
|
|
|
startrev = 0;
|
|
|
|
} else {
|
|
|
|
struct rev_note note = { 0 };
|
|
|
|
if (parse_rev_note(note_msg, ¬e))
|
|
|
|
die("Revision number couldn't be parsed from note.");
|
|
|
|
startrev = note.rev_nr + 1;
|
|
|
|
free(note_msg);
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 23:21:29 +08:00
|
|
|
check_or_regenerate_marks(startrev - 1);
|
2012-09-19 23:21:27 +08:00
|
|
|
|
2012-09-19 23:21:23 +08:00
|
|
|
if (dump_from_file) {
|
|
|
|
dumpin_fd = open(url, O_RDONLY);
|
|
|
|
if(dumpin_fd < 0)
|
|
|
|
die_errno("Couldn't open svn dump file %s.", url);
|
|
|
|
} else {
|
|
|
|
svndump_proc.out = -1;
|
2014-07-18 23:20:19 +08:00
|
|
|
argv_array_push(&svndump_proc.args, command);
|
|
|
|
argv_array_push(&svndump_proc.args, "dump");
|
|
|
|
argv_array_push(&svndump_proc.args, url);
|
|
|
|
argv_array_pushf(&svndump_proc.args, "-r%u:HEAD", startrev);
|
2012-09-19 23:21:23 +08:00
|
|
|
|
|
|
|
code = start_command(&svndump_proc);
|
|
|
|
if (code)
|
2014-07-18 23:20:19 +08:00
|
|
|
die("Unable to start %s, code %d", command, code);
|
2012-09-19 23:21:23 +08:00
|
|
|
dumpin_fd = svndump_proc.out;
|
|
|
|
}
|
2012-09-19 23:21:26 +08:00
|
|
|
/* setup marks file import/export */
|
|
|
|
printf("feature import-marks-if-exists=%s\n"
|
|
|
|
"feature export-marks=%s\n", marksfilename, marksfilename);
|
|
|
|
|
2012-09-19 23:21:15 +08:00
|
|
|
svndump_init_fd(dumpin_fd, STDIN_FILENO);
|
2012-09-19 23:21:27 +08:00
|
|
|
svndump_read(url, private_ref, notes_ref);
|
2012-09-19 23:21:15 +08:00
|
|
|
svndump_deinit();
|
|
|
|
svndump_reset();
|
|
|
|
|
|
|
|
close(dumpin_fd);
|
2012-09-19 23:21:23 +08:00
|
|
|
if (!dump_from_file) {
|
|
|
|
code = finish_command(&svndump_proc);
|
|
|
|
if (code)
|
2014-07-18 23:20:19 +08:00
|
|
|
warning("%s, returned %d", command, code);
|
2012-09-19 23:21:23 +08:00
|
|
|
}
|
2012-09-19 23:21:15 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd_list(const char *line)
|
|
|
|
{
|
|
|
|
printf("? %s\n\n", remote_ref);
|
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_command(struct strbuf *line)
|
|
|
|
{
|
|
|
|
const struct input_command_entry *p = input_command_list;
|
|
|
|
static struct string_list batchlines = STRING_LIST_INIT_DUP;
|
|
|
|
static const struct input_command_entry *batch_cmd;
|
|
|
|
/*
|
|
|
|
* commands can be grouped together in a batch.
|
|
|
|
* Batches are ended by \n. If no batch is active the program ends.
|
|
|
|
* During a batch all lines are buffered and passed to the handler function
|
|
|
|
* when the batch is terminated.
|
|
|
|
*/
|
|
|
|
if (line->len == 0) {
|
|
|
|
if (batch_cmd) {
|
|
|
|
struct string_list_item *item;
|
|
|
|
for_each_string_list_item(item, &batchlines)
|
|
|
|
batch_cmd->fn(item->string);
|
|
|
|
terminate_batch();
|
|
|
|
batch_cmd = NULL;
|
|
|
|
string_list_clear(&batchlines, 0);
|
|
|
|
return 0; /* end of the batch, continue reading other commands. */
|
|
|
|
}
|
|
|
|
return 1; /* end of command stream, quit */
|
|
|
|
}
|
|
|
|
if (batch_cmd) {
|
2013-12-01 04:55:40 +08:00
|
|
|
if (!starts_with(batch_cmd->name, line->buf))
|
2012-09-19 23:21:15 +08:00
|
|
|
die("Active %s batch interrupted by %s", batch_cmd->name, line->buf);
|
|
|
|
/* buffer batch lines */
|
|
|
|
string_list_append(&batchlines, line->buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = input_command_list; p->name; p++) {
|
2013-12-01 04:55:40 +08:00
|
|
|
if (starts_with(line->buf, p->name) && (strlen(p->name) == line->len ||
|
2012-09-19 23:21:15 +08:00
|
|
|
line->buf[strlen(p->name)] == ' ')) {
|
|
|
|
if (p->batchable) {
|
|
|
|
batch_cmd = p;
|
|
|
|
string_list_append(&batchlines, line->buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return p->fn(line->buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
die("Unknown command '%s'\n", line->buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
add an extra level of indirection to main()
There are certain startup tasks that we expect every git
process to do. In some cases this is just to improve the
quality of the program (e.g., setting up gettext()). In
others it is a requirement for using certain functions in
libgit.a (e.g., system_path() expects that you have called
git_extract_argv0_path()).
Most commands are builtins and are covered by the git.c
version of main(). However, there are still a few external
commands that use their own main(). Each of these has to
remember to include the correct startup sequence, and we are
not always consistent.
Rather than just fix the inconsistencies, let's make this
harder to get wrong by providing a common main() that can
run this standard startup.
We basically have two options to do this:
- the compat/mingw.h file already does something like this by
adding a #define that replaces the definition of main with a
wrapper that calls mingw_startup().
The upside is that the code in each program doesn't need
to be changed at all; it's rewritten on the fly by the
preprocessor.
The downside is that it may make debugging of the startup
sequence a bit more confusing, as the preprocessor is
quietly inserting new code.
- the builtin functions are all of the form cmd_foo(),
and git.c's main() calls them.
This is much more explicit, which may make things more
obvious to somebody reading the code. It's also more
flexible (because of course we have to figure out _which_
cmd_foo() to call).
The downside is that each of the builtins must define
cmd_foo(), instead of just main().
This patch chooses the latter option, preferring the more
explicit approach, even though it is more invasive. We
introduce a new file common-main.c, with the "real" main. It
expects to call cmd_main() from whatever other objects it is
linked against.
We link common-main.o against anything that links against
libgit.a, since we know that such programs will need to do
this setup. Note that common-main.o can't actually go inside
libgit.a, as the linker would not pick up its main()
function automatically (it has no callers).
The rest of the patch is just adjusting all of the various
external programs (mostly in t/helper) to use cmd_main().
I've provided a global declaration for cmd_main(), which
means that all of the programs also need to match its
signature. In particular, many functions need to switch to
"const char **" instead of "char **" for argv. This effect
ripples out to a few other variables and functions, as well.
This makes the patch even more invasive, but the end result
is much better. We should be treating argv strings as const
anyway, and now all programs conform to the same signature
(which also matches the way builtins are defined).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-07-01 13:58:58 +08:00
|
|
|
int cmd_main(int argc, const char **argv)
|
2012-09-19 23:21:15 +08:00
|
|
|
{
|
|
|
|
struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
|
2012-09-19 23:21:27 +08:00
|
|
|
private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT,
|
|
|
|
notes_ref_sb = STRBUF_INIT;
|
2012-09-19 23:21:15 +08:00
|
|
|
static struct remote *remote;
|
|
|
|
const char *url_in;
|
|
|
|
|
|
|
|
setup_git_directory();
|
|
|
|
if (argc < 2 || argc > 3) {
|
|
|
|
usage("git-remote-svn <remote-name> [<url>]");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
remote = remote_get(argv[1]);
|
|
|
|
url_in = (argc == 3) ? argv[2] : remote->url[0];
|
|
|
|
|
2013-12-01 04:55:40 +08:00
|
|
|
if (starts_with(url_in, "file://")) {
|
2012-09-19 23:21:23 +08:00
|
|
|
dump_from_file = 1;
|
|
|
|
url = url_decode(url_in + sizeof("file://")-1);
|
|
|
|
} else {
|
|
|
|
dump_from_file = 0;
|
|
|
|
end_url_with_slash(&url_sb, url_in);
|
|
|
|
url = url_sb.buf;
|
|
|
|
}
|
2012-09-19 23:21:15 +08:00
|
|
|
|
|
|
|
strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
|
|
|
|
private_ref = private_ref_sb.buf;
|
|
|
|
|
2012-09-19 23:21:27 +08:00
|
|
|
strbuf_addf(¬es_ref_sb, "refs/notes/%s/revs", remote->name);
|
|
|
|
notes_ref = notes_ref_sb.buf;
|
|
|
|
|
2012-09-19 23:21:26 +08:00
|
|
|
strbuf_addf(&marksfilename_sb, "%s/info/fast-import/remote-svn/%s.marks",
|
|
|
|
get_git_dir(), remote->name);
|
|
|
|
marksfilename = marksfilename_sb.buf;
|
|
|
|
|
2012-09-19 23:21:15 +08:00
|
|
|
while (1) {
|
2016-01-14 07:31:17 +08:00
|
|
|
if (strbuf_getline_lf(&buf, stdin) == EOF) {
|
2012-09-19 23:21:15 +08:00
|
|
|
if (ferror(stdin))
|
|
|
|
die("Error reading command stream");
|
|
|
|
else
|
|
|
|
die("Unexpected end of command stream");
|
|
|
|
}
|
|
|
|
if (do_command(&buf))
|
|
|
|
break;
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_release(&buf);
|
|
|
|
strbuf_release(&url_sb);
|
|
|
|
strbuf_release(&private_ref_sb);
|
2012-09-19 23:21:27 +08:00
|
|
|
strbuf_release(¬es_ref_sb);
|
2012-09-19 23:21:26 +08:00
|
|
|
strbuf_release(&marksfilename_sb);
|
2012-09-19 23:21:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|