Code

Merge branch 'cb/ignored-paths-are-precious' into pu
[git.git] / builtin / checkout.c
index 757f9a08ddbaf102726a781b06d7294a4638984e..0ca8f6a2061b177410136fb89b988806a05eb16e 100644 (file)
@@ -399,9 +399,11 @@ static int merge_working_tree(struct checkout_opts *opts,
                topts.gently = opts->merge && old->commit;
                topts.verbose_update = !opts->quiet;
                topts.fn = twoway_merge;
-               topts.dir = xcalloc(1, sizeof(*topts.dir));
-               topts.dir->flags |= DIR_SHOW_IGNORED;
-               topts.dir->exclude_per_dir = ".gitignore";
+               if (!ignored_are_precious) {
+                       topts.dir = xcalloc(1, sizeof(*topts.dir));
+                       topts.dir->flags |= DIR_SHOW_IGNORED;
+                       topts.dir->exclude_per_dir = ".gitignore";
+               }
                tree = parse_tree_indirect(old->commit ?
                                           old->commit->object.sha1 :
                                           (unsigned char *)EMPTY_TREE_SHA1_BIN);
@@ -678,7 +680,7 @@ static const char *unique_tracking_name(const char *name)
 int cmd_checkout(int argc, const char **argv, const char *prefix)
 {
        struct checkout_opts opts;
-       unsigned char rev[20];
+       unsigned char rev[20], branch_rev[20];
        const char *arg;
        struct branch_info new;
        struct tree *source_tree = NULL;
@@ -700,6 +702,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                OPT_SET_INT('3', "theirs", &opts.writeout_stage, "checkout their version for unmerged files",
                            3),
                OPT__FORCE(&opts.force, "force checkout (throw away local modifications)"),
+               OPT_BOOLEAN('i', "ignored-are-precious", &ignored_are_precious,
+                 "fail when an ignored file needs to be overwritten"),
                OPT_BOOLEAN('m', "merge", &opts.merge, "perform a 3-way merge with the new branch"),
                OPT_STRING(0, "conflict", &conflict_style, "style",
                           "conflict style (merge or diff3)"),
@@ -832,18 +836,21 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                argc--;
 
                new.name = arg;
-               if ((new.commit = lookup_commit_reference_gently(rev, 1))) {
-                       setup_branch_path(&new);
+               setup_branch_path(&new);
 
-                       if ((check_ref_format(new.path) == CHECK_REF_FORMAT_OK) &&
-                           resolve_ref(new.path, rev, 1, NULL))
-                               ;
-                       else
-                               new.path = NULL;
+               if (check_ref_format(new.path) == CHECK_REF_FORMAT_OK &&
+                   resolve_ref(new.path, branch_rev, 1, NULL))
+                       hashcpy(rev, branch_rev);
+               else
+                       new.path = NULL; /* not an existing branch */
+
+               if (!(new.commit = lookup_commit_reference_gently(rev, 1))) {
+                       /* not a commit */
+                       source_tree = parse_tree_indirect(rev);
+               } else {
                        parse_commit(new.commit);
                        source_tree = new.commit->tree;
-               } else
-                       source_tree = parse_tree_indirect(rev);
+               }
 
                if (!source_tree)                   /* case (1): want a tree */
                        die("reference is not a tree: %s", arg);