pack-objects: Allow missing base objects when creating thin packs
[gitweb.git] / builtin-checkout.c
index d6641c2c562c6b35bfed1cd3053945b460472581..411cc513c65ba854221ad52dd6aeaaac7d213c9d 100644 (file)
@@ -43,7 +43,7 @@ static int post_checkout_hook(struct commit *old, struct commit *new,
 }
 
 static int update_some(const unsigned char *sha1, const char *base, int baselen,
-                      const char *pathname, unsigned mode, int stage)
+               const char *pathname, unsigned mode, int stage, void *context)
 {
        int len;
        struct cache_entry *ce;
@@ -67,7 +67,7 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
 
 static int read_tree_some(struct tree *tree, const char **pathspec)
 {
-       read_tree_recursive(tree, "", 0, 0, pathspec, update_some);
+       read_tree_recursive(tree, "", 0, 0, pathspec, update_some, NULL);
 
        /* update the index with the given tree's info
         * for all args, expanding wildcards, and exit
@@ -430,6 +430,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                OPT_BOOLEAN('m', NULL, &opts.merge, "merge"),
                OPT_END(),
        };
+       int has_dash_dash;
 
        memset(&opts, 0, sizeof(opts));
        memset(&new, 0, sizeof(new));
@@ -438,12 +439,57 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 
        opts.track = git_branch_track;
 
-       argc = parse_options(argc, argv, options, checkout_usage, 0);
+       argc = parse_options(argc, argv, options, checkout_usage,
+                            PARSE_OPT_KEEP_DASHDASH);
+
+       if (!opts.new_branch && (opts.track != git_branch_track))
+               die("git checkout: --track and --no-track require -b");
+
+       if (opts.force && opts.merge)
+               die("git checkout: -f and -m are incompatible");
+
+       /*
+        * case 1: git checkout <ref> -- [<paths>]
+        *
+        *   <ref> must be a valid tree, everything after the '--' must be
+        *   a path.
+        *
+        * case 2: git checkout -- [<paths>]
+        *
+        *   everything after the '--' must be paths.
+        *
+        * case 3: git checkout <something> [<paths>]
+        *
+        *   With no paths, if <something> is a commit, that is to
+        *   switch to the branch or detach HEAD at it.
+        *
+        *   Otherwise <something> shall not be ambiguous.
+        *   - If it's *only* a reference, treat it like case (1).
+        *   - If it's only a path, treat it like case (2).
+        *   - else: fail.
+        *
+        */
        if (argc) {
+               if (!strcmp(argv[0], "--")) {       /* case (2) */
+                       argv++;
+                       argc--;
+                       goto no_reference;
+               }
+
                arg = argv[0];
-               if (get_sha1(arg, rev))
-                       ;
-               else if ((new.commit = lookup_commit_reference_gently(rev, 1))) {
+               has_dash_dash = (argc > 1) && !strcmp(argv[1], "--");
+
+               if (get_sha1(arg, rev)) {
+                       if (has_dash_dash)          /* case (1) */
+                               die("invalid reference: %s", arg);
+                       goto no_reference;          /* case (3 -> 2) */
+               }
+
+               /* we can't end up being in (2) anymore, eat the argument */
+               argv++;
+               argc--;
+
+               if ((new.commit = lookup_commit_reference_gently(rev, 1))) {
                        new.name = arg;
                        setup_branch_path(&new);
                        if (resolve_ref(new.path, rev, 1, NULL))
@@ -452,25 +498,28 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                                new.path = NULL;
                        parse_commit(new.commit);
                        source_tree = new.commit->tree;
-                       argv++;
-                       argc--;
-               } else if ((source_tree = parse_tree_indirect(rev))) {
+               } else
+                       source_tree = parse_tree_indirect(rev);
+
+               if (!source_tree)                   /* case (1): want a tree */
+                       die("reference is not a tree: %s", arg);
+               if (!has_dash_dash) {/* case (3 -> 1) */
+                       /*
+                        * Do not complain the most common case
+                        *      git checkout branch
+                        * even if there happen to be a file called 'branch';
+                        * it would be extremely annoying.
+                        */
+                       if (argc)
+                               verify_non_filename(NULL, arg);
+               }
+               else {
                        argv++;
                        argc--;
                }
        }
 
-       if (argc && !strcmp(argv[0], "--")) {
-               argv++;
-               argc--;
-       }
-
-       if (!opts.new_branch && (opts.track != git_branch_track))
-               die("git checkout: --track and --no-track require -b");
-
-       if (opts.force && opts.merge)
-               die("git checkout: -f and -m are incompatible");
-
+no_reference:
        if (argc) {
                const char **pathspec = get_pathspec(prefix, argv);