Imported Upstream version 2.14.3
[platform/upstream/git.git] / remote-testsvn.c
index 686e07d..0ff4a31 100644 (file)
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "refs.h"
 #include "remote.h"
 #include "strbuf.h"
 #include "url.h"
@@ -50,23 +51,22 @@ static void terminate_batch(void)
 }
 
 /* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */
-static char *read_ref_note(const unsigned char sha1[20])
+static char *read_ref_note(const struct object_id *oid)
 {
-       const unsigned char *note_sha1;
+       const struct object_id *note_oid;
        char *msg = NULL;
        unsigned long msglen;
        enum object_type type;
 
        init_notes(NULL, notes_ref, NULL, 0);
-       if (!(note_sha1 = get_note(NULL, sha1)))
+       if (!(note_oid = get_note(NULL, oid)))
                return NULL;    /* note tree not found */
-       if (!(msg = read_sha1_file(note_sha1, &type, &msglen)))
+       if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen)))
                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);
-               free(msg);
-               msg = NULL;
+               FREE_AND_NULL(msg);
        }
        free_notes(NULL);
        return msg;
@@ -98,8 +98,8 @@ static int parse_rev_note(const char *msg, struct rev_note *res)
        return -1;
 }
 
-static int note2mark_cb(const unsigned char *object_sha1,
-               const unsigned char *note_sha1, char *note_path,
+static int note2mark_cb(const struct object_id *object_oid,
+               const struct object_id *note_oid, char *note_path,
                void *cb_data)
 {
        FILE *file = (FILE *)cb_data;
@@ -108,14 +108,14 @@ static int note2mark_cb(const unsigned char *object_sha1,
        enum object_type type;
        struct rev_note note;
 
-       if (!(msg = read_sha1_file(note_sha1, &type, &msglen)) ||
+       if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen)) ||
                        !msglen || type != OBJ_BLOB) {
                free(msg);
                return 1;
        }
        if (parse_rev_note(msg, &note))
                return 2;
-       if (fprintf(file, ":%d %s\n", note.rev_nr, sha1_to_hex(object_sha1)) < 1)
+       if (fprintf(file, ":%d %s\n", note.rev_nr, oid_to_hex(object_oid)) < 1)
                return 3;
        return 0;
 }
@@ -123,10 +123,8 @@ static int note2mark_cb(const unsigned char *object_sha1,
 static void regenerate_marks(void)
 {
        int ret;
-       FILE *marksfile = fopen(marksfilename, "w+");
+       FILE *marksfile = xfopen(marksfilename, "w+");
 
-       if (!marksfile)
-               die_errno("Couldn't create mark file %s.", marksfilename);
        ret = for_each_note(NULL, 0, note2mark_cb, marksfile);
        if (ret)
                die("Regeneration of marks failed, returned %d.", ret);
@@ -147,13 +145,11 @@ static void check_or_regenerate_marks(int latestrev)
        marksfile = fopen(marksfilename, "r");
        if (!marksfile) {
                regenerate_marks();
-               marksfile = fopen(marksfilename, "r");
-               if (!marksfile)
-                       die_errno("cannot read marks file %s!", marksfilename);
+               marksfile = xfopen(marksfilename, "r");
                fclose(marksfile);
        } else {
                strbuf_addf(&sb, ":%d ", latestrev);
-               while (strbuf_getline(&line, marksfile, '\n') != EOF) {
+               while (strbuf_getline_lf(&line, marksfile) != EOF) {
                        if (starts_with(line.buf, sb.buf)) {
                                found++;
                                break;
@@ -173,15 +169,15 @@ static int cmd_import(const char *line)
        int code;
        int dumpin_fd;
        char *note_msg;
-       unsigned char head_sha1[20];
+       struct object_id head_oid;
        unsigned int startrev;
-       struct child_process svndump_proc;
+       struct child_process svndump_proc = CHILD_PROCESS_INIT;
        const char *command = "svnrdump";
 
-       if (read_ref(private_ref, head_sha1))
+       if (read_ref(private_ref, head_oid.hash))
                startrev = 0;
        else {
-               note_msg = read_ref_note(head_sha1);
+               note_msg = read_ref_note(&head_oid);
                if(note_msg == NULL) {
                        warning("No note found for %s.", private_ref);
                        startrev = 0;
@@ -200,7 +196,6 @@ static int cmd_import(const char *line)
                if(dumpin_fd < 0)
                        die_errno("Couldn't open svn dump file %s.", url);
        } else {
-               memset(&svndump_proc, 0, sizeof(struct child_process));
                svndump_proc.out = -1;
                argv_array_push(&svndump_proc.args, command);
                argv_array_push(&svndump_proc.args, "dump");
@@ -284,7 +279,7 @@ static int do_command(struct strbuf *line)
        return 0;
 }
 
-int main(int argc, char **argv)
+int cmd_main(int argc, const char **argv)
 {
        struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
                        private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT,
@@ -292,7 +287,6 @@ int main(int argc, char **argv)
        static struct remote *remote;
        const char *url_in;
 
-       git_extract_argv0_path(argv[0]);
        setup_git_directory();
        if (argc < 2 || argc > 3) {
                usage("git-remote-svn <remote-name> [<url>]");
@@ -322,7 +316,7 @@ int main(int argc, char **argv)
        marksfilename = marksfilename_sb.buf;
 
        while (1) {
-               if (strbuf_getline(&buf, stdin, '\n') == EOF) {
+               if (strbuf_getline_lf(&buf, stdin) == EOF) {
                        if (ferror(stdin))
                                die("Error reading command stream");
                        else