]> git.ipfire.org Git - thirdparty/git.git/blobdiff - bundle.c
Enhance hold_lock_file_for_{update,append}() API
[thirdparty/git.git] / bundle.c
index 9b9b9166df05e984dc571462f333aeaee9cdcd23..7d17a1fde16204859849aaf28945739aaa685f91 100644 (file)
--- a/bundle.c
+++ b/bundle.c
@@ -48,7 +48,7 @@ int read_bundle_header(const char *path, struct bundle_header *header)
                        : &header->references;
                char delim;
 
-               if (buffer[len - 1] == '\n')
+               if (len && buffer[len - 1] == '\n')
                        buffer[len - 1] = '\0';
                if (get_sha1_hex(buffer + offset, sha1)) {
                        warning("unrecognized header: %s", buffer);
@@ -128,7 +128,8 @@ int verify_bundle(struct bundle_header *header, int verbose)
                add_object_array(e->item, e->name, &refs);
        }
 
-       prepare_revision_walk(&revs);
+       if (prepare_revision_walk(&revs))
+               die("revision walk setup failed");
 
        i = req_nr;
        while (i && (commit = get_revision(&revs)))
@@ -177,6 +178,7 @@ int create_bundle(struct bundle_header *header, const char *path,
        int i, ref_count = 0;
        char buffer[1024];
        struct rev_info revs;
+       int read_from_stdin = 0;
        struct child_process rls;
        FILE *rls_fout;
 
@@ -184,7 +186,8 @@ int create_bundle(struct bundle_header *header, const char *path,
        if (bundle_to_stdout)
                bundle_fd = 1;
        else
-               bundle_fd = hold_lock_file_for_update(&lock, path, 1);
+               bundle_fd = hold_lock_file_for_update(&lock, path,
+                                                     LOCK_DIE_ON_ERROR);
 
        /* write signature */
        write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
@@ -226,8 +229,16 @@ int create_bundle(struct bundle_header *header, const char *path,
 
        /* write references */
        argc = setup_revisions(argc, argv, &revs, NULL);
-       if (argc > 1)
-               return error("unrecognized argument: %s'", argv[1]);
+
+       for (i = 1; i < argc; i++) {
+               if (!strcmp(argv[i], "--stdin")) {
+                       if (read_from_stdin++)
+                               die("--stdin given twice?");
+                       read_revisions_from_stdin(&revs);
+                       continue;
+               }
+               return error("unrecognized argument: %s'", argv[i]);
+       }
 
        for (i = 0; i < revs.pending.nr; i++) {
                struct object_array_entry *e = revs.pending.objects + i;
@@ -317,16 +328,24 @@ int create_bundle(struct bundle_header *header, const char *path,
        rls.git_cmd = 1;
        if (start_command(&rls))
                return error("Could not spawn pack-objects");
+
+       /*
+        * start_command closed bundle_fd if it was > 1
+        * so set the lock fd to -1 so commit_lock_file()
+        * won't fail trying to close it.
+        */
+       lock.fd = -1;
+
        for (i = 0; i < revs.pending.nr; i++) {
                struct object *object = revs.pending.objects[i].item;
                if (object->flags & UNINTERESTING)
-                       write(rls.in, "^", 1);
-               write(rls.in, sha1_to_hex(object->sha1), 40);
-               write(rls.in, "\n", 1);
+                       write_or_die(rls.in, "^", 1);
+               write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
+               write_or_die(rls.in, "\n", 1);
        }
+       close(rls.in);
        if (finish_command(&rls))
                return error ("pack-objects died");
-       close(bundle_fd);
        if (!bundle_to_stdout)
                commit_lock_file(&lock);
        return 0;