ident.c: add support for IPv6
[gitweb.git] / builtin / replace.c
index 294b61b97e20ac9b5684bd6a180da37ebef43db9..6b3c469a331e62e13fb6f9626ee5a64f28747c06 100644 (file)
@@ -35,7 +35,7 @@ struct show_data {
        enum replace_format format;
 };
 
-static int show_reference(const char *refname, const unsigned char *sha1,
+static int show_reference(const char *refname, const struct object_id *oid,
                          int flag, void *cb_data)
 {
        struct show_data *data = cb_data;
@@ -44,19 +44,19 @@ static int show_reference(const char *refname, const unsigned char *sha1,
                if (data->format == REPLACE_FORMAT_SHORT)
                        printf("%s\n", refname);
                else if (data->format == REPLACE_FORMAT_MEDIUM)
-                       printf("%s -> %s\n", refname, sha1_to_hex(sha1));
+                       printf("%s -> %s\n", refname, oid_to_hex(oid));
                else { /* data->format == REPLACE_FORMAT_LONG */
-                       unsigned char object[20];
+                       struct object_id object;
                        enum object_type obj_type, repl_type;
 
-                       if (get_sha1(refname, object))
+                       if (get_sha1(refname, object.hash))
                                return error("Failed to resolve '%s' as a valid ref.", refname);
 
-                       obj_type = sha1_object_info(object, NULL);
-                       repl_type = sha1_object_info(sha1, NULL);
+                       obj_type = sha1_object_info(object.hash, NULL);
+                       repl_type = sha1_object_info(oid->hash, NULL);
 
                        printf("%s (%s) -> %s (%s)\n", refname, typename(obj_type),
-                              sha1_to_hex(sha1), typename(repl_type));
+                              oid_to_hex(oid), typename(repl_type));
                }
        }
 
@@ -82,7 +82,7 @@ static int list_replace_refs(const char *pattern, const char *format)
                    "valid formats are 'short', 'medium' and 'long'\n",
                    format);
 
-       for_each_replace_ref(show_reference, (void *) &data);
+       for_each_replace_ref(show_reference, (void *)&data);
 
        return 0;
 }
@@ -104,9 +104,9 @@ static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
                        continue;
                }
                full_hex = sha1_to_hex(sha1);
-               snprintf(ref, sizeof(ref), "refs/replace/%s", full_hex);
+               snprintf(ref, sizeof(ref), "%s%s", git_replace_ref_base, full_hex);
                /* read_ref() may reuse the buffer */
-               full_hex = ref + strlen("refs/replace/");
+               full_hex = ref + strlen(git_replace_ref_base);
                if (read_ref(ref, sha1)) {
                        error("replace ref '%s' not found.", full_hex);
                        had_error = 1;
@@ -134,7 +134,7 @@ static void check_ref_valid(unsigned char object[20],
                            int force)
 {
        if (snprintf(ref, ref_size,
-                    "refs/replace/%s",
+                    "%s%s", git_replace_ref_base,
                     sha1_to_hex(object)) > ref_size - 1)
                die("replace ref name too long: %.*s...", 50, ref);
        if (check_refname_format(ref, 0))
@@ -155,7 +155,8 @@ static int replace_object_sha1(const char *object_ref,
        unsigned char prev[20];
        enum object_type obj_type, repl_type;
        char ref[PATH_MAX];
-       struct ref_lock *lock;
+       struct ref_transaction *transaction;
+       struct strbuf err = STRBUF_INIT;
 
        obj_type = sha1_object_info(object, NULL);
        repl_type = sha1_object_info(repl, NULL);
@@ -168,12 +169,14 @@ static int replace_object_sha1(const char *object_ref,
 
        check_ref_valid(object, prev, ref, sizeof(ref), force);
 
-       lock = lock_any_ref_for_update(ref, prev, 0, NULL);
-       if (!lock)
-               die("%s: cannot lock the ref", ref);
-       if (write_ref_sha1(lock, repl, NULL) < 0)
-               die("%s: cannot update the ref", ref);
+       transaction = ref_transaction_begin(&err);
+       if (!transaction ||
+           ref_transaction_update(transaction, ref, repl, prev,
+                                  0, NULL, &err) ||
+           ref_transaction_commit(transaction, &err))
+               die("%s", err.buf);
 
+       ref_transaction_free(transaction);
        return 0;
 }
 
@@ -197,7 +200,7 @@ static int replace_object(const char *object_ref, const char *replace_ref, int f
 static void export_object(const unsigned char *sha1, enum object_type type,
                          int raw, const char *filename)
 {
-       struct child_process cmd = { NULL };
+       struct child_process cmd = CHILD_PROCESS_INIT;
        int fd;
 
        fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
@@ -234,7 +237,7 @@ static void import_object(unsigned char *sha1, enum object_type type,
 
        if (!raw && type == OBJ_TREE) {
                const char *argv[] = { "mktree", NULL };
-               struct child_process cmd = { argv };
+               struct child_process cmd = CHILD_PROCESS_INIT;
                struct strbuf result = STRBUF_INIT;
 
                cmd.argv = argv;