Fix a "label defined but unreferenced" warning.
[git.git] / builtin-pack-refs.c
index 042d2718f904303600a57cfe8b5de83f5c0d34db..3de9b3eefdaad43f0b0212b93055f26a9542533a 100644 (file)
@@ -1,8 +1,10 @@
 #include "cache.h"
 #include "refs.h"
+#include "object.h"
+#include "tag.h"
 
 static const char builtin_pack_refs_usage[] =
-"git-pack-refs [--all] [--prune]";
+"git-pack-refs [--all] [--prune | --no-prune]";
 
 struct ref_to_prune {
        struct ref_to_prune *next;
@@ -29,12 +31,28 @@ static int handle_one_ref(const char *path, const unsigned char *sha1,
                          int flags, void *cb_data)
 {
        struct pack_refs_cb_data *cb = cb_data;
+       int is_tag_ref;
 
-       if (!cb->all && strncmp(path, "refs/tags/", 10))
-               return 0;
        /* Do not pack the symbolic refs */
-       if (!(flags & REF_ISSYMREF))
-               fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
+       if ((flags & REF_ISSYMREF))
+               return 0;
+       is_tag_ref = !strncmp(path, "refs/tags/", 10);
+
+       /* ALWAYS pack refs that were already packed or are tags */
+       if (!cb->all && !is_tag_ref && !(flags & REF_ISPACKED))
+               return 0;
+
+       fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
+       if (is_tag_ref) {
+               struct object *o = parse_object(sha1);
+               if (o->type == OBJ_TAG) {
+                       o = deref_tag(o, path, 0);
+                       if (o)
+                               fprintf(cb->refs_file, "^%s\n",
+                                       sha1_to_hex(o->sha1));
+               }
+       }
+
        if (cb->prune && !do_not_prune(flags)) {
                int namelen = strlen(path) + 1;
                struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
@@ -74,10 +92,15 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
 
        memset(&cbdata, 0, sizeof(cbdata));
 
+       cbdata.prune = 1;
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
                if (!strcmp(arg, "--prune")) {
-                       cbdata.prune = 1;
+                       cbdata.prune = 1; /* now the default */
+                       continue;
+               }
+               if (!strcmp(arg, "--no-prune")) {
+                       cbdata.prune = 0;
                        continue;
                }
                if (!strcmp(arg, "--all")) {
@@ -95,6 +118,10 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
        if (!cbdata.refs_file)
                die("unable to create ref-pack file structure (%s)",
                    strerror(errno));
+
+       /* perhaps other traits later as well */
+       fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
+
        for_each_ref(handle_one_ref, &cbdata);
        fflush(cbdata.refs_file);
        fsync(fd);