Code

Make pull() support fetching multiple targets at once
authorPetr Baudis <pasky@suse.cz>
Thu, 27 Jul 2006 21:56:17 +0000 (23:56 +0200)
committerJunio C Hamano <junkio@cox.net>
Fri, 28 Jul 2006 02:33:48 +0000 (19:33 -0700)
pull() now takes an array of arguments instead of just one of each kind.
Currently, no users use the new capability, but that'll change.

Signed-off-by: Petr Baudis <pasky@suse.cz>
Signed-off-by: Junio C Hamano <junkio@cox.net>
fetch.c
fetch.h
http-fetch.c
local-fetch.c
ssh-fetch.c

diff --git a/fetch.c b/fetch.c
index 3255cc62507e18b7d7da5cbf26a1238402a93314..281df61e7be1a998c4b384ee06bcdabaecd83504 100644 (file)
--- a/fetch.c
+++ b/fetch.c
@@ -210,55 +210,67 @@ static int mark_complete(const char *path, const unsigned char *sha1)
        return 0;
 }
 
-int pull(char *target, const char *write_ref,
+int pull(int targets, char **target, const char **write_ref,
          const char *write_ref_log_details)
 {
-       struct ref_lock *lock = NULL;
-       unsigned char sha1[20];
+       struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
+       unsigned char *sha1 = xmalloc(targets * 20);
        char *msg;
        int ret;
+       int i;
 
        save_commit_buffer = 0;
        track_object_refs = 0;
-       if (write_ref) {
-               lock = lock_ref_sha1(write_ref, NULL, 0);
-               if (!lock) {
-                       error("Can't lock ref %s", write_ref);
-                       return -1;
+
+       for (i = 0; i < targets; i++) {
+               if (!write_ref[i])
+                       continue;
+
+               lock[i] = lock_ref_sha1(write_ref[i], NULL, 0);
+               if (!lock[i]) {
+                       error("Can't lock ref %s", write_ref[i]);
+                       goto unlock_and_fail;
                }
        }
 
        if (!get_recover)
                for_each_ref(mark_complete);
 
-       if (interpret_target(target, sha1)) {
-               error("Could not interpret %s as something to pull", target);
-               if (lock)
-                       unlock_ref(lock);
-               return -1;
+       for (i = 0; i < targets; i++) {
+               if (interpret_target(target[i], &sha1[20 * i])) {
+                       error("Could not interpret %s as something to pull", target[i]);
+                       goto unlock_and_fail;
+               }
+               if (process(lookup_unknown_object(&sha1[20 * i])))
+                       goto unlock_and_fail;
        }
-       if (process(lookup_unknown_object(sha1))) {
-               if (lock)
-                       unlock_ref(lock);
-               return -1;
+
+       if (loop())
+               goto unlock_and_fail;
+
+       if (write_ref_log_details) {
+               msg = xmalloc(strlen(write_ref_log_details) + 12);
+               sprintf(msg, "fetch from %s", write_ref_log_details);
+       } else {
+               msg = NULL;
        }
-       if (loop()) {
-               if (lock)
-                       unlock_ref(lock);
-               return -1;
+       for (i = 0; i < targets; i++) {
+               if (!write_ref[i])
+                       continue;
+               ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
+               lock[i] = NULL;
+               if (ret)
+                       goto unlock_and_fail;
        }
+       if (msg)
+               free(msg);
 
-       if (write_ref) {
-               if (write_ref_log_details) {
-                       msg = xmalloc(strlen(write_ref_log_details) + 12);
-                       sprintf(msg, "fetch from %s", write_ref_log_details);
-               }
-               else
-                       msg = NULL;
-               ret = write_ref_sha1(lock, sha1, msg ? msg : "fetch (unknown)");
-               if (msg)
-                       free(msg);
-               return ret;
-       }
        return 0;
+
+
+unlock_and_fail:
+       for (i = 0; i < targets; i++)
+               if (lock[i])
+                       unlock_ref(lock[i]);
+       return -1;
 }
diff --git a/fetch.h b/fetch.h
index 7bda355bc1ce8f345b926c129bf7b60e48a8ec4f..75e48af78029d0ca352f2e429c62248d05a70db1 100644 (file)
--- a/fetch.h
+++ b/fetch.h
@@ -42,7 +42,7 @@ extern void pull_say(const char *, const char *);
 
 /* If write_ref is set, the ref filename to write the target value to. */
 /* If write_ref_log_details is set, additional text will appear in the ref log. */
-extern int pull(char *target, const char *write_ref,
+extern int pull(int targets, char **target, const char **write_ref,
                const char *write_ref_log_details);
 
 #endif /* PULL_H */
index 963d439f822e6984560db47a6f4492c1cb8bc455..bc67db1add945438ee2847f96bbefa0086b853a5 100644 (file)
@@ -1268,7 +1268,7 @@ int main(int argc, char **argv)
                        alt->path_len = strlen(path);
        }
 
-       if (pull(commit_id, write_ref, url))
+       if (pull(1, &commit_id, &write_ref, url))
                rc = 1;
 
        http_cleanup();
index 452b83396c89f3f0baa6447b09c77c13327d2f5f..1be73904f1b56fe0981973e9c21a368a4bba7975 100644 (file)
@@ -242,7 +242,7 @@ int main(int argc, char **argv)
        commit_id = argv[arg];
        path = argv[arg + 1];
 
-       if (pull(commit_id, write_ref, path))
+       if (pull(1, &commit_id, &write_ref, path))
                return 1;
 
        return 0;
index aef3aa4a0578c0b6c45fa20e3558e46bea4fd225..6e16568f88f6fcbbb79d23c1f6187860c8cfb453 100644 (file)
@@ -167,7 +167,7 @@ int main(int argc, char **argv)
        if (get_version())
                return 1;
 
-       if (pull(commit_id, write_ref, url))
+       if (pull(1, &commit_id, &write_ref, url))
                return 1;
 
        return 0;