Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 1 | #define USE_THE_INDEX_VARIABLE |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 2 | #include "builtin.h" |
Nguyễn Thái Ngọc Duy | 0dabeff | 2019-03-29 17:39:00 +0700 | [diff] [blame] | 3 | #include "advice.h" |
Nguyễn Thái Ngọc Duy | 0dabeff | 2019-03-29 17:39:00 +0700 | [diff] [blame] | 4 | #include "branch.h" |
| 5 | #include "cache-tree.h" |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 6 | #include "checkout.h" |
Nguyễn Thái Ngọc Duy | 0dabeff | 2019-03-29 17:39:00 +0700 | [diff] [blame] | 7 | #include "commit.h" |
| 8 | #include "config.h" |
| 9 | #include "diff.h" |
| 10 | #include "dir.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 11 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 12 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 13 | #include "hex.h" |
Emily Shaffer | 72ddf34 | 2021-12-22 04:59:35 +0100 | [diff] [blame] | 14 | #include "hook.h" |
Elijah Newren | 6723899 | 2023-05-16 06:34:04 +0000 | [diff] [blame] | 15 | #include "merge-ll.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 16 | #include "lockfile.h" |
Elijah Newren | 5bc0722 | 2023-04-11 00:41:47 -0700 | [diff] [blame] | 17 | #include "mem-pool.h" |
Nguyễn Thái Ngọc Duy | 0dabeff | 2019-03-29 17:39:00 +0700 | [diff] [blame] | 18 | #include "merge-recursive.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 19 | #include "object-name.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 20 | #include "object-store-ll.h" |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 21 | #include "parse-options.h" |
Elijah Newren | c339932 | 2023-05-16 06:33:59 +0000 | [diff] [blame] | 22 | #include "path.h" |
Elijah Newren | fbffdfb | 2023-05-16 06:33:52 +0000 | [diff] [blame] | 23 | #include "preload-index.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 24 | #include "read-cache.h" |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 25 | #include "refs.h" |
Nguyễn Thái Ngọc Duy | 0dabeff | 2019-03-29 17:39:00 +0700 | [diff] [blame] | 26 | #include "remote.h" |
| 27 | #include "resolve-undo.h" |
| 28 | #include "revision.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 29 | #include "setup.h" |
Nguyễn Thái Ngọc Duy | 0dabeff | 2019-03-29 17:39:00 +0700 | [diff] [blame] | 30 | #include "submodule.h" |
Elijah Newren | cb2a513 | 2023-04-22 20:17:09 +0000 | [diff] [blame] | 31 | #include "symlinks.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 32 | #include "trace2.h" |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 33 | #include "tree.h" |
| 34 | #include "tree-walk.h" |
| 35 | #include "unpack-trees.h" |
Nguyễn Thái Ngọc Duy | c45f0f5 | 2019-03-29 17:39:15 +0700 | [diff] [blame] | 36 | #include "wt-status.h" |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 37 | #include "xdiff-interface.h" |
Matheus Tavares | d052cc0 | 2021-03-23 11:19:32 -0300 | [diff] [blame] | 38 | #include "entry.h" |
Matheus Tavares | 6053950 | 2021-05-04 13:27:29 -0300 | [diff] [blame] | 39 | #include "parallel-checkout.h" |
Ævar Arnfjörð Bjarmason | d21878f | 2023-02-06 23:58:57 +0100 | [diff] [blame] | 40 | #include "add-interactive.h" |
Ben Peart | fa655d8 | 2018-08-16 18:27:11 +0000 | [diff] [blame] | 41 | |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 42 | static const char * const checkout_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 43 | N_("git checkout [<options>] <branch>"), |
| 44 | N_("git checkout [<options>] [<branch>] -- <file>..."), |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 45 | NULL, |
| 46 | }; |
| 47 | |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 48 | static const char * const switch_branch_usage[] = { |
| 49 | N_("git switch [<options>] [<branch>]"), |
| 50 | NULL, |
| 51 | }; |
| 52 | |
Nguyễn Thái Ngọc Duy | 46e91b6 | 2019-04-25 16:45:45 +0700 | [diff] [blame] | 53 | static const char * const restore_usage[] = { |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 54 | N_("git restore [<options>] [--source=<branch>] <file>..."), |
Nguyễn Thái Ngọc Duy | 46e91b6 | 2019-04-25 16:45:45 +0700 | [diff] [blame] | 55 | NULL, |
| 56 | }; |
| 57 | |
Junio C Hamano | db94109 | 2008-08-30 07:46:55 -0700 | [diff] [blame] | 58 | struct checkout_opts { |
Nguyễn Thái Ngọc Duy | e51e305 | 2012-08-29 20:55:23 +0700 | [diff] [blame] | 59 | int patch_mode; |
Junio C Hamano | db94109 | 2008-08-30 07:46:55 -0700 | [diff] [blame] | 60 | int quiet; |
| 61 | int merge; |
| 62 | int force; |
Junio C Hamano | 3266967 | 2011-02-08 04:32:49 -0600 | [diff] [blame] | 63 | int force_detach; |
Nguyễn Thái Ngọc Duy | 7968bef | 2019-03-29 17:39:11 +0700 | [diff] [blame] | 64 | int implicit_detach; |
Junio C Hamano | 38901a4 | 2008-08-30 07:48:18 -0700 | [diff] [blame] | 65 | int writeout_stage; |
Nguyễn Thái Ngọc Duy | c1d7036 | 2011-11-27 17:15:33 +0700 | [diff] [blame] | 66 | int overwrite_ignore; |
Nguyễn Thái Ngọc Duy | 08d595d | 2013-04-13 09:12:08 +1000 | [diff] [blame] | 67 | int ignore_skipworktree; |
Nguyễn Thái Ngọc Duy | 1d0fa89 | 2015-01-03 16:41:26 +0700 | [diff] [blame] | 68 | int ignore_other_worktrees; |
Edmundo Carmona Antoranz | 870ebdb | 2015-11-01 15:19:05 -0600 | [diff] [blame] | 69 | int show_progress; |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 70 | int count_checkout_paths; |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 71 | int overlay_mode; |
Nguyễn Thái Ngọc Duy | ccb111b | 2019-03-29 17:39:13 +0700 | [diff] [blame] | 72 | int dwim_new_local_branch; |
Nguyễn Thái Ngọc Duy | 3ec37ad | 2019-03-29 17:39:07 +0700 | [diff] [blame] | 73 | int discard_changes; |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 74 | int accept_ref; |
Nguyễn Thái Ngọc Duy | 5c06e26 | 2019-03-29 17:39:09 +0700 | [diff] [blame] | 75 | int accept_pathspec; |
Nguyễn Thái Ngọc Duy | e342c63 | 2019-03-29 17:39:10 +0700 | [diff] [blame] | 76 | int switch_branch_doing_nothing_is_ok; |
Nguyễn Thái Ngọc Duy | 65f099b | 2019-03-29 17:39:14 +0700 | [diff] [blame] | 77 | int only_merge_on_switching_branches; |
Nguyễn Thái Ngọc Duy | c45f0f5 | 2019-03-29 17:39:15 +0700 | [diff] [blame] | 78 | int can_switch_when_in_progress; |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 79 | int orphan_from_empty_tree; |
Nguyễn Thái Ngọc Duy | be8ed50 | 2019-04-25 16:45:47 +0700 | [diff] [blame] | 80 | int empty_pathspec_ok; |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 81 | int checkout_index; |
| 82 | int checkout_worktree; |
Nguyễn Thái Ngọc Duy | a5e5f39 | 2019-04-25 16:45:53 +0700 | [diff] [blame] | 83 | const char *ignore_unmerged_opt; |
| 84 | int ignore_unmerged; |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 85 | int pathspec_file_nul; |
Jeff King | 7ce4088 | 2023-03-04 05:31:22 -0500 | [diff] [blame] | 86 | char *pathspec_from_file; |
Junio C Hamano | db94109 | 2008-08-30 07:46:55 -0700 | [diff] [blame] | 87 | |
| 88 | const char *new_branch; |
Tay Ray Chuan | 02ac983 | 2010-06-24 03:29:00 +0800 | [diff] [blame] | 89 | const char *new_branch_force; |
Erick Mattos | 9db5ebf | 2010-03-21 12:34:38 -0300 | [diff] [blame] | 90 | const char *new_orphan_branch; |
Junio C Hamano | db94109 | 2008-08-30 07:46:55 -0700 | [diff] [blame] | 91 | int new_branch_log; |
| 92 | enum branch_track track; |
Johannes Schindelin | 175f6e5 | 2010-08-25 12:34:57 +0200 | [diff] [blame] | 93 | struct diff_options diff_options; |
Nguyễn Thái Ngọc Duy | 55cf704 | 2019-03-29 17:39:03 +0700 | [diff] [blame] | 94 | char *conflict_style; |
Nguyễn Thái Ngọc Duy | e51e305 | 2012-08-29 20:55:23 +0700 | [diff] [blame] | 95 | |
| 96 | int branch_exists; |
| 97 | const char *prefix; |
Nguyễn Thái Ngọc Duy | 817b345 | 2013-07-14 15:35:41 +0700 | [diff] [blame] | 98 | struct pathspec pathspec; |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 99 | const char *from_treeish; |
Nguyễn Thái Ngọc Duy | e51e305 | 2012-08-29 20:55:23 +0700 | [diff] [blame] | 100 | struct tree *source_tree; |
Junio C Hamano | db94109 | 2008-08-30 07:46:55 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
brian m. carlson | a860476 | 2020-03-10 18:20:39 +0000 | [diff] [blame] | 103 | struct branch_info { |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 104 | char *name; /* The short name used */ |
| 105 | char *path; /* The full name of a real branch */ |
brian m. carlson | a860476 | 2020-03-10 18:20:39 +0000 | [diff] [blame] | 106 | struct commit *commit; /* The named commit */ |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 107 | char *refname; /* The full name of the ref being checked out. */ |
| 108 | struct object_id oid; /* The object ID of the commit being checked out. */ |
brian m. carlson | a860476 | 2020-03-10 18:20:39 +0000 | [diff] [blame] | 109 | /* |
| 110 | * if not null the branch is detached because it's already |
| 111 | * checked out in this checkout |
| 112 | */ |
| 113 | char *checkout; |
| 114 | }; |
| 115 | |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 116 | static void branch_info_release(struct branch_info *info) |
| 117 | { |
| 118 | free(info->name); |
| 119 | free(info->path); |
| 120 | free(info->refname); |
| 121 | free(info->checkout); |
| 122 | } |
| 123 | |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 124 | static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit, |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 125 | int changed) |
| 126 | { |
Emily Shaffer | 72ddf34 | 2021-12-22 04:59:35 +0100 | [diff] [blame] | 127 | return run_hooks_l("post-checkout", |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 128 | oid_to_hex(old_commit ? &old_commit->object.oid : null_oid()), |
| 129 | oid_to_hex(new_commit ? &new_commit->object.oid : null_oid()), |
Benoit Pierre | 15048f8 | 2014-03-18 11:00:53 +0100 | [diff] [blame] | 130 | changed ? "1" : "0", NULL); |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 131 | /* "new_commit" can be NULL when checking out from the index before |
Stephan Beyer | 2292ce4 | 2009-01-16 20:09:58 +0100 | [diff] [blame] | 132 | a commit exists. */ |
Stephan Beyer | ae98a00 | 2009-01-16 20:09:59 +0100 | [diff] [blame] | 133 | |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 134 | } |
| 135 | |
brian m. carlson | df46d77 | 2018-03-12 02:27:26 +0000 | [diff] [blame] | 136 | static int update_some(const struct object_id *oid, struct strbuf *base, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 137 | const char *pathname, unsigned mode, void *context UNUSED) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 138 | { |
| 139 | int len; |
| 140 | struct cache_entry *ce; |
Jeff King | c5326bd | 2014-11-13 13:30:34 -0500 | [diff] [blame] | 141 | int pos; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 142 | |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 143 | if (S_ISDIR(mode)) |
| 144 | return READ_TREE_RECURSIVE; |
| 145 | |
Nguyễn Thái Ngọc Duy | 6a0b0b6 | 2014-11-30 16:05:00 +0700 | [diff] [blame] | 146 | len = base->len + strlen(pathname); |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 147 | ce = make_empty_cache_entry(&the_index, len); |
brian m. carlson | df46d77 | 2018-03-12 02:27:26 +0000 | [diff] [blame] | 148 | oidcpy(&ce->oid, oid); |
Nguyễn Thái Ngọc Duy | 6a0b0b6 | 2014-11-30 16:05:00 +0700 | [diff] [blame] | 149 | memcpy(ce->name, base->buf, base->len); |
| 150 | memcpy(ce->name + base->len, pathname, len - base->len); |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 151 | ce->ce_flags = create_ce_flags(0) | CE_UPDATE; |
| 152 | ce->ce_namelen = len; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 153 | ce->ce_mode = create_ce_mode(mode); |
Jeff King | c5326bd | 2014-11-13 13:30:34 -0500 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * If the entry is the same as the current index, we can leave the old |
| 157 | * entry in place. Whether it is UPTODATE or not, checkout_entry will |
| 158 | * do the right thing. |
| 159 | */ |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 160 | pos = index_name_pos(&the_index, ce->name, ce->ce_namelen); |
Jeff King | c5326bd | 2014-11-13 13:30:34 -0500 | [diff] [blame] | 161 | if (pos >= 0) { |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 162 | struct cache_entry *old = the_index.cache[pos]; |
Jeff King | c5326bd | 2014-11-13 13:30:34 -0500 | [diff] [blame] | 163 | if (ce->ce_mode == old->ce_mode && |
Varun Naik | ecd7204 | 2019-08-02 09:28:52 -0700 | [diff] [blame] | 164 | !ce_intent_to_add(old) && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 165 | oideq(&ce->oid, &old->oid)) { |
Jeff King | c5326bd | 2014-11-13 13:30:34 -0500 | [diff] [blame] | 166 | old->ce_flags |= CE_UPDATE; |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 167 | discard_cache_entry(ce); |
Jeff King | c5326bd | 2014-11-13 13:30:34 -0500 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | } |
| 171 | |
Ævar Arnfjörð Bjarmason | 031b203 | 2022-11-19 14:07:33 +0100 | [diff] [blame] | 172 | add_index_entry(&the_index, ce, |
| 173 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
Nguyễn Thái Ngọc Duy | 18e4f40 | 2013-07-14 15:35:52 +0700 | [diff] [blame] | 177 | static int read_tree_some(struct tree *tree, const struct pathspec *pathspec) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 178 | { |
Ævar Arnfjörð Bjarmason | 4795748 | 2021-03-20 23:37:51 +0100 | [diff] [blame] | 179 | read_tree(the_repository, tree, |
| 180 | pathspec, update_some, NULL); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 181 | |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 182 | /* update the index with the given tree's info |
| 183 | * for all args, expanding wildcards, and exit |
| 184 | * with any non-zero return code. |
| 185 | */ |
| 186 | return 0; |
| 187 | } |
| 188 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 189 | static int skip_same_name(const struct cache_entry *ce, int pos) |
Junio C Hamano | 8fdcf31 | 2008-08-29 13:40:36 -0700 | [diff] [blame] | 190 | { |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 191 | while (++pos < the_index.cache_nr && |
| 192 | !strcmp(the_index.cache[pos]->name, ce->name)) |
Junio C Hamano | 8fdcf31 | 2008-08-29 13:40:36 -0700 | [diff] [blame] | 193 | ; /* skip */ |
| 194 | return pos; |
| 195 | } |
| 196 | |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 197 | static int check_stage(int stage, const struct cache_entry *ce, int pos, |
| 198 | int overlay_mode) |
Junio C Hamano | 38901a4 | 2008-08-30 07:48:18 -0700 | [diff] [blame] | 199 | { |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 200 | while (pos < the_index.cache_nr && |
| 201 | !strcmp(the_index.cache[pos]->name, ce->name)) { |
| 202 | if (ce_stage(the_index.cache[pos]) == stage) |
Junio C Hamano | 38901a4 | 2008-08-30 07:48:18 -0700 | [diff] [blame] | 203 | return 0; |
| 204 | pos++; |
| 205 | } |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 206 | if (!overlay_mode) |
| 207 | return 0; |
Ævar Arnfjörð Bjarmason | 9f97ab0 | 2011-02-22 23:41:40 +0000 | [diff] [blame] | 208 | if (stage == 2) |
| 209 | return error(_("path '%s' does not have our version"), ce->name); |
| 210 | else |
| 211 | return error(_("path '%s' does not have their version"), ce->name); |
Junio C Hamano | 38901a4 | 2008-08-30 07:48:18 -0700 | [diff] [blame] | 212 | } |
Junio C Hamano | 8fdcf31 | 2008-08-29 13:40:36 -0700 | [diff] [blame] | 213 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 214 | static int check_stages(unsigned stages, const struct cache_entry *ce, int pos) |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 215 | { |
Junio C Hamano | fbbccd0 | 2011-12-05 10:58:23 -0800 | [diff] [blame] | 216 | unsigned seen = 0; |
| 217 | const char *name = ce->name; |
| 218 | |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 219 | while (pos < the_index.cache_nr) { |
| 220 | ce = the_index.cache[pos]; |
Junio C Hamano | fbbccd0 | 2011-12-05 10:58:23 -0800 | [diff] [blame] | 221 | if (strcmp(name, ce->name)) |
| 222 | break; |
| 223 | seen |= (1 << ce_stage(ce)); |
| 224 | pos++; |
| 225 | } |
| 226 | if ((stages & seen) != stages) |
| 227 | return error(_("path '%s' does not have all necessary versions"), |
| 228 | name); |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
René Scharfe | ce25e4c | 2016-09-13 19:11:52 +0200 | [diff] [blame] | 232 | static int checkout_stage(int stage, const struct cache_entry *ce, int pos, |
Junio C Hamano | 7d0c1f4 | 2019-03-07 09:59:51 +0900 | [diff] [blame] | 233 | const struct checkout *state, int *nr_checkouts, |
| 234 | int overlay_mode) |
Junio C Hamano | 38901a4 | 2008-08-30 07:48:18 -0700 | [diff] [blame] | 235 | { |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 236 | while (pos < the_index.cache_nr && |
| 237 | !strcmp(the_index.cache[pos]->name, ce->name)) { |
| 238 | if (ce_stage(the_index.cache[pos]) == stage) |
| 239 | return checkout_entry(the_index.cache[pos], state, |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 240 | NULL, nr_checkouts); |
Junio C Hamano | 38901a4 | 2008-08-30 07:48:18 -0700 | [diff] [blame] | 241 | pos++; |
| 242 | } |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 243 | if (!overlay_mode) { |
Ævar Arnfjörð Bjarmason | 4002ec3 | 2022-12-20 13:39:56 +0100 | [diff] [blame] | 244 | unlink_entry(ce, NULL); |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 245 | return 0; |
| 246 | } |
Ævar Arnfjörð Bjarmason | 9f97ab0 | 2011-02-22 23:41:40 +0000 | [diff] [blame] | 247 | if (stage == 2) |
| 248 | return error(_("path '%s' does not have our version"), ce->name); |
| 249 | else |
| 250 | return error(_("path '%s' does not have their version"), ce->name); |
Junio C Hamano | 38901a4 | 2008-08-30 07:48:18 -0700 | [diff] [blame] | 251 | } |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 252 | |
Matheus Tavares | 6053950 | 2021-05-04 13:27:29 -0300 | [diff] [blame] | 253 | static int checkout_merged(int pos, const struct checkout *state, |
| 254 | int *nr_checkouts, struct mem_pool *ce_mem_pool) |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 255 | { |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 256 | struct cache_entry *ce = the_index.cache[pos]; |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 257 | const char *path = ce->name; |
| 258 | mmfile_t ancestor, ours, theirs; |
Elijah Newren | 35f6967 | 2022-02-02 02:37:30 +0000 | [diff] [blame] | 259 | enum ll_merge_result merge_status; |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 260 | int status; |
brian m. carlson | 60af769 | 2016-09-05 20:08:00 +0000 | [diff] [blame] | 261 | struct object_id oid; |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 262 | mmbuffer_t result_buf; |
brian m. carlson | 60af769 | 2016-09-05 20:08:00 +0000 | [diff] [blame] | 263 | struct object_id threeway[3]; |
Junio C Hamano | 335c6e4 | 2011-12-15 10:10:11 -0800 | [diff] [blame] | 264 | unsigned mode = 0; |
Elijah Newren | 00906d6 | 2020-08-03 18:41:20 +0000 | [diff] [blame] | 265 | struct ll_merge_options ll_opts; |
| 266 | int renormalize = 0; |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 267 | |
Junio C Hamano | fbbccd0 | 2011-12-05 10:58:23 -0800 | [diff] [blame] | 268 | memset(threeway, 0, sizeof(threeway)); |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 269 | while (pos < the_index.cache_nr) { |
Junio C Hamano | fbbccd0 | 2011-12-05 10:58:23 -0800 | [diff] [blame] | 270 | int stage; |
| 271 | stage = ce_stage(ce); |
| 272 | if (!stage || strcmp(path, ce->name)) |
| 273 | break; |
brian m. carlson | 60af769 | 2016-09-05 20:08:00 +0000 | [diff] [blame] | 274 | oidcpy(&threeway[stage - 1], &ce->oid); |
Junio C Hamano | fbbccd0 | 2011-12-05 10:58:23 -0800 | [diff] [blame] | 275 | if (stage == 2) |
| 276 | mode = create_ce_mode(ce->ce_mode); |
| 277 | pos++; |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 278 | ce = the_index.cache[pos]; |
Junio C Hamano | fbbccd0 | 2011-12-05 10:58:23 -0800 | [diff] [blame] | 279 | } |
brian m. carlson | 60af769 | 2016-09-05 20:08:00 +0000 | [diff] [blame] | 280 | if (is_null_oid(&threeway[1]) || is_null_oid(&threeway[2])) |
Junio C Hamano | fbbccd0 | 2011-12-05 10:58:23 -0800 | [diff] [blame] | 281 | return error(_("path '%s' does not have necessary versions"), path); |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 282 | |
brian m. carlson | d449347 | 2016-09-05 20:08:02 +0000 | [diff] [blame] | 283 | read_mmblob(&ancestor, &threeway[0]); |
| 284 | read_mmblob(&ours, &threeway[1]); |
| 285 | read_mmblob(&theirs, &threeway[2]); |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 286 | |
Elijah Newren | 00906d6 | 2020-08-03 18:41:20 +0000 | [diff] [blame] | 287 | memset(&ll_opts, 0, sizeof(ll_opts)); |
| 288 | git_config_get_bool("merge.renormalize", &renormalize); |
| 289 | ll_opts.renormalize = renormalize; |
Elijah Newren | 35f6967 | 2022-02-02 02:37:30 +0000 | [diff] [blame] | 290 | merge_status = ll_merge(&result_buf, path, &ancestor, "base", |
| 291 | &ours, "ours", &theirs, "theirs", |
| 292 | state->istate, &ll_opts); |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 293 | free(ancestor.ptr); |
| 294 | free(ours.ptr); |
| 295 | free(theirs.ptr); |
Elijah Newren | 35f6967 | 2022-02-02 02:37:30 +0000 | [diff] [blame] | 296 | if (merge_status == LL_MERGE_BINARY_CONFLICT) |
| 297 | warning("Cannot merge binary files: %s (%s vs. %s)", |
| 298 | path, "ours", "theirs"); |
| 299 | if (merge_status < 0 || !result_buf.ptr) { |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 300 | free(result_buf.ptr); |
Ævar Arnfjörð Bjarmason | e8a8a4d | 2011-02-22 23:41:39 +0000 | [diff] [blame] | 301 | return error(_("path '%s': cannot merge"), path); |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* |
| 305 | * NEEDSWORK: |
| 306 | * There is absolutely no reason to write this as a blob object |
Johannes Schindelin | 514e803 | 2017-05-04 15:56:40 +0200 | [diff] [blame] | 307 | * and create a phony cache entry. This hack is primarily to get |
| 308 | * to the write_entry() machinery that massages the contents to |
| 309 | * work-tree format and writes out which only allows it for a |
| 310 | * cache entry. The code in write_entry() needs to be refactored |
| 311 | * to allow us to feed a <buffer, size, mode> instead of a cache |
| 312 | * entry. Such a refactoring would help merge_recursive as well |
| 313 | * (it also writes the merge result to the object database even |
| 314 | * when it may contain conflicts). |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 315 | */ |
Ævar Arnfjörð Bjarmason | c80d226 | 2022-02-05 00:48:26 +0100 | [diff] [blame] | 316 | if (write_object_file(result_buf.ptr, result_buf.size, OBJ_BLOB, &oid)) |
Ævar Arnfjörð Bjarmason | e8a8a4d | 2011-02-22 23:41:39 +0000 | [diff] [blame] | 317 | die(_("Unable to add merge result for '%s'"), path); |
Junio C Hamano | 443a12f | 2017-05-08 13:21:06 +0900 | [diff] [blame] | 318 | free(result_buf.ptr); |
Matheus Tavares | 6053950 | 2021-05-04 13:27:29 -0300 | [diff] [blame] | 319 | ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool); |
Dmitry Potapov | 048f276 | 2008-10-05 06:14:40 +0400 | [diff] [blame] | 320 | if (!ce) |
Ævar Arnfjörð Bjarmason | e8a8a4d | 2011-02-22 23:41:39 +0000 | [diff] [blame] | 321 | die(_("make_cache_entry failed for path '%s'"), path); |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 322 | status = checkout_entry(ce, state, NULL, nr_checkouts); |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 323 | return status; |
| 324 | } |
| 325 | |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 326 | static void mark_ce_for_checkout_overlay(struct cache_entry *ce, |
| 327 | char *ps_matched, |
| 328 | const struct checkout_opts *opts) |
Thomas Gummerer | b7033e7 | 2018-12-20 13:48:18 +0000 | [diff] [blame] | 329 | { |
| 330 | ce->ce_flags &= ~CE_MATCHED; |
| 331 | if (!opts->ignore_skipworktree && ce_skip_worktree(ce)) |
| 332 | return; |
| 333 | if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) |
| 334 | /* |
| 335 | * "git checkout tree-ish -- path", but this entry |
| 336 | * is in the original index but is not in tree-ish |
| 337 | * or does not match the pathspec; it will not be |
| 338 | * checked out to the working tree. We will not do |
| 339 | * anything to this entry at all. |
| 340 | */ |
| 341 | return; |
| 342 | /* |
| 343 | * Either this entry came from the tree-ish we are |
| 344 | * checking the paths out of, or we are checking out |
| 345 | * of the index. |
| 346 | * |
| 347 | * If it comes from the tree-ish, we already know it |
| 348 | * matches the pathspec and could just stamp |
| 349 | * CE_MATCHED to it from update_some(). But we still |
Ævar Arnfjörð Bjarmason | 4795748 | 2021-03-20 23:37:51 +0100 | [diff] [blame] | 350 | * need ps_matched and read_tree (and |
Thomas Gummerer | b7033e7 | 2018-12-20 13:48:18 +0000 | [diff] [blame] | 351 | * eventually tree_entry_interesting) cannot fill |
| 352 | * ps_matched yet. Once it can, we can avoid calling |
| 353 | * match_pathspec() for _all_ entries when |
| 354 | * opts->source_tree != NULL. |
| 355 | */ |
| 356 | if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) |
| 357 | ce->ce_flags |= CE_MATCHED; |
| 358 | } |
| 359 | |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 360 | static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce, |
| 361 | char *ps_matched, |
| 362 | const struct checkout_opts *opts) |
| 363 | { |
| 364 | ce->ce_flags &= ~CE_MATCHED; |
| 365 | if (!opts->ignore_skipworktree && ce_skip_worktree(ce)) |
| 366 | return; |
| 367 | if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) { |
| 368 | ce->ce_flags |= CE_MATCHED; |
| 369 | if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) |
| 370 | /* |
| 371 | * In overlay mode, but the path is not in |
| 372 | * tree-ish, which means we should remove it |
| 373 | * from the index and the working tree. |
| 374 | */ |
| 375 | ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE; |
| 376 | } |
| 377 | } |
| 378 | |
brian m. carlson | a860476 | 2020-03-10 18:20:39 +0000 | [diff] [blame] | 379 | static int checkout_worktree(const struct checkout_opts *opts, |
| 380 | const struct branch_info *info) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 381 | { |
René Scharfe | 68e3d62 | 2016-09-22 18:11:33 +0200 | [diff] [blame] | 382 | struct checkout state = CHECKOUT_INIT; |
Nguyễn Thái Ngọc Duy | 1d1f689 | 2019-02-06 09:51:15 +0700 | [diff] [blame] | 383 | int nr_checkouts = 0, nr_unmerged = 0; |
Nguyễn Thái Ngọc Duy | 4058199 | 2019-04-25 16:45:49 +0700 | [diff] [blame] | 384 | int errs = 0; |
| 385 | int pos; |
Matheus Tavares | 6053950 | 2021-05-04 13:27:29 -0300 | [diff] [blame] | 386 | int pc_workers, pc_threshold; |
| 387 | struct mem_pool ce_mem_pool; |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 388 | |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 389 | state.force = 1; |
| 390 | state.refresh_cache = 1; |
Nguyễn Thái Ngọc Duy | d4a2024 | 2014-06-13 19:19:34 +0700 | [diff] [blame] | 391 | state.istate = &the_index; |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 392 | |
Matheus Tavares | 6053950 | 2021-05-04 13:27:29 -0300 | [diff] [blame] | 393 | mem_pool_init(&ce_mem_pool, 0); |
| 394 | get_parallel_checkout_configs(&pc_workers, &pc_threshold); |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 395 | init_checkout_metadata(&state.meta, info->refname, |
| 396 | info->commit ? &info->commit->object.oid : &info->oid, |
| 397 | NULL); |
| 398 | |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 399 | enable_delayed_checkout(&state); |
Derrick Stolee | 0f6d3ba | 2021-04-01 01:49:44 +0000 | [diff] [blame] | 400 | |
Matheus Tavares | 6053950 | 2021-05-04 13:27:29 -0300 | [diff] [blame] | 401 | if (pc_workers > 1) |
| 402 | init_parallel_checkout(); |
| 403 | |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 404 | for (pos = 0; pos < the_index.cache_nr; pos++) { |
| 405 | struct cache_entry *ce = the_index.cache[pos]; |
Nguyễn Thái Ngọc Duy | e721c15 | 2013-03-27 12:58:21 +0700 | [diff] [blame] | 406 | if (ce->ce_flags & CE_MATCHED) { |
Junio C Hamano | 8fdcf31 | 2008-08-29 13:40:36 -0700 | [diff] [blame] | 407 | if (!ce_stage(ce)) { |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 408 | errs |= checkout_entry(ce, &state, |
| 409 | NULL, &nr_checkouts); |
Junio C Hamano | 8fdcf31 | 2008-08-29 13:40:36 -0700 | [diff] [blame] | 410 | continue; |
| 411 | } |
Stefan Beller | f902207 | 2013-08-03 13:51:21 +0200 | [diff] [blame] | 412 | if (opts->writeout_stage) |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 413 | errs |= checkout_stage(opts->writeout_stage, |
| 414 | ce, pos, |
Junio C Hamano | 7d0c1f4 | 2019-03-07 09:59:51 +0900 | [diff] [blame] | 415 | &state, |
| 416 | &nr_checkouts, opts->overlay_mode); |
Stefan Beller | f902207 | 2013-08-03 13:51:21 +0200 | [diff] [blame] | 417 | else if (opts->merge) |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 418 | errs |= checkout_merged(pos, &state, |
Matheus Tavares | 6053950 | 2021-05-04 13:27:29 -0300 | [diff] [blame] | 419 | &nr_unmerged, |
| 420 | &ce_mem_pool); |
Junio C Hamano | 8fdcf31 | 2008-08-29 13:40:36 -0700 | [diff] [blame] | 421 | pos = skip_same_name(ce, pos) - 1; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 422 | } |
| 423 | } |
Matheus Tavares | 6053950 | 2021-05-04 13:27:29 -0300 | [diff] [blame] | 424 | if (pc_workers > 1) |
| 425 | errs |= run_parallel_checkout(&state, pc_workers, pc_threshold, |
| 426 | NULL, NULL); |
| 427 | mem_pool_discard(&ce_mem_pool, should_validate_cache_entries()); |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 428 | remove_marked_cache_entries(&the_index, 1); |
| 429 | remove_scheduled_dirs(); |
Matheus Tavares | 611c778 | 2022-07-14 08:49:12 -0300 | [diff] [blame] | 430 | errs |= finish_delayed_checkout(&state, opts->show_progress); |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 431 | |
| 432 | if (opts->count_checkout_paths) { |
Nguyễn Thái Ngọc Duy | 1d1f689 | 2019-02-06 09:51:15 +0700 | [diff] [blame] | 433 | if (nr_unmerged) |
| 434 | fprintf_ln(stderr, Q_("Recreated %d merge conflict", |
| 435 | "Recreated %d merge conflicts", |
| 436 | nr_unmerged), |
| 437 | nr_unmerged); |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 438 | if (opts->source_tree) |
Nguyễn Thái Ngọc Duy | 3c5883b | 2019-02-06 09:51:14 +0700 | [diff] [blame] | 439 | fprintf_ln(stderr, Q_("Updated %d path from %s", |
| 440 | "Updated %d paths from %s", |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 441 | nr_checkouts), |
| 442 | nr_checkouts, |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 443 | repo_find_unique_abbrev(the_repository, &opts->source_tree->object.oid, |
| 444 | DEFAULT_ABBREV)); |
Nguyễn Thái Ngọc Duy | 1d1f689 | 2019-02-06 09:51:15 +0700 | [diff] [blame] | 445 | else if (!nr_unmerged || nr_checkouts) |
Nguyễn Thái Ngọc Duy | 3c5883b | 2019-02-06 09:51:14 +0700 | [diff] [blame] | 446 | fprintf_ln(stderr, Q_("Updated %d path from the index", |
| 447 | "Updated %d paths from the index", |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 448 | nr_checkouts), |
| 449 | nr_checkouts); |
| 450 | } |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 451 | |
Nguyễn Thái Ngọc Duy | 4058199 | 2019-04-25 16:45:49 +0700 | [diff] [blame] | 452 | return errs; |
| 453 | } |
| 454 | |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 455 | static int checkout_paths(const struct checkout_opts *opts, |
brian m. carlson | a860476 | 2020-03-10 18:20:39 +0000 | [diff] [blame] | 456 | const struct branch_info *new_branch_info) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 457 | { |
| 458 | int pos; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 459 | static char *ps_matched; |
| 460 | struct object_id rev; |
| 461 | struct commit *head; |
| 462 | int errs = 0; |
| 463 | struct lock_file lock_file = LOCK_INIT; |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 464 | int checkout_index; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 465 | |
| 466 | trace2_cmd_mode(opts->patch_mode ? "patch" : "path"); |
| 467 | |
| 468 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) |
| 469 | die(_("'%s' cannot be used with updating paths"), "--track"); |
| 470 | |
| 471 | if (opts->new_branch_log) |
| 472 | die(_("'%s' cannot be used with updating paths"), "-l"); |
| 473 | |
Nguyễn Thái Ngọc Duy | a5e5f39 | 2019-04-25 16:45:53 +0700 | [diff] [blame] | 474 | if (opts->ignore_unmerged && opts->patch_mode) |
| 475 | die(_("'%s' cannot be used with updating paths"), |
| 476 | opts->ignore_unmerged_opt); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 477 | |
| 478 | if (opts->force_detach) |
| 479 | die(_("'%s' cannot be used with updating paths"), "--detach"); |
| 480 | |
| 481 | if (opts->merge && opts->patch_mode) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 482 | die(_("options '%s' and '%s' cannot be used together"), "--merge", "--patch"); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 483 | |
Nguyễn Thái Ngọc Duy | a5e5f39 | 2019-04-25 16:45:53 +0700 | [diff] [blame] | 484 | if (opts->ignore_unmerged && opts->merge) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 485 | die(_("options '%s' and '%s' cannot be used together"), |
Nguyễn Thái Ngọc Duy | a5e5f39 | 2019-04-25 16:45:53 +0700 | [diff] [blame] | 486 | opts->ignore_unmerged_opt, "-m"); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 487 | |
| 488 | if (opts->new_branch) |
| 489 | die(_("Cannot update paths and switch to branch '%s' at the same time."), |
| 490 | opts->new_branch); |
| 491 | |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 492 | if (!opts->checkout_worktree && !opts->checkout_index) |
| 493 | die(_("neither '%s' or '%s' is specified"), |
| 494 | "--staged", "--worktree"); |
| 495 | |
| 496 | if (!opts->checkout_worktree && !opts->from_treeish) |
| 497 | die(_("'%s' must be used when '%s' is not specified"), |
| 498 | "--worktree", "--source"); |
| 499 | |
Andy Koppe | ee8a888 | 2023-02-26 18:43:54 +0000 | [diff] [blame] | 500 | /* |
| 501 | * Reject --staged option to the restore command when combined with |
| 502 | * merge-related options. Use the accept_ref flag to distinguish it |
| 503 | * from the checkout command, which does not accept --staged anyway. |
| 504 | * |
| 505 | * `restore --ours|--theirs --worktree --staged` could mean resolving |
| 506 | * conflicted paths to one side in both the worktree and the index, |
| 507 | * but does not currently. |
| 508 | * |
| 509 | * `restore --merge|--conflict=<style>` already recreates conflicts |
| 510 | * in both the worktree and the index, so adding --staged would be |
| 511 | * meaningless. |
| 512 | */ |
| 513 | if (!opts->accept_ref && opts->checkout_index) { |
| 514 | if (opts->writeout_stage) |
| 515 | die(_("'%s' or '%s' cannot be used with %s"), |
| 516 | "--ours", "--theirs", "--staged"); |
Nguyễn Thái Ngọc Duy | e3ddd3b | 2019-04-25 16:45:51 +0700 | [diff] [blame] | 517 | |
Andy Koppe | ee8a888 | 2023-02-26 18:43:54 +0000 | [diff] [blame] | 518 | if (opts->merge) |
| 519 | die(_("'%s' or '%s' cannot be used with %s"), |
| 520 | "--merge", "--conflict", "--staged"); |
| 521 | } |
Nguyễn Thái Ngọc Duy | e3ddd3b | 2019-04-25 16:45:51 +0700 | [diff] [blame] | 522 | |
Junio C Hamano | 54f98fe | 2023-07-31 15:44:07 -0700 | [diff] [blame] | 523 | /* |
| 524 | * recreating unmerged index entries and writing out data from |
| 525 | * unmerged index entries would make no sense when checking out |
| 526 | * of a tree-ish. |
| 527 | */ |
| 528 | if ((opts->merge || opts->writeout_stage) && opts->source_tree) |
| 529 | die(_("'%s', '%s', or '%s' cannot be used when checking out of a tree"), |
| 530 | "--merge", "--ours", "--theirs"); |
| 531 | |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 532 | if (opts->patch_mode) { |
Ævar Arnfjörð Bjarmason | d21878f | 2023-02-06 23:58:57 +0100 | [diff] [blame] | 533 | enum add_p_mode patch_mode; |
Denton Liu | 5602b50 | 2020-10-07 00:56:15 -0700 | [diff] [blame] | 534 | const char *rev = new_branch_info->name; |
| 535 | char rev_oid[GIT_MAX_HEXSZ + 1]; |
| 536 | |
| 537 | /* |
| 538 | * Since rev can be in the form of `<a>...<b>` (which is not |
| 539 | * recognized by diff-index), we will always replace the name |
| 540 | * with the hex of the commit (whether it's in `...` form or |
| 541 | * not) for the run_add_interactive() machinery to work |
| 542 | * properly. However, there is special logic for the HEAD case |
Johannes Schindelin | 5c29f19 | 2020-12-19 14:55:59 +0000 | [diff] [blame] | 543 | * so we mustn't replace that. Also, when we were given a |
| 544 | * tree-object, new_branch_info->commit would be NULL, but we |
| 545 | * do not have to do any replacement, either. |
Denton Liu | 5602b50 | 2020-10-07 00:56:15 -0700 | [diff] [blame] | 546 | */ |
Johannes Schindelin | 5c29f19 | 2020-12-19 14:55:59 +0000 | [diff] [blame] | 547 | if (rev && new_branch_info->commit && strcmp(rev, "HEAD")) |
Denton Liu | 5602b50 | 2020-10-07 00:56:15 -0700 | [diff] [blame] | 548 | rev = oid_to_hex_r(rev_oid, &new_branch_info->commit->object.oid); |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 549 | |
| 550 | if (opts->checkout_index && opts->checkout_worktree) |
Ævar Arnfjörð Bjarmason | d21878f | 2023-02-06 23:58:57 +0100 | [diff] [blame] | 551 | patch_mode = ADD_P_CHECKOUT; |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 552 | else if (opts->checkout_index && !opts->checkout_worktree) |
Ævar Arnfjörð Bjarmason | d21878f | 2023-02-06 23:58:57 +0100 | [diff] [blame] | 553 | patch_mode = ADD_P_RESET; |
Nguyễn Thái Ngọc Duy | 2f0896e | 2019-04-25 16:45:54 +0700 | [diff] [blame] | 554 | else if (!opts->checkout_index && opts->checkout_worktree) |
Ævar Arnfjörð Bjarmason | d21878f | 2023-02-06 23:58:57 +0100 | [diff] [blame] | 555 | patch_mode = ADD_P_WORKTREE; |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 556 | else |
Nguyễn Thái Ngọc Duy | 2f0896e | 2019-04-25 16:45:54 +0700 | [diff] [blame] | 557 | BUG("either flag must have been set, worktree=%d, index=%d", |
| 558 | opts->checkout_worktree, opts->checkout_index); |
Ævar Arnfjörð Bjarmason | d21878f | 2023-02-06 23:58:57 +0100 | [diff] [blame] | 559 | return !!run_add_p(the_repository, patch_mode, rev, |
| 560 | &opts->pathspec); |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 561 | } |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 562 | |
| 563 | repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 564 | if (repo_read_index_preload(the_repository, &opts->pathspec, 0) < 0) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 565 | return error(_("index file corrupt")); |
| 566 | |
| 567 | if (opts->source_tree) |
| 568 | read_tree_some(opts->source_tree, &opts->pathspec); |
Junio C Hamano | 5bdedac | 2023-07-31 15:44:09 -0700 | [diff] [blame] | 569 | if (opts->merge) |
| 570 | unmerge_index(&the_index, &opts->pathspec, CE_MATCHED); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 571 | |
| 572 | ps_matched = xcalloc(opts->pathspec.nr, 1); |
| 573 | |
| 574 | /* |
| 575 | * Make sure all pathspecs participated in locating the paths |
| 576 | * to be checked out. |
| 577 | */ |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 578 | for (pos = 0; pos < the_index.cache_nr; pos++) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 579 | if (opts->overlay_mode) |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 580 | mark_ce_for_checkout_overlay(the_index.cache[pos], |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 581 | ps_matched, |
| 582 | opts); |
| 583 | else |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 584 | mark_ce_for_checkout_no_overlay(the_index.cache[pos], |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 585 | ps_matched, |
| 586 | opts); |
| 587 | |
Junio C Hamano | f496b06 | 2019-07-09 15:25:44 -0700 | [diff] [blame] | 588 | if (report_path_error(ps_matched, &opts->pathspec)) { |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 589 | free(ps_matched); |
| 590 | return 1; |
| 591 | } |
| 592 | free(ps_matched); |
Daniel Barkalow | 7533687 | 2008-02-28 16:52:44 -0500 | [diff] [blame] | 593 | |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 594 | /* Any unmerged paths? */ |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 595 | for (pos = 0; pos < the_index.cache_nr; pos++) { |
| 596 | const struct cache_entry *ce = the_index.cache[pos]; |
Junio C Hamano | d2b3691 | 2008-05-28 14:48:57 -0700 | [diff] [blame] | 597 | if (ce->ce_flags & CE_MATCHED) { |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 598 | if (!ce_stage(ce)) |
| 599 | continue; |
Nguyễn Thái Ngọc Duy | a5e5f39 | 2019-04-25 16:45:53 +0700 | [diff] [blame] | 600 | if (opts->ignore_unmerged) { |
| 601 | if (!opts->quiet) |
| 602 | warning(_("path '%s' is unmerged"), ce->name); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 603 | } else if (opts->writeout_stage) { |
| 604 | errs |= check_stage(opts->writeout_stage, ce, pos, opts->overlay_mode); |
| 605 | } else if (opts->merge) { |
| 606 | errs |= check_stages((1<<2) | (1<<3), ce, pos); |
| 607 | } else { |
| 608 | errs = 1; |
| 609 | error(_("path '%s' is unmerged"), ce->name); |
| 610 | } |
| 611 | pos = skip_same_name(ce, pos) - 1; |
| 612 | } |
| 613 | } |
| 614 | if (errs) |
| 615 | return 1; |
| 616 | |
| 617 | /* Now we are committed to check them out */ |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 618 | if (opts->checkout_worktree) |
brian m. carlson | a860476 | 2020-03-10 18:20:39 +0000 | [diff] [blame] | 619 | errs |= checkout_worktree(opts, new_branch_info); |
Jeff King | e701bab | 2020-01-08 06:43:44 -0500 | [diff] [blame] | 620 | else |
| 621 | remove_marked_cache_entries(&the_index, 1); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 622 | |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 623 | /* |
| 624 | * Allow updating the index when checking out from the index. |
| 625 | * This is to save new stat info. |
| 626 | */ |
| 627 | if (opts->checkout_worktree && !opts->checkout_index && !opts->source_tree) |
| 628 | checkout_index = 1; |
| 629 | else |
| 630 | checkout_index = opts->checkout_index; |
| 631 | |
| 632 | if (checkout_index) { |
| 633 | if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
| 634 | die(_("unable to write new index file")); |
| 635 | } else { |
| 636 | /* |
| 637 | * NEEDSWORK: if --worktree is not specified, we |
| 638 | * should save stat info of checked out files in the |
| 639 | * index to avoid the next (potentially costly) |
| 640 | * refresh. But it's a bit tricker to do... |
| 641 | */ |
| 642 | rollback_lock_file(&lock_file); |
| 643 | } |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 644 | |
brian m. carlson | 34c290a | 2017-10-15 22:06:56 +0000 | [diff] [blame] | 645 | read_ref_full("HEAD", 0, &rev, NULL); |
Stefan Beller | 21e1ee8 | 2018-06-28 18:21:57 -0700 | [diff] [blame] | 646 | head = lookup_commit_reference_gently(the_repository, &rev, 1); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 647 | |
| 648 | errs |= post_checkout_hook(head, head, 0); |
| 649 | return errs; |
| 650 | } |
| 651 | |
Nguyễn Thái Ngọc Duy | a2b4994 | 2012-08-29 20:55:22 +0700 | [diff] [blame] | 652 | static void show_local_changes(struct object *head, |
| 653 | const struct diff_options *opts) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 654 | { |
| 655 | struct rev_info rev; |
| 656 | /* I think we want full paths, even if we're in a subdirectory. */ |
Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 17:57:38 +0200 | [diff] [blame] | 657 | repo_init_revisions(the_repository, &rev, NULL); |
Johannes Schindelin | 175f6e5 | 2010-08-25 12:34:57 +0200 | [diff] [blame] | 658 | rev.diffopt.flags = opts->flags; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 659 | rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS; |
Victoria Dye | 49ff3cb | 2022-08-08 19:07:49 +0000 | [diff] [blame] | 660 | rev.diffopt.flags.recursive = 1; |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 661 | diff_setup_done(&rev.diffopt); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 662 | add_pending_object(&rev, head, NULL); |
| 663 | run_diff_index(&rev, 0); |
Ævar Arnfjörð Bjarmason | 1878b5e | 2022-04-13 22:01:35 +0200 | [diff] [blame] | 664 | release_revisions(&rev); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 665 | } |
| 666 | |
Jonathan Nieder | b3c0494 | 2011-02-22 22:43:22 +0000 | [diff] [blame] | 667 | static void describe_detached_head(const char *msg, struct commit *commit) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 668 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 669 | struct strbuf sb = STRBUF_INIT; |
Ann T Ropea | ca69d4d | 2017-12-06 01:20:42 +0100 | [diff] [blame] | 670 | |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 671 | if (!repo_parse_commit(the_repository, commit)) |
Jeff King | 3c62183 | 2013-10-24 04:54:53 -0400 | [diff] [blame] | 672 | pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb); |
Ann T Ropea | ca69d4d | 2017-12-06 01:20:42 +0100 | [diff] [blame] | 673 | if (print_sha1_ellipsis()) { |
| 674 | fprintf(stderr, "%s %s... %s\n", msg, |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 675 | repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV), |
| 676 | sb.buf); |
Ann T Ropea | ca69d4d | 2017-12-06 01:20:42 +0100 | [diff] [blame] | 677 | } else { |
| 678 | fprintf(stderr, "%s %s %s\n", msg, |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 679 | repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV), |
| 680 | sb.buf); |
Ann T Ropea | ca69d4d | 2017-12-06 01:20:42 +0100 | [diff] [blame] | 681 | } |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 682 | strbuf_release(&sb); |
| 683 | } |
| 684 | |
Nguyễn Thái Ngọc Duy | a2b4994 | 2012-08-29 20:55:22 +0700 | [diff] [blame] | 685 | static int reset_tree(struct tree *tree, const struct checkout_opts *o, |
brian m. carlson | 13e7ed6 | 2020-03-16 18:05:04 +0000 | [diff] [blame] | 686 | int worktree, int *writeout_error, |
| 687 | struct branch_info *info) |
Junio C Hamano | 6286a08 | 2008-05-28 14:59:40 -0700 | [diff] [blame] | 688 | { |
| 689 | struct unpack_trees_options opts; |
| 690 | struct tree_desc tree_desc; |
| 691 | |
| 692 | memset(&opts, 0, sizeof(opts)); |
| 693 | opts.head_idx = -1; |
| 694 | opts.update = worktree; |
| 695 | opts.skip_unmerged = !worktree; |
Elijah Newren | 480d3d6 | 2021-09-27 16:33:44 +0000 | [diff] [blame] | 696 | opts.reset = o->force ? UNPACK_RESET_OVERWRITE_UNTRACKED : |
| 697 | UNPACK_RESET_PROTECT_UNTRACKED; |
| 698 | opts.preserve_ignored = (!o->force && !o->overwrite_ignore); |
Junio C Hamano | 6286a08 | 2008-05-28 14:59:40 -0700 | [diff] [blame] | 699 | opts.merge = 1; |
| 700 | opts.fn = oneway_merge; |
Edmundo Carmona Antoranz | 870ebdb | 2015-11-01 15:19:05 -0600 | [diff] [blame] | 701 | opts.verbose_update = o->show_progress; |
Junio C Hamano | 6286a08 | 2008-05-28 14:59:40 -0700 | [diff] [blame] | 702 | opts.src_index = &the_index; |
| 703 | opts.dst_index = &the_index; |
brian m. carlson | 13e7ed6 | 2020-03-16 18:05:04 +0000 | [diff] [blame] | 704 | init_checkout_metadata(&opts.meta, info->refname, |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 705 | info->commit ? &info->commit->object.oid : null_oid(), |
brian m. carlson | 13e7ed6 | 2020-03-16 18:05:04 +0000 | [diff] [blame] | 706 | NULL); |
Junio C Hamano | 6286a08 | 2008-05-28 14:59:40 -0700 | [diff] [blame] | 707 | parse_tree(tree); |
| 708 | init_tree_desc(&tree_desc, tree->buffer, tree->size); |
Junio C Hamano | 291d823 | 2008-05-28 15:26:59 -0700 | [diff] [blame] | 709 | switch (unpack_trees(1, &tree_desc, &opts)) { |
| 710 | case -2: |
Nguyễn Thái Ngọc Duy | a2b4994 | 2012-08-29 20:55:22 +0700 | [diff] [blame] | 711 | *writeout_error = 1; |
Junio C Hamano | 291d823 | 2008-05-28 15:26:59 -0700 | [diff] [blame] | 712 | /* |
| 713 | * We return 0 nevertheless, as the index is all right |
| 714 | * and more importantly we have made best efforts to |
| 715 | * update paths in the work tree, and we cannot revert |
| 716 | * them. |
| 717 | */ |
Jeff King | 1cf01a3 | 2017-09-21 02:25:41 -0400 | [diff] [blame] | 718 | /* fallthrough */ |
Junio C Hamano | 291d823 | 2008-05-28 15:26:59 -0700 | [diff] [blame] | 719 | case 0: |
| 720 | return 0; |
| 721 | default: |
Junio C Hamano | 6286a08 | 2008-05-28 14:59:40 -0700 | [diff] [blame] | 722 | return 128; |
Junio C Hamano | 291d823 | 2008-05-28 15:26:59 -0700 | [diff] [blame] | 723 | } |
Junio C Hamano | 6286a08 | 2008-05-28 14:59:40 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 726 | static void setup_branch_path(struct branch_info *branch) |
| 727 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 728 | struct strbuf buf = STRBUF_INIT; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 729 | |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 730 | /* |
| 731 | * If this is a ref, resolve it; otherwise, look up the OID for our |
| 732 | * expression. Failure here is okay. |
| 733 | */ |
Ævar Arnfjörð Bjarmason | 12cb1c1 | 2023-03-28 15:58:54 +0200 | [diff] [blame] | 734 | if (!repo_dwim_ref(the_repository, branch->name, strlen(branch->name), |
| 735 | &branch->oid, &branch->refname, 0)) |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 736 | repo_get_oid_committish(the_repository, branch->name, &branch->oid); |
| 737 | |
Jeff King | fd4692f | 2017-03-02 03:23:18 -0500 | [diff] [blame] | 738 | strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL); |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 739 | if (strcmp(buf.buf, branch->name)) { |
| 740 | free(branch->name); |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 741 | branch->name = xstrdup(buf.buf); |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 742 | } |
Junio C Hamano | a552de7 | 2009-03-21 13:17:30 -0700 | [diff] [blame] | 743 | strbuf_splice(&buf, 0, 0, "refs/heads/", 11); |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 744 | free(branch->path); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 745 | branch->path = strbuf_detach(&buf, NULL); |
| 746 | } |
| 747 | |
Ævar Arnfjörð Bjarmason | 33d0dda | 2022-07-01 12:42:57 +0200 | [diff] [blame] | 748 | static void init_topts(struct unpack_trees_options *topts, int merge, |
| 749 | int show_progress, int overwrite_ignore, |
| 750 | struct commit *old_commit) |
| 751 | { |
| 752 | memset(topts, 0, sizeof(*topts)); |
| 753 | topts->head_idx = -1; |
| 754 | topts->src_index = &the_index; |
| 755 | topts->dst_index = &the_index; |
| 756 | |
| 757 | setup_unpack_trees_porcelain(topts, "checkout"); |
| 758 | |
Ævar Arnfjörð Bjarmason | fbc1ed6 | 2022-11-19 14:07:30 +0100 | [diff] [blame] | 759 | topts->initial_checkout = is_index_unborn(&the_index); |
Ævar Arnfjörð Bjarmason | 33d0dda | 2022-07-01 12:42:57 +0200 | [diff] [blame] | 760 | topts->update = 1; |
| 761 | topts->merge = 1; |
| 762 | topts->quiet = merge && old_commit; |
| 763 | topts->verbose_update = show_progress; |
| 764 | topts->fn = twoway_merge; |
| 765 | topts->preserve_ignored = !overwrite_ignore; |
| 766 | } |
| 767 | |
Nguyễn Thái Ngọc Duy | a2b4994 | 2012-08-29 20:55:22 +0700 | [diff] [blame] | 768 | static int merge_working_tree(const struct checkout_opts *opts, |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 769 | struct branch_info *old_branch_info, |
| 770 | struct branch_info *new_branch_info, |
Nguyễn Thái Ngọc Duy | a2b4994 | 2012-08-29 20:55:22 +0700 | [diff] [blame] | 771 | int *writeout_error) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 772 | { |
| 773 | int ret; |
Martin Ågren | 837e34e | 2017-10-05 22:32:04 +0200 | [diff] [blame] | 774 | struct lock_file lock_file = LOCK_INIT; |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 775 | struct tree *new_tree; |
Junio C Hamano | b96524f | 2008-12-05 17:54:10 -0800 | [diff] [blame] | 776 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 777 | repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); |
| 778 | if (repo_read_index_preload(the_repository, NULL, 0) < 0) |
Vasco Almeida | e923a8a | 2016-06-17 20:21:20 +0000 | [diff] [blame] | 779 | return error(_("index file corrupt")); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 780 | |
Ævar Arnfjörð Bjarmason | 031b203 | 2022-11-19 14:07:33 +0100 | [diff] [blame] | 781 | resolve_undo_clear_index(&the_index); |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 782 | if (opts->new_orphan_branch && opts->orphan_from_empty_tree) { |
| 783 | if (new_branch_info->commit) |
| 784 | BUG("'switch --orphan' should never accept a commit as starting point"); |
| 785 | new_tree = parse_tree_indirect(the_hash_algo->empty_tree); |
| 786 | } else |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 787 | new_tree = repo_get_commit_tree(the_repository, |
| 788 | new_branch_info->commit); |
Nguyễn Thái Ngọc Duy | 3ec37ad | 2019-03-29 17:39:07 +0700 | [diff] [blame] | 789 | if (opts->discard_changes) { |
brian m. carlson | 13e7ed6 | 2020-03-16 18:05:04 +0000 | [diff] [blame] | 790 | ret = reset_tree(new_tree, opts, 1, writeout_error, new_branch_info); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 791 | if (ret) |
| 792 | return ret; |
| 793 | } else { |
| 794 | struct tree_desc trees[2]; |
| 795 | struct tree *tree; |
| 796 | struct unpack_trees_options topts; |
Glen Choo | 8d2eaf6 | 2022-03-01 16:36:13 -0800 | [diff] [blame] | 797 | const struct object_id *old_commit_oid; |
Linus Torvalds | bc052d7 | 2008-03-06 12:26:14 -0800 | [diff] [blame] | 798 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 799 | refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 800 | |
Ævar Arnfjörð Bjarmason | fbc1ed6 | 2022-11-19 14:07:30 +0100 | [diff] [blame] | 801 | if (unmerged_index(&the_index)) { |
Ævar Arnfjörð Bjarmason | e8a8a4d | 2011-02-22 23:41:39 +0000 | [diff] [blame] | 802 | error(_("you need to resolve your current index first")); |
Junio C Hamano | 04c9e11 | 2008-02-23 15:45:19 -0800 | [diff] [blame] | 803 | return 1; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 804 | } |
Junio C Hamano | 04c9e11 | 2008-02-23 15:45:19 -0800 | [diff] [blame] | 805 | |
| 806 | /* 2-way merge to the new branch */ |
Ævar Arnfjörð Bjarmason | 33d0dda | 2022-07-01 12:42:57 +0200 | [diff] [blame] | 807 | init_topts(&topts, opts->merge, opts->show_progress, |
| 808 | opts->overwrite_ignore, old_branch_info->commit); |
brian m. carlson | 13e7ed6 | 2020-03-16 18:05:04 +0000 | [diff] [blame] | 809 | init_checkout_metadata(&topts.meta, new_branch_info->refname, |
| 810 | new_branch_info->commit ? |
| 811 | &new_branch_info->commit->object.oid : |
| 812 | &new_branch_info->oid, NULL); |
Glen Choo | 8d2eaf6 | 2022-03-01 16:36:13 -0800 | [diff] [blame] | 813 | |
| 814 | old_commit_oid = old_branch_info->commit ? |
| 815 | &old_branch_info->commit->object.oid : |
| 816 | the_hash_algo->empty_tree; |
| 817 | tree = parse_tree_indirect(old_commit_oid); |
| 818 | if (!tree) |
| 819 | die(_("unable to parse commit %s"), |
| 820 | oid_to_hex(old_commit_oid)); |
| 821 | |
Junio C Hamano | 04c9e11 | 2008-02-23 15:45:19 -0800 | [diff] [blame] | 822 | init_tree_desc(&trees[0], tree->buffer, tree->size); |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 823 | parse_tree(new_tree); |
| 824 | tree = new_tree; |
Junio C Hamano | 04c9e11 | 2008-02-23 15:45:19 -0800 | [diff] [blame] | 825 | init_tree_desc(&trees[1], tree->buffer, tree->size); |
| 826 | |
Junio C Hamano | 291d823 | 2008-05-28 15:26:59 -0700 | [diff] [blame] | 827 | ret = unpack_trees(2, trees, &topts); |
Martin Ågren | 1c41d28 | 2018-05-21 16:54:28 +0200 | [diff] [blame] | 828 | clear_unpack_trees_porcelain(&topts); |
Junio C Hamano | 49d833d | 2009-05-12 09:41:28 -0700 | [diff] [blame] | 829 | if (ret == -1) { |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 830 | /* |
| 831 | * Unpack couldn't do a trivial merge; either |
| 832 | * give up or do a real merge, depending on |
| 833 | * whether the merge flag was used. |
| 834 | */ |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 835 | struct tree *work; |
Nguyễn Thái Ngọc Duy | 6eff409 | 2019-03-22 16:31:38 +0700 | [diff] [blame] | 836 | struct tree *old_tree; |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 837 | struct merge_options o; |
Nguyễn Thái Ngọc Duy | a7256de | 2019-03-19 16:39:10 +0700 | [diff] [blame] | 838 | struct strbuf sb = STRBUF_INIT; |
Elijah Newren | 65c01c6 | 2019-08-15 14:40:31 -0700 | [diff] [blame] | 839 | struct strbuf old_commit_shortname = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | a7256de | 2019-03-19 16:39:10 +0700 | [diff] [blame] | 840 | |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 841 | if (!opts->merge) |
| 842 | return 1; |
Junio C Hamano | 64da3ae | 2010-01-06 23:51:47 -0800 | [diff] [blame] | 843 | |
| 844 | /* |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 845 | * Without old_branch_info->commit, the below is the same as |
Junio C Hamano | 64da3ae | 2010-01-06 23:51:47 -0800 | [diff] [blame] | 846 | * the two-tree unpack we already tried and failed. |
| 847 | */ |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 848 | if (!old_branch_info->commit) |
Junio C Hamano | 64da3ae | 2010-01-06 23:51:47 -0800 | [diff] [blame] | 849 | return 1; |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 850 | old_tree = repo_get_commit_tree(the_repository, |
| 851 | old_branch_info->commit); |
Nguyễn Thái Ngọc Duy | 6eff409 | 2019-03-22 16:31:38 +0700 | [diff] [blame] | 852 | |
| 853 | if (repo_index_has_changes(the_repository, old_tree, &sb)) |
| 854 | die(_("cannot continue with staged changes in " |
| 855 | "the following files:\n%s"), sb.buf); |
| 856 | strbuf_release(&sb); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 857 | |
| 858 | /* Do more real merge */ |
| 859 | |
| 860 | /* |
| 861 | * We update the index fully, then write the |
| 862 | * tree from the index, then merge the new |
| 863 | * branch with the current tree, with the old |
| 864 | * branch as the base. Then we reset the index |
| 865 | * (but not the working tree) to the new |
| 866 | * branch, leaving the working tree as the |
| 867 | * merged version, but skipping unmerged |
| 868 | * entries in the index. |
| 869 | */ |
| 870 | |
Elijah Newren | 50c37ee | 2023-05-16 06:33:46 +0000 | [diff] [blame] | 871 | add_files_to_cache(the_repository, NULL, NULL, 0, 0); |
Nguyễn Thái Ngọc Duy | 0d6caa2 | 2019-01-12 09:13:29 +0700 | [diff] [blame] | 872 | init_merge_options(&o, the_repository); |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 873 | o.verbosity = 0; |
Elijah Newren | 724dd76 | 2019-08-17 11:41:32 -0700 | [diff] [blame] | 874 | work = write_in_core_index_as_tree(the_repository); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 875 | |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 876 | ret = reset_tree(new_tree, |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 877 | opts, 1, |
brian m. carlson | 13e7ed6 | 2020-03-16 18:05:04 +0000 | [diff] [blame] | 878 | writeout_error, new_branch_info); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 879 | if (ret) |
| 880 | return ret; |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 881 | o.ancestor = old_branch_info->name; |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 882 | if (!old_branch_info->name) { |
Elijah Newren | 65c01c6 | 2019-08-15 14:40:31 -0700 | [diff] [blame] | 883 | strbuf_add_unique_abbrev(&old_commit_shortname, |
| 884 | &old_branch_info->commit->object.oid, |
| 885 | DEFAULT_ABBREV); |
| 886 | o.ancestor = old_commit_shortname.buf; |
| 887 | } |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 888 | o.branch1 = new_branch_info->name; |
Miklos Vajna | 8a2fce1 | 2008-08-25 16:25:57 +0200 | [diff] [blame] | 889 | o.branch2 = "local"; |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 890 | ret = merge_trees(&o, |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 891 | new_tree, |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 892 | work, |
Elijah Newren | b4db8a2 | 2019-08-17 11:41:30 -0700 | [diff] [blame] | 893 | old_tree); |
Johannes Schindelin | f241ff0 | 2016-07-26 18:06:02 +0200 | [diff] [blame] | 894 | if (ret < 0) |
| 895 | exit(128); |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 896 | ret = reset_tree(new_tree, |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 897 | opts, 0, |
brian m. carlson | 13e7ed6 | 2020-03-16 18:05:04 +0000 | [diff] [blame] | 898 | writeout_error, new_branch_info); |
Johannes Schindelin | 548009c | 2016-08-01 13:44:53 +0200 | [diff] [blame] | 899 | strbuf_release(&o.obuf); |
Elijah Newren | 65c01c6 | 2019-08-15 14:40:31 -0700 | [diff] [blame] | 900 | strbuf_release(&old_commit_shortname); |
Junio C Hamano | 84a5750 | 2008-05-28 14:54:02 -0700 | [diff] [blame] | 901 | if (ret) |
| 902 | return ret; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 906 | if (!cache_tree_fully_valid(the_index.cache_tree)) |
Junio C Hamano | 3fd13cb | 2014-09-11 10:33:32 -0700 | [diff] [blame] | 907 | cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR); |
David Turner | aecf567 | 2014-07-05 21:06:56 -0700 | [diff] [blame] | 908 | |
Martin Ågren | 837e34e | 2017-10-05 22:32:04 +0200 | [diff] [blame] | 909 | if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
Ævar Arnfjörð Bjarmason | e8a8a4d | 2011-02-22 23:41:39 +0000 | [diff] [blame] | 910 | die(_("unable to write new index file")); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 911 | |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 912 | if (!opts->discard_changes && !opts->quiet && new_branch_info->commit) |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 913 | show_local_changes(&new_branch_info->commit->object, &opts->diff_options); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 918 | static void report_tracking(struct branch_info *new_branch_info) |
Junio C Hamano | 79a1e6b | 2008-02-16 17:17:09 -0800 | [diff] [blame] | 919 | { |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 920 | struct strbuf sb = STRBUF_INIT; |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 921 | struct branch *branch = branch_get(new_branch_info->name); |
Junio C Hamano | 79a1e6b | 2008-02-16 17:17:09 -0800 | [diff] [blame] | 922 | |
Alex Henrie | b6f3da5 | 2023-07-12 22:41:13 -0600 | [diff] [blame] | 923 | if (!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL, 1)) |
Junio C Hamano | 79a1e6b | 2008-02-16 17:17:09 -0800 | [diff] [blame] | 924 | return; |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 925 | fputs(sb.buf, stdout); |
| 926 | strbuf_release(&sb); |
Junio C Hamano | b0030db | 2008-02-20 15:05:23 -0800 | [diff] [blame] | 927 | } |
Junio C Hamano | 79a1e6b | 2008-02-16 17:17:09 -0800 | [diff] [blame] | 928 | |
Nguyễn Thái Ngọc Duy | a2b4994 | 2012-08-29 20:55:22 +0700 | [diff] [blame] | 929 | static void update_refs_for_switch(const struct checkout_opts *opts, |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 930 | struct branch_info *old_branch_info, |
| 931 | struct branch_info *new_branch_info) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 932 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 933 | struct strbuf msg = STRBUF_INIT; |
Ramkumar Ramachandra | 3bed291 | 2013-06-16 14:15:16 +0530 | [diff] [blame] | 934 | const char *old_desc, *reflog_msg; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 935 | if (opts->new_branch) { |
Erick Mattos | 3631bf7 | 2010-05-21 21:28:37 -0300 | [diff] [blame] | 936 | if (opts->new_orphan_branch) { |
Cornelius Weig | 341fb28 | 2017-01-27 11:09:47 +0100 | [diff] [blame] | 937 | char *refname; |
| 938 | |
| 939 | refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch); |
| 940 | if (opts->new_branch_log && |
| 941 | !should_autocreate_reflog(refname)) { |
Nguyễn Thái Ngọc Duy | 1a83c24 | 2014-11-30 15:24:28 +0700 | [diff] [blame] | 942 | int ret; |
David Turner | a4c653d | 2015-07-21 17:04:50 -0400 | [diff] [blame] | 943 | struct strbuf err = STRBUF_INIT; |
Erick Mattos | 3631bf7 | 2010-05-21 21:28:37 -0300 | [diff] [blame] | 944 | |
Han-Wen Nienhuys | 7b08912 | 2021-11-22 14:19:08 +0000 | [diff] [blame] | 945 | ret = safe_create_reflog(refname, &err); |
Nguyễn Thái Ngọc Duy | 1a83c24 | 2014-11-30 15:24:28 +0700 | [diff] [blame] | 946 | if (ret) { |
David Turner | a4c653d | 2015-07-21 17:04:50 -0400 | [diff] [blame] | 947 | fprintf(stderr, _("Can not do reflog for '%s': %s\n"), |
| 948 | opts->new_orphan_branch, err.buf); |
| 949 | strbuf_release(&err); |
Cornelius Weig | 341fb28 | 2017-01-27 11:09:47 +0100 | [diff] [blame] | 950 | free(refname); |
Erick Mattos | 3631bf7 | 2010-05-21 21:28:37 -0300 | [diff] [blame] | 951 | return; |
| 952 | } |
David Turner | abd0cd3 | 2015-07-21 17:04:52 -0400 | [diff] [blame] | 953 | strbuf_release(&err); |
Erick Mattos | 3631bf7 | 2010-05-21 21:28:37 -0300 | [diff] [blame] | 954 | } |
Cornelius Weig | 341fb28 | 2017-01-27 11:09:47 +0100 | [diff] [blame] | 955 | free(refname); |
Erick Mattos | 3631bf7 | 2010-05-21 21:28:37 -0300 | [diff] [blame] | 956 | } |
| 957 | else |
Nguyễn Thái Ngọc Duy | 4edce17 | 2018-11-10 06:49:00 +0100 | [diff] [blame] | 958 | create_branch(the_repository, |
| 959 | opts->new_branch, new_branch_info->name, |
Tay Ray Chuan | 02ac983 | 2010-06-24 03:29:00 +0800 | [diff] [blame] | 960 | opts->new_branch_force ? 1 : 0, |
Jonathan Nieder | 39bd6f7 | 2011-11-26 02:54:55 -0600 | [diff] [blame] | 961 | opts->new_branch_force ? 1 : 0, |
Kaartic Sivaraam | e2bbd0c | 2017-11-18 22:56:46 +0530 | [diff] [blame] | 962 | opts->new_branch_log, |
Jeff King | f9a482e | 2012-03-26 19:51:01 -0400 | [diff] [blame] | 963 | opts->quiet, |
Glen Choo | 3f3e760 | 2022-01-28 16:04:43 -0800 | [diff] [blame] | 964 | opts->track, |
| 965 | 0); |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 966 | free(new_branch_info->name); |
| 967 | free(new_branch_info->refname); |
| 968 | new_branch_info->name = xstrdup(opts->new_branch); |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 969 | setup_branch_path(new_branch_info); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 970 | } |
| 971 | |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 972 | old_desc = old_branch_info->name; |
| 973 | if (!old_desc && old_branch_info->commit) |
| 974 | old_desc = oid_to_hex(&old_branch_info->commit->object.oid); |
Ramkumar Ramachandra | 3bed291 | 2013-06-16 14:15:16 +0530 | [diff] [blame] | 975 | |
| 976 | reflog_msg = getenv("GIT_REFLOG_ACTION"); |
| 977 | if (!reflog_msg) |
| 978 | strbuf_addf(&msg, "checkout: moving from %s to %s", |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 979 | old_desc ? old_desc : "(invalid)", new_branch_info->name); |
Ramkumar Ramachandra | 3bed291 | 2013-06-16 14:15:16 +0530 | [diff] [blame] | 980 | else |
René Scharfe | a91cc7f | 2020-02-09 14:44:23 +0100 | [diff] [blame] | 981 | strbuf_insertstr(&msg, 0, reflog_msg); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 982 | |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 983 | if (!strcmp(new_branch_info->name, "HEAD") && !new_branch_info->path && !opts->force_detach) { |
Jonathan Nieder | f8bd36a | 2011-02-08 04:34:34 -0600 | [diff] [blame] | 984 | /* Nothing to do. */ |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 985 | } else if (opts->force_detach || !new_branch_info->path) { /* No longer on any branch. */ |
| 986 | update_ref(msg.buf, "HEAD", &new_branch_info->commit->object.oid, NULL, |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 987 | REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR); |
Jonathan Nieder | f8bd36a | 2011-02-08 04:34:34 -0600 | [diff] [blame] | 988 | if (!opts->quiet) { |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 989 | if (old_branch_info->path && |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 990 | advice_enabled(ADVICE_DETACHED_HEAD) && !opts->force_detach) |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 991 | detach_advice(new_branch_info->name); |
| 992 | describe_detached_head(_("HEAD is now at"), new_branch_info->commit); |
Jonathan Nieder | f8bd36a | 2011-02-08 04:34:34 -0600 | [diff] [blame] | 993 | } |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 994 | } else if (new_branch_info->path) { /* Switch branches. */ |
| 995 | if (create_symref("HEAD", new_branch_info->path, msg.buf) < 0) |
Nguyễn Thái Ngọc Duy | 4636f65 | 2016-02-27 13:41:54 +0700 | [diff] [blame] | 996 | die(_("unable to update HEAD")); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 997 | if (!opts->quiet) { |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 998 | if (old_branch_info->path && !strcmp(new_branch_info->path, old_branch_info->path)) { |
Jonathan Nieder | 39bd6f7 | 2011-11-26 02:54:55 -0600 | [diff] [blame] | 999 | if (opts->new_branch_force) |
| 1000 | fprintf(stderr, _("Reset branch '%s'\n"), |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1001 | new_branch_info->name); |
Jonathan Nieder | 39bd6f7 | 2011-11-26 02:54:55 -0600 | [diff] [blame] | 1002 | else |
| 1003 | fprintf(stderr, _("Already on '%s'\n"), |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1004 | new_branch_info->name); |
Ævar Arnfjörð Bjarmason | 08eaa4b | 2011-02-22 23:41:43 +0000 | [diff] [blame] | 1005 | } else if (opts->new_branch) { |
| 1006 | if (opts->branch_exists) |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1007 | fprintf(stderr, _("Switched to and reset branch '%s'\n"), new_branch_info->name); |
Ævar Arnfjörð Bjarmason | 08eaa4b | 2011-02-22 23:41:43 +0000 | [diff] [blame] | 1008 | else |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1009 | fprintf(stderr, _("Switched to a new branch '%s'\n"), new_branch_info->name); |
Ævar Arnfjörð Bjarmason | 08eaa4b | 2011-02-22 23:41:43 +0000 | [diff] [blame] | 1010 | } else { |
Ævar Arnfjörð Bjarmason | e8a8a4d | 2011-02-22 23:41:39 +0000 | [diff] [blame] | 1011 | fprintf(stderr, _("Switched to branch '%s'\n"), |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1012 | new_branch_info->name); |
Ævar Arnfjörð Bjarmason | 08eaa4b | 2011-02-22 23:41:43 +0000 | [diff] [blame] | 1013 | } |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1014 | } |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1015 | if (old_branch_info->path && old_branch_info->name) { |
| 1016 | if (!ref_exists(old_branch_info->path) && reflog_exists(old_branch_info->path)) |
| 1017 | delete_reflog(old_branch_info->path); |
Erick Mattos | 3631bf7 | 2010-05-21 21:28:37 -0300 | [diff] [blame] | 1018 | } |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1019 | } |
Nguyễn Thái Ngọc Duy | f4a4b9a | 2019-03-29 17:38:59 +0700 | [diff] [blame] | 1020 | remove_branch_state(the_repository, !opts->quiet); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1021 | strbuf_release(&msg); |
Junio C Hamano | 3266967 | 2011-02-08 04:32:49 -0600 | [diff] [blame] | 1022 | if (!opts->quiet && |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1023 | (new_branch_info->path || (!opts->force_detach && !strcmp(new_branch_info->name, "HEAD")))) |
| 1024 | report_tracking(new_branch_info); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1025 | } |
| 1026 | |
René Scharfe | 468224e | 2011-10-01 17:51:39 +0200 | [diff] [blame] | 1027 | static int add_pending_uninteresting_ref(const char *refname, |
Michael Haggerty | fcb615f | 2015-05-25 18:38:33 +0000 | [diff] [blame] | 1028 | const struct object_id *oid, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 1029 | int flags UNUSED, void *cb_data) |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1030 | { |
brian m. carlson | a58a1b0 | 2017-05-06 22:10:26 +0000 | [diff] [blame] | 1031 | add_pending_oid(cb_data, refname, oid, UNINTERESTING); |
Jeff King | 5c08dc4 | 2011-03-20 05:09:18 -0400 | [diff] [blame] | 1032 | return 0; |
| 1033 | } |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1034 | |
| 1035 | static void describe_one_orphan(struct strbuf *sb, struct commit *commit) |
| 1036 | { |
Jeff King | 0be240c | 2011-03-20 05:19:01 -0400 | [diff] [blame] | 1037 | strbuf_addstr(sb, " "); |
brian m. carlson | 30e677e | 2018-03-12 02:27:28 +0000 | [diff] [blame] | 1038 | strbuf_add_unique_abbrev(sb, &commit->object.oid, DEFAULT_ABBREV); |
Jeff King | 0be240c | 2011-03-20 05:19:01 -0400 | [diff] [blame] | 1039 | strbuf_addch(sb, ' '); |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 1040 | if (!repo_parse_commit(the_repository, commit)) |
Jeff King | 3c62183 | 2013-10-24 04:54:53 -0400 | [diff] [blame] | 1041 | pp_commit_easy(CMIT_FMT_ONELINE, commit, sb); |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1042 | strbuf_addch(sb, '\n'); |
| 1043 | } |
| 1044 | |
| 1045 | #define ORPHAN_CUTOFF 4 |
| 1046 | static void suggest_reattach(struct commit *commit, struct rev_info *revs) |
| 1047 | { |
| 1048 | struct commit *c, *last = NULL; |
| 1049 | struct strbuf sb = STRBUF_INIT; |
| 1050 | int lost = 0; |
| 1051 | while ((c = get_revision(revs)) != NULL) { |
| 1052 | if (lost < ORPHAN_CUTOFF) |
| 1053 | describe_one_orphan(&sb, c); |
| 1054 | last = c; |
| 1055 | lost++; |
| 1056 | } |
| 1057 | if (ORPHAN_CUTOFF < lost) { |
| 1058 | int more = lost - ORPHAN_CUTOFF; |
| 1059 | if (more == 1) |
| 1060 | describe_one_orphan(&sb, last); |
| 1061 | else |
Ævar Arnfjörð Bjarmason | f06f08b | 2011-04-10 19:34:08 +0000 | [diff] [blame] | 1062 | strbuf_addf(&sb, _(" ... and %d more.\n"), more); |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | fprintf(stderr, |
Ævar Arnfjörð Bjarmason | f06f08b | 2011-04-10 19:34:08 +0000 | [diff] [blame] | 1066 | Q_( |
| 1067 | /* The singular version */ |
| 1068 | "Warning: you are leaving %d commit behind, " |
| 1069 | "not connected to\n" |
| 1070 | "any of your branches:\n\n" |
Junio C Hamano | f807b3d | 2011-05-24 10:08:29 -0700 | [diff] [blame] | 1071 | "%s\n", |
Ævar Arnfjörð Bjarmason | f06f08b | 2011-04-10 19:34:08 +0000 | [diff] [blame] | 1072 | /* The plural version */ |
| 1073 | "Warning: you are leaving %d commits behind, " |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1074 | "not connected to\n" |
| 1075 | "any of your branches:\n\n" |
Junio C Hamano | 0faf247 | 2011-06-29 17:03:10 -0700 | [diff] [blame] | 1076 | "%s\n", |
Ævar Arnfjörð Bjarmason | f06f08b | 2011-04-10 19:34:08 +0000 | [diff] [blame] | 1077 | /* Give ngettext() the count */ |
| 1078 | lost), |
| 1079 | lost, |
Junio C Hamano | f807b3d | 2011-05-24 10:08:29 -0700 | [diff] [blame] | 1080 | sb.buf); |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1081 | strbuf_release(&sb); |
Junio C Hamano | f807b3d | 2011-05-24 10:08:29 -0700 | [diff] [blame] | 1082 | |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 1083 | if (advice_enabled(ADVICE_DETACHED_HEAD)) |
Junio C Hamano | f807b3d | 2011-05-24 10:08:29 -0700 | [diff] [blame] | 1084 | fprintf(stderr, |
Thomas Schneider | fc792ca | 2015-04-01 19:38:00 +0200 | [diff] [blame] | 1085 | Q_( |
| 1086 | /* The singular version */ |
| 1087 | "If you want to keep it by creating a new branch, " |
| 1088 | "this may be a good time\nto do so with:\n\n" |
| 1089 | " git branch <new-branch-name> %s\n\n", |
| 1090 | /* The plural version */ |
Junio C Hamano | f807b3d | 2011-05-24 10:08:29 -0700 | [diff] [blame] | 1091 | "If you want to keep them by creating a new branch, " |
| 1092 | "this may be a good time\nto do so with:\n\n" |
Thomas Schneider | fc792ca | 2015-04-01 19:38:00 +0200 | [diff] [blame] | 1093 | " git branch <new-branch-name> %s\n\n", |
| 1094 | /* Give ngettext() the count */ |
| 1095 | lost), |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1096 | repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV)); |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * We are about to leave commit that was at the tip of a detached |
| 1101 | * HEAD. If it is not reachable from any ref, this is the last chance |
| 1102 | * for the user to do so without resorting to reflog. |
| 1103 | */ |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1104 | static void orphaned_commit_warning(struct commit *old_commit, struct commit *new_commit) |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1105 | { |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1106 | struct rev_info revs; |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1107 | struct object *object = &old_commit->object; |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1108 | |
Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 17:57:38 +0200 | [diff] [blame] | 1109 | repo_init_revisions(the_repository, &revs, NULL); |
René Scharfe | 468224e | 2011-10-01 17:51:39 +0200 | [diff] [blame] | 1110 | setup_revisions(0, NULL, &revs, NULL); |
| 1111 | |
| 1112 | object->flags &= ~UNINTERESTING; |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1113 | add_pending_object(&revs, object, oid_to_hex(&object->oid)); |
René Scharfe | 468224e | 2011-10-01 17:51:39 +0200 | [diff] [blame] | 1114 | |
Michael Haggerty | fcb615f | 2015-05-25 18:38:33 +0000 | [diff] [blame] | 1115 | for_each_ref(add_pending_uninteresting_ref, &revs); |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 1116 | if (new_commit) |
| 1117 | add_pending_oid(&revs, "HEAD", |
| 1118 | &new_commit->object.oid, |
| 1119 | UNINTERESTING); |
René Scharfe | 468224e | 2011-10-01 17:51:39 +0200 | [diff] [blame] | 1120 | |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1121 | if (prepare_revision_walk(&revs)) |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 1122 | die(_("internal error in revision walk")); |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1123 | if (!(old_commit->object.flags & UNINTERESTING)) |
| 1124 | suggest_reattach(old_commit, &revs); |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1125 | else |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1126 | describe_detached_head(_("Previous HEAD position was"), old_commit); |
Jeff King | 5c08dc4 | 2011-03-20 05:09:18 -0400 | [diff] [blame] | 1127 | |
Martin Ågren | b2ccdf7 | 2017-09-23 01:34:51 +0200 | [diff] [blame] | 1128 | /* Clean up objects used, as they will be reused. */ |
René Scharfe | cd88884 | 2020-10-31 13:46:08 +0100 | [diff] [blame] | 1129 | repo_clear_commit_marks(the_repository, ALL_REV_FLAGS); |
Ævar Arnfjörð Bjarmason | 2108fe4 | 2022-04-13 22:01:36 +0200 | [diff] [blame] | 1130 | release_revisions(&revs); |
Junio C Hamano | 8e2dc6a | 2011-02-18 16:04:47 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
Nguyễn Thái Ngọc Duy | e51e305 | 2012-08-29 20:55:23 +0700 | [diff] [blame] | 1133 | static int switch_branches(const struct checkout_opts *opts, |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1134 | struct branch_info *new_branch_info) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1135 | { |
| 1136 | int ret = 0; |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1137 | struct branch_info old_branch_info = { 0 }; |
brian m. carlson | 60af769 | 2016-09-05 20:08:00 +0000 | [diff] [blame] | 1138 | struct object_id rev; |
Nguyễn Thái Ngọc Duy | a2b4994 | 2012-08-29 20:55:22 +0700 | [diff] [blame] | 1139 | int flag, writeout_error = 0; |
Nguyễn Thái Ngọc Duy | 65f099b | 2019-03-29 17:39:14 +0700 | [diff] [blame] | 1140 | int do_merge = 1; |
Jeff Hostetler | e27dd8a | 2019-02-22 14:25:08 -0800 | [diff] [blame] | 1141 | |
| 1142 | trace2_cmd_mode("branch"); |
| 1143 | |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1144 | memset(&old_branch_info, 0, sizeof(old_branch_info)); |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1145 | old_branch_info.path = resolve_refdup("HEAD", 0, &rev, &flag); |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1146 | if (old_branch_info.path) |
Stefan Beller | 21e1ee8 | 2018-06-28 18:21:57 -0700 | [diff] [blame] | 1147 | old_branch_info.commit = lookup_commit_reference_gently(the_repository, &rev, 1); |
Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 21:17:48 +0700 | [diff] [blame] | 1148 | if (!(flag & REF_ISSYMREF)) |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1149 | FREE_AND_NULL(old_branch_info.path); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1150 | |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1151 | if (old_branch_info.path) { |
| 1152 | const char *const prefix = "refs/heads/"; |
| 1153 | const char *p; |
| 1154 | if (skip_prefix(old_branch_info.path, prefix, &p)) |
| 1155 | old_branch_info.name = xstrdup(p); |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1156 | } |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1157 | |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 1158 | if (opts->new_orphan_branch && opts->orphan_from_empty_tree) { |
| 1159 | if (new_branch_info->name) |
| 1160 | BUG("'switch --orphan' should never accept a commit as starting point"); |
| 1161 | new_branch_info->commit = NULL; |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1162 | new_branch_info->name = xstrdup("(empty)"); |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 1163 | do_merge = 1; |
| 1164 | } |
| 1165 | |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1166 | if (!new_branch_info->name) { |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1167 | new_branch_info->name = xstrdup("HEAD"); |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1168 | new_branch_info->commit = old_branch_info.commit; |
| 1169 | if (!new_branch_info->commit) |
Ævar Arnfjörð Bjarmason | e8a8a4d | 2011-02-22 23:41:39 +0000 | [diff] [blame] | 1170 | die(_("You are on a branch yet to be born")); |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1171 | parse_commit_or_die(new_branch_info->commit); |
Nguyễn Thái Ngọc Duy | 65f099b | 2019-03-29 17:39:14 +0700 | [diff] [blame] | 1172 | |
| 1173 | if (opts->only_merge_on_switching_branches) |
| 1174 | do_merge = 0; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1175 | } |
| 1176 | |
Nguyễn Thái Ngọc Duy | 65f099b | 2019-03-29 17:39:14 +0700 | [diff] [blame] | 1177 | if (do_merge) { |
Ben Peart | fa655d8 | 2018-08-16 18:27:11 +0000 | [diff] [blame] | 1178 | ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error); |
| 1179 | if (ret) { |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1180 | branch_info_release(&old_branch_info); |
Ben Peart | fa655d8 | 2018-08-16 18:27:11 +0000 | [diff] [blame] | 1181 | return ret; |
| 1182 | } |
Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 21:17:48 +0700 | [diff] [blame] | 1183 | } |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1184 | |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1185 | if (!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit) |
| 1186 | orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit); |
Daniel Cordero | 77ebd56 | 2009-05-16 10:54:45 -0700 | [diff] [blame] | 1187 | |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1188 | update_refs_for_switch(opts, &old_branch_info, new_branch_info); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1189 | |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1190 | ret = post_checkout_hook(old_branch_info.commit, new_branch_info->commit, 1); |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1191 | branch_info_release(&old_branch_info); |
| 1192 | |
Nguyễn Thái Ngọc Duy | a2b4994 | 2012-08-29 20:55:22 +0700 | [diff] [blame] | 1193 | return ret || writeout_error; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1194 | } |
| 1195 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1196 | static int git_checkout_config(const char *var, const char *value, |
| 1197 | const struct config_context *ctx, void *cb) |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 1198 | { |
Denton Liu | 64f1f58 | 2020-10-07 22:48:15 -0700 | [diff] [blame] | 1199 | struct checkout_opts *opts = cb; |
| 1200 | |
Johannes Schindelin | 175f6e5 | 2010-08-25 12:34:57 +0200 | [diff] [blame] | 1201 | if (!strcmp(var, "diff.ignoresubmodules")) { |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 1202 | if (!value) |
| 1203 | return config_error_nonbool(var); |
Johannes Schindelin | 175f6e5 | 2010-08-25 12:34:57 +0200 | [diff] [blame] | 1204 | handle_ignore_submodules_arg(&opts->diff_options, value); |
| 1205 | return 0; |
| 1206 | } |
Denton Liu | 64f1f58 | 2020-10-07 22:48:15 -0700 | [diff] [blame] | 1207 | if (!strcmp(var, "checkout.guess")) { |
| 1208 | opts->dwim_new_local_branch = git_config_bool(var, value); |
| 1209 | return 0; |
| 1210 | } |
Jens Lehmann | 23b4c7b | 2010-08-28 16:50:07 +0200 | [diff] [blame] | 1211 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1212 | if (starts_with(var, "submodule.")) |
Brandon Williams | 7463e2e | 2017-08-03 11:19:53 -0700 | [diff] [blame] | 1213 | return git_default_submodule_config(var, value, NULL); |
Jens Lehmann | 23b4c7b | 2010-08-28 16:50:07 +0200 | [diff] [blame] | 1214 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1215 | return git_xmerge_config(var, value, ctx, NULL); |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
Nguyễn Thái Ngọc Duy | 7ab4ad0 | 2019-03-29 17:39:01 +0700 | [diff] [blame] | 1218 | static void setup_new_branch_info_and_source_tree( |
| 1219 | struct branch_info *new_branch_info, |
| 1220 | struct checkout_opts *opts, |
| 1221 | struct object_id *rev, |
| 1222 | const char *arg) |
| 1223 | { |
| 1224 | struct tree **source_tree = &opts->source_tree; |
| 1225 | struct object_id branch_rev; |
| 1226 | |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1227 | new_branch_info->name = xstrdup(arg); |
Nguyễn Thái Ngọc Duy | 7ab4ad0 | 2019-03-29 17:39:01 +0700 | [diff] [blame] | 1228 | setup_branch_path(new_branch_info); |
| 1229 | |
| 1230 | if (!check_refname_format(new_branch_info->path, 0) && |
| 1231 | !read_ref(new_branch_info->path, &branch_rev)) |
| 1232 | oidcpy(rev, &branch_rev); |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1233 | else |
| 1234 | /* not an existing branch */ |
| 1235 | FREE_AND_NULL(new_branch_info->path); |
Nguyễn Thái Ngọc Duy | 7ab4ad0 | 2019-03-29 17:39:01 +0700 | [diff] [blame] | 1236 | |
| 1237 | new_branch_info->commit = lookup_commit_reference_gently(the_repository, rev, 1); |
| 1238 | if (!new_branch_info->commit) { |
| 1239 | /* not a commit */ |
| 1240 | *source_tree = parse_tree_indirect(rev); |
| 1241 | } else { |
| 1242 | parse_commit_or_die(new_branch_info->commit); |
Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 15:58:48 +0200 | [diff] [blame] | 1243 | *source_tree = repo_get_commit_tree(the_repository, |
| 1244 | new_branch_info->commit); |
Nguyễn Thái Ngọc Duy | 7ab4ad0 | 2019-03-29 17:39:01 +0700 | [diff] [blame] | 1245 | } |
| 1246 | } |
| 1247 | |
Alexandr Miloslavskiy | 2957709 | 2019-12-30 18:38:12 +0000 | [diff] [blame] | 1248 | static const char *parse_remote_branch(const char *arg, |
| 1249 | struct object_id *rev, |
Alexandr Miloslavskiy | fa74180 | 2019-12-30 18:38:13 +0000 | [diff] [blame] | 1250 | int could_be_checkout_paths) |
Alexandr Miloslavskiy | 2957709 | 2019-12-30 18:38:12 +0000 | [diff] [blame] | 1251 | { |
Alexandr Miloslavskiy | fa74180 | 2019-12-30 18:38:13 +0000 | [diff] [blame] | 1252 | int num_matches = 0; |
| 1253 | const char *remote = unique_tracking_name(arg, rev, &num_matches); |
Alexandr Miloslavskiy | 2957709 | 2019-12-30 18:38:12 +0000 | [diff] [blame] | 1254 | |
| 1255 | if (remote && could_be_checkout_paths) { |
| 1256 | die(_("'%s' could be both a local file and a tracking branch.\n" |
| 1257 | "Please use -- (and optionally --no-guess) to disambiguate"), |
| 1258 | arg); |
| 1259 | } |
| 1260 | |
Alexandr Miloslavskiy | fa74180 | 2019-12-30 18:38:13 +0000 | [diff] [blame] | 1261 | if (!remote && num_matches > 1) { |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 1262 | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) { |
Alexandr Miloslavskiy | fa74180 | 2019-12-30 18:38:13 +0000 | [diff] [blame] | 1263 | advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" |
| 1264 | "you can do so by fully qualifying the name with the --track option:\n" |
| 1265 | "\n" |
| 1266 | " git checkout --track origin/<name>\n" |
| 1267 | "\n" |
| 1268 | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" |
| 1269 | "one remote, e.g. the 'origin' remote, consider setting\n" |
| 1270 | "checkout.defaultRemote=origin in your config.")); |
| 1271 | } |
| 1272 | |
| 1273 | die(_("'%s' matched multiple (%d) remote tracking branches"), |
| 1274 | arg, num_matches); |
| 1275 | } |
| 1276 | |
Alexandr Miloslavskiy | 2957709 | 2019-12-30 18:38:12 +0000 | [diff] [blame] | 1277 | return remote; |
| 1278 | } |
| 1279 | |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1280 | static int parse_branchname_arg(int argc, const char **argv, |
| 1281 | int dwim_new_local_branch_ok, |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1282 | struct branch_info *new_branch_info, |
Nguyễn Thái Ngọc Duy | 10f102b | 2015-01-03 16:41:25 +0700 | [diff] [blame] | 1283 | struct checkout_opts *opts, |
Alexandr Miloslavskiy | fa74180 | 2019-12-30 18:38:13 +0000 | [diff] [blame] | 1284 | struct object_id *rev) |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1285 | { |
Nguyễn Thái Ngọc Duy | 10f102b | 2015-01-03 16:41:25 +0700 | [diff] [blame] | 1286 | const char **new_branch = &opts->new_branch; |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1287 | int argcount = 0; |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1288 | const char *arg; |
Matthieu Moy | bca3969 | 2013-10-18 11:25:58 +0200 | [diff] [blame] | 1289 | int dash_dash_pos; |
| 1290 | int has_dash_dash = 0; |
| 1291 | int i; |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1292 | |
| 1293 | /* |
| 1294 | * case 1: git checkout <ref> -- [<paths>] |
| 1295 | * |
| 1296 | * <ref> must be a valid tree, everything after the '--' must be |
| 1297 | * a path. |
| 1298 | * |
| 1299 | * case 2: git checkout -- [<paths>] |
| 1300 | * |
| 1301 | * everything after the '--' must be paths. |
| 1302 | * |
Matthieu Moy | a047faf | 2013-10-18 11:25:57 +0200 | [diff] [blame] | 1303 | * case 3: git checkout <something> [--] |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1304 | * |
Matthieu Moy | a047faf | 2013-10-18 11:25:57 +0200 | [diff] [blame] | 1305 | * (a) If <something> is a commit, that is to |
| 1306 | * switch to the branch or detach HEAD at it. As a special case, |
| 1307 | * if <something> is A...B (missing A or B means HEAD but you can |
| 1308 | * omit at most one side), and if there is a unique merge base |
| 1309 | * between A and B, A...B names that merge base. |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1310 | * |
Matthieu Moy | a047faf | 2013-10-18 11:25:57 +0200 | [diff] [blame] | 1311 | * (b) If <something> is _not_ a commit, either "--" is present |
Andrei Rybak | b39a841 | 2023-01-07 14:56:55 +0100 | [diff] [blame] | 1312 | * or <something> is not a path, no -t or -b was given, |
Matthieu Moy | a047faf | 2013-10-18 11:25:57 +0200 | [diff] [blame] | 1313 | * and there is a tracking branch whose name is <something> |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 1314 | * in one and only one remote (or if the branch exists on the |
| 1315 | * remote named in checkout.defaultRemote), then this is a |
| 1316 | * short-hand to fork local <something> from that |
| 1317 | * remote-tracking branch. |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1318 | * |
Matthieu Moy | a047faf | 2013-10-18 11:25:57 +0200 | [diff] [blame] | 1319 | * (c) Otherwise, if "--" is present, treat it like case (1). |
| 1320 | * |
| 1321 | * (d) Otherwise : |
| 1322 | * - if it's a reference, treat it like case (1) |
| 1323 | * - else if it's a path, treat it like case (2) |
| 1324 | * - else: fail. |
| 1325 | * |
| 1326 | * case 4: git checkout <something> <paths> |
| 1327 | * |
| 1328 | * The first argument must not be ambiguous. |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1329 | * - If it's *only* a reference, treat it like case (1). |
| 1330 | * - If it's only a path, treat it like case (2). |
| 1331 | * - else: fail. |
| 1332 | * |
| 1333 | */ |
| 1334 | if (!argc) |
| 1335 | return 0; |
| 1336 | |
Nguyễn Thái Ngọc Duy | 5c06e26 | 2019-03-29 17:39:09 +0700 | [diff] [blame] | 1337 | if (!opts->accept_pathspec) { |
| 1338 | if (argc > 1) |
| 1339 | die(_("only one reference expected")); |
| 1340 | has_dash_dash = 1; /* helps disambiguate */ |
| 1341 | } |
| 1342 | |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1343 | arg = argv[0]; |
Matthieu Moy | bca3969 | 2013-10-18 11:25:58 +0200 | [diff] [blame] | 1344 | dash_dash_pos = -1; |
| 1345 | for (i = 0; i < argc; i++) { |
Nguyễn Thái Ngọc Duy | 5c06e26 | 2019-03-29 17:39:09 +0700 | [diff] [blame] | 1346 | if (opts->accept_pathspec && !strcmp(argv[i], "--")) { |
Matthieu Moy | bca3969 | 2013-10-18 11:25:58 +0200 | [diff] [blame] | 1347 | dash_dash_pos = i; |
| 1348 | break; |
| 1349 | } |
| 1350 | } |
| 1351 | if (dash_dash_pos == 0) |
| 1352 | return 1; /* case (2) */ |
| 1353 | else if (dash_dash_pos == 1) |
| 1354 | has_dash_dash = 1; /* case (3) or (1) */ |
| 1355 | else if (dash_dash_pos >= 2) |
| 1356 | die(_("only one reference expected, %d given."), dash_dash_pos); |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 1357 | opts->count_checkout_paths = !opts->quiet && !has_dash_dash; |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1358 | |
| 1359 | if (!strcmp(arg, "-")) |
| 1360 | arg = "@{-1}"; |
| 1361 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1362 | if (repo_get_oid_mb(the_repository, arg, rev)) { |
Matthieu Moy | a047faf | 2013-10-18 11:25:57 +0200 | [diff] [blame] | 1363 | /* |
| 1364 | * Either case (3) or (4), with <something> not being |
| 1365 | * a commit, or an attempt to use case (1) with an |
| 1366 | * invalid ref. |
| 1367 | * |
| 1368 | * It's likely an error, but we need to find out if |
| 1369 | * we should auto-create the branch, case (3).(b). |
| 1370 | */ |
| 1371 | int recover_with_dwim = dwim_new_local_branch_ok; |
| 1372 | |
Nguyễn Thái Ngọc Duy | be4908f | 2018-11-13 18:52:26 +0100 | [diff] [blame] | 1373 | int could_be_checkout_paths = !has_dash_dash && |
| 1374 | check_filename(opts->prefix, arg); |
| 1375 | |
| 1376 | if (!has_dash_dash && !no_wildcard(arg)) |
Matthieu Moy | a047faf | 2013-10-18 11:25:57 +0200 | [diff] [blame] | 1377 | recover_with_dwim = 0; |
Nguyễn Thái Ngọc Duy | be4908f | 2018-11-13 18:52:26 +0100 | [diff] [blame] | 1378 | |
Matthieu Moy | a047faf | 2013-10-18 11:25:57 +0200 | [diff] [blame] | 1379 | /* |
Nguyễn Thái Ngọc Duy | 5c06e26 | 2019-03-29 17:39:09 +0700 | [diff] [blame] | 1380 | * Accept "git checkout foo", "git checkout foo --" |
| 1381 | * and "git switch foo" as candidates for dwim. |
Matthieu Moy | a047faf | 2013-10-18 11:25:57 +0200 | [diff] [blame] | 1382 | */ |
| 1383 | if (!(argc == 1 && !has_dash_dash) && |
Nguyễn Thái Ngọc Duy | 5c06e26 | 2019-03-29 17:39:09 +0700 | [diff] [blame] | 1384 | !(argc == 2 && has_dash_dash) && |
| 1385 | opts->accept_pathspec) |
Matthieu Moy | a047faf | 2013-10-18 11:25:57 +0200 | [diff] [blame] | 1386 | recover_with_dwim = 0; |
| 1387 | |
| 1388 | if (recover_with_dwim) { |
Alexandr Miloslavskiy | 2957709 | 2019-12-30 18:38:12 +0000 | [diff] [blame] | 1389 | const char *remote = parse_remote_branch(arg, rev, |
Alexandr Miloslavskiy | fa74180 | 2019-12-30 18:38:13 +0000 | [diff] [blame] | 1390 | could_be_checkout_paths); |
Matthieu Moy | a047faf | 2013-10-18 11:25:57 +0200 | [diff] [blame] | 1391 | if (remote) { |
| 1392 | *new_branch = arg; |
| 1393 | arg = remote; |
| 1394 | /* DWIMmed to create local branch, case (3).(b) */ |
| 1395 | } else { |
| 1396 | recover_with_dwim = 0; |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | if (!recover_with_dwim) { |
| 1401 | if (has_dash_dash) |
| 1402 | die(_("invalid reference: %s"), arg); |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1403 | return argcount; |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | /* we can't end up being in (2) anymore, eat the argument */ |
| 1408 | argcount++; |
| 1409 | argv++; |
| 1410 | argc--; |
| 1411 | |
Nguyễn Thái Ngọc Duy | 7ab4ad0 | 2019-03-29 17:39:01 +0700 | [diff] [blame] | 1412 | setup_new_branch_info_and_source_tree(new_branch_info, opts, rev, arg); |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1413 | |
Nguyễn Thái Ngọc Duy | 7ab4ad0 | 2019-03-29 17:39:01 +0700 | [diff] [blame] | 1414 | if (!opts->source_tree) /* case (1): want a tree */ |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 1415 | die(_("reference is not a tree: %s"), arg); |
Nguyễn Thái Ngọc Duy | 7ab4ad0 | 2019-03-29 17:39:01 +0700 | [diff] [blame] | 1416 | |
Nguyễn Thái Ngọc Duy | 7f82b24 | 2016-09-07 18:19:39 +0700 | [diff] [blame] | 1417 | if (!has_dash_dash) { /* case (3).(d) -> (1) */ |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1418 | /* |
| 1419 | * Do not complain the most common case |
| 1420 | * git checkout branch |
| 1421 | * even if there happen to be a file called 'branch'; |
| 1422 | * it would be extremely annoying. |
| 1423 | */ |
| 1424 | if (argc) |
Nguyễn Thái Ngọc Duy | b829b94 | 2016-09-07 18:19:41 +0700 | [diff] [blame] | 1425 | verify_non_filename(opts->prefix, arg); |
Nguyễn Thái Ngọc Duy | 5c06e26 | 2019-03-29 17:39:09 +0700 | [diff] [blame] | 1426 | } else if (opts->accept_pathspec) { |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1427 | argcount++; |
| 1428 | argv++; |
| 1429 | argc--; |
| 1430 | } |
| 1431 | |
| 1432 | return argcount; |
| 1433 | } |
| 1434 | |
Nguyễn Thái Ngọc Duy | a2b4994 | 2012-08-29 20:55:22 +0700 | [diff] [blame] | 1435 | static int switch_unborn_to_new_branch(const struct checkout_opts *opts) |
Junio C Hamano | abe1998 | 2012-01-30 12:10:08 -0800 | [diff] [blame] | 1436 | { |
| 1437 | int status; |
| 1438 | struct strbuf branch_ref = STRBUF_INIT; |
| 1439 | |
Jeff Hostetler | e27dd8a | 2019-02-22 14:25:08 -0800 | [diff] [blame] | 1440 | trace2_cmd_mode("unborn"); |
| 1441 | |
Chris Webb | 8ced1aa | 2012-06-26 16:06:42 +0100 | [diff] [blame] | 1442 | if (!opts->new_branch) |
| 1443 | die(_("You are on a branch yet to be born")); |
Junio C Hamano | abe1998 | 2012-01-30 12:10:08 -0800 | [diff] [blame] | 1444 | strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch); |
| 1445 | status = create_symref("HEAD", branch_ref.buf, "checkout -b"); |
| 1446 | strbuf_release(&branch_ref); |
Jeff King | afa8c07 | 2012-11-15 09:12:33 -0800 | [diff] [blame] | 1447 | if (!opts->quiet) |
| 1448 | fprintf(stderr, _("Switched to a new branch '%s'\n"), |
| 1449 | opts->new_branch); |
Junio C Hamano | abe1998 | 2012-01-30 12:10:08 -0800 | [diff] [blame] | 1450 | return status; |
| 1451 | } |
| 1452 | |
Nguyễn Thái Ngọc Duy | 7968bef | 2019-03-29 17:39:11 +0700 | [diff] [blame] | 1453 | static void die_expecting_a_branch(const struct branch_info *branch_info) |
| 1454 | { |
| 1455 | struct object_id oid; |
| 1456 | char *to_free; |
Alex Henrie | 808213b | 2022-02-25 23:12:13 -0700 | [diff] [blame] | 1457 | int code; |
Nguyễn Thái Ngọc Duy | 7968bef | 2019-03-29 17:39:11 +0700 | [diff] [blame] | 1458 | |
Ævar Arnfjörð Bjarmason | 12cb1c1 | 2023-03-28 15:58:54 +0200 | [diff] [blame] | 1459 | if (repo_dwim_ref(the_repository, branch_info->name, |
| 1460 | strlen(branch_info->name), &oid, &to_free, 0) == 1) { |
Nguyễn Thái Ngọc Duy | 7968bef | 2019-03-29 17:39:11 +0700 | [diff] [blame] | 1461 | const char *ref = to_free; |
| 1462 | |
| 1463 | if (skip_prefix(ref, "refs/tags/", &ref)) |
Alex Henrie | 808213b | 2022-02-25 23:12:13 -0700 | [diff] [blame] | 1464 | code = die_message(_("a branch is expected, got tag '%s'"), ref); |
| 1465 | else if (skip_prefix(ref, "refs/remotes/", &ref)) |
| 1466 | code = die_message(_("a branch is expected, got remote branch '%s'"), ref); |
| 1467 | else |
| 1468 | code = die_message(_("a branch is expected, got '%s'"), ref); |
Nguyễn Thái Ngọc Duy | 7968bef | 2019-03-29 17:39:11 +0700 | [diff] [blame] | 1469 | } |
Alex Henrie | 808213b | 2022-02-25 23:12:13 -0700 | [diff] [blame] | 1470 | else if (branch_info->commit) |
| 1471 | code = die_message(_("a branch is expected, got commit '%s'"), branch_info->name); |
| 1472 | else |
| 1473 | /* |
| 1474 | * This case should never happen because we already die() on |
| 1475 | * non-commit, but just in case. |
| 1476 | */ |
| 1477 | code = die_message(_("a branch is expected, got '%s'"), branch_info->name); |
| 1478 | |
| 1479 | if (advice_enabled(ADVICE_SUGGEST_DETACHING_HEAD)) |
| 1480 | advise(_("If you want to detach HEAD at the commit, try again with the --detach option.")); |
| 1481 | |
| 1482 | exit(code); |
Nguyễn Thái Ngọc Duy | 7968bef | 2019-03-29 17:39:11 +0700 | [diff] [blame] | 1483 | } |
| 1484 | |
Nguyễn Thái Ngọc Duy | c45f0f5 | 2019-03-29 17:39:15 +0700 | [diff] [blame] | 1485 | static void die_if_some_operation_in_progress(void) |
| 1486 | { |
| 1487 | struct wt_status_state state; |
| 1488 | |
| 1489 | memset(&state, 0, sizeof(state)); |
| 1490 | wt_status_get_state(the_repository, &state, 0); |
| 1491 | |
| 1492 | if (state.merge_in_progress) |
| 1493 | die(_("cannot switch branch while merging\n" |
| 1494 | "Consider \"git merge --quit\" " |
| 1495 | "or \"git worktree add\".")); |
| 1496 | if (state.am_in_progress) |
| 1497 | die(_("cannot switch branch in the middle of an am session\n" |
| 1498 | "Consider \"git am --quit\" " |
| 1499 | "or \"git worktree add\".")); |
| 1500 | if (state.rebase_interactive_in_progress || state.rebase_in_progress) |
| 1501 | die(_("cannot switch branch while rebasing\n" |
| 1502 | "Consider \"git rebase --quit\" " |
| 1503 | "or \"git worktree add\".")); |
| 1504 | if (state.cherry_pick_in_progress) |
| 1505 | die(_("cannot switch branch while cherry-picking\n" |
| 1506 | "Consider \"git cherry-pick --quit\" " |
| 1507 | "or \"git worktree add\".")); |
| 1508 | if (state.revert_in_progress) |
| 1509 | die(_("cannot switch branch while reverting\n" |
| 1510 | "Consider \"git revert --quit\" " |
| 1511 | "or \"git worktree add\".")); |
| 1512 | if (state.bisect_in_progress) |
Nguyễn Thái Ngọc Duy | d16dc42 | 2019-06-20 16:55:21 +0700 | [diff] [blame] | 1513 | warning(_("you are switching branch while bisecting")); |
Ævar Arnfjörð Bjarmason | b6046ab | 2022-11-08 19:17:42 +0100 | [diff] [blame] | 1514 | |
| 1515 | wt_status_state_free_buffers(&state); |
Nguyễn Thái Ngọc Duy | c45f0f5 | 2019-03-29 17:39:15 +0700 | [diff] [blame] | 1516 | } |
| 1517 | |
Junio C Hamano | 9263c40 | 2023-11-23 14:11:41 +0900 | [diff] [blame] | 1518 | /* |
| 1519 | * die if attempting to checkout an existing branch that is in use |
| 1520 | * in another worktree, unless ignore-other-wortrees option is given. |
| 1521 | * The check is bypassed when the branch is already the current one, |
| 1522 | * as it will not make things any worse. |
| 1523 | */ |
| 1524 | static void die_if_switching_to_a_branch_in_use(struct checkout_opts *opts, |
| 1525 | const char *full_ref) |
| 1526 | { |
| 1527 | int flags; |
| 1528 | char *head_ref; |
| 1529 | |
| 1530 | if (opts->ignore_other_worktrees) |
| 1531 | return; |
| 1532 | head_ref = resolve_refdup("HEAD", 0, NULL, &flags); |
| 1533 | if (head_ref && (!(flags & REF_ISSYMREF) || strcmp(head_ref, full_ref))) |
| 1534 | die_if_checked_out(full_ref, 1); |
| 1535 | free(head_ref); |
| 1536 | } |
| 1537 | |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1538 | static int checkout_branch(struct checkout_opts *opts, |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1539 | struct branch_info *new_branch_info) |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1540 | { |
Nguyễn Thái Ngọc Duy | 817b345 | 2013-07-14 15:35:41 +0700 | [diff] [blame] | 1541 | if (opts->pathspec.nr) |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1542 | die(_("paths cannot be used with switching branches")); |
| 1543 | |
| 1544 | if (opts->patch_mode) |
| 1545 | die(_("'%s' cannot be used with switching branches"), |
| 1546 | "--patch"); |
| 1547 | |
Nguyễn Thái Ngọc Duy | a6cfb9b | 2019-04-25 16:45:48 +0700 | [diff] [blame] | 1548 | if (opts->overlay_mode != -1) |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 1549 | die(_("'%s' cannot be used with switching branches"), |
Nguyễn Thái Ngọc Duy | a6cfb9b | 2019-04-25 16:45:48 +0700 | [diff] [blame] | 1550 | "--[no]-overlay"); |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 1551 | |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1552 | if (opts->writeout_stage) |
| 1553 | die(_("'%s' cannot be used with switching branches"), |
| 1554 | "--ours/--theirs"); |
| 1555 | |
| 1556 | if (opts->force && opts->merge) |
| 1557 | die(_("'%s' cannot be used with '%s'"), "-f", "-m"); |
| 1558 | |
Nguyễn Thái Ngọc Duy | 3ec37ad | 2019-03-29 17:39:07 +0700 | [diff] [blame] | 1559 | if (opts->discard_changes && opts->merge) |
| 1560 | die(_("'%s' cannot be used with '%s'"), "--discard-changes", "--merge"); |
| 1561 | |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1562 | if (opts->force_detach && opts->new_branch) |
| 1563 | die(_("'%s' cannot be used with '%s'"), |
| 1564 | "--detach", "-b/-B/--orphan"); |
| 1565 | |
| 1566 | if (opts->new_orphan_branch) { |
| 1567 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) |
| 1568 | die(_("'%s' cannot be used with '%s'"), "--orphan", "-t"); |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 1569 | if (opts->orphan_from_empty_tree && new_branch_info->name) |
| 1570 | die(_("'%s' cannot take <start-point>"), "--orphan"); |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1571 | } else if (opts->force_detach) { |
| 1572 | if (opts->track != BRANCH_TRACK_UNSPECIFIED) |
| 1573 | die(_("'%s' cannot be used with '%s'"), "--detach", "-t"); |
| 1574 | } else if (opts->track == BRANCH_TRACK_UNSPECIFIED) |
| 1575 | opts->track = git_branch_track; |
| 1576 | |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1577 | if (new_branch_info->name && !new_branch_info->commit) |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1578 | die(_("Cannot switch branch to a non-commit '%s'"), |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1579 | new_branch_info->name); |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1580 | |
Nguyễn Thái Ngọc Duy | e342c63 | 2019-03-29 17:39:10 +0700 | [diff] [blame] | 1581 | if (!opts->switch_branch_doing_nothing_is_ok && |
| 1582 | !new_branch_info->name && |
| 1583 | !opts->new_branch && |
| 1584 | !opts->force_detach) |
| 1585 | die(_("missing branch or commit argument")); |
| 1586 | |
Nguyễn Thái Ngọc Duy | 7968bef | 2019-03-29 17:39:11 +0700 | [diff] [blame] | 1587 | if (!opts->implicit_detach && |
| 1588 | !opts->force_detach && |
| 1589 | !opts->new_branch && |
| 1590 | !opts->new_branch_force && |
| 1591 | new_branch_info->name && |
| 1592 | !new_branch_info->path) |
| 1593 | die_expecting_a_branch(new_branch_info); |
| 1594 | |
Nguyễn Thái Ngọc Duy | c45f0f5 | 2019-03-29 17:39:15 +0700 | [diff] [blame] | 1595 | if (!opts->can_switch_when_in_progress) |
| 1596 | die_if_some_operation_in_progress(); |
| 1597 | |
Junio C Hamano | 9263c40 | 2023-11-23 14:11:41 +0900 | [diff] [blame] | 1598 | /* "git checkout <branch>" */ |
| 1599 | if (new_branch_info->path && !opts->force_detach && !opts->new_branch) |
| 1600 | die_if_switching_to_a_branch_in_use(opts, new_branch_info->path); |
Nguyễn Thái Ngọc Duy | e1c1ab9 | 2015-06-12 17:49:24 +0700 | [diff] [blame] | 1601 | |
Junio C Hamano | b23285a | 2023-11-23 15:00:31 +0900 | [diff] [blame] | 1602 | /* "git checkout -B <branch>" */ |
| 1603 | if (opts->new_branch_force) { |
| 1604 | char *full_ref = xstrfmt("refs/heads/%s", opts->new_branch); |
| 1605 | die_if_switching_to_a_branch_in_use(opts, full_ref); |
| 1606 | free(full_ref); |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1607 | } |
| 1608 | |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1609 | if (!new_branch_info->commit && opts->new_branch) { |
brian m. carlson | 60af769 | 2016-09-05 20:08:00 +0000 | [diff] [blame] | 1610 | struct object_id rev; |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1611 | int flag; |
| 1612 | |
brian m. carlson | 34c290a | 2017-10-15 22:06:56 +0000 | [diff] [blame] | 1613 | if (!read_ref_full("HEAD", 0, &rev, &flag) && |
brian m. carlson | 60af769 | 2016-09-05 20:08:00 +0000 | [diff] [blame] | 1614 | (flag & REF_ISSYMREF) && is_null_oid(&rev)) |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1615 | return switch_unborn_to_new_branch(opts); |
| 1616 | } |
Brandon Williams | c8a3ea1 | 2018-02-14 10:59:31 -0800 | [diff] [blame] | 1617 | return switch_branches(opts, new_branch_info); |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1618 | } |
| 1619 | |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 1620 | static struct option *add_common_options(struct checkout_opts *opts, |
| 1621 | struct option *prevopts) |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1622 | { |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1623 | struct option options[] = { |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1624 | OPT__QUIET(&opts->quiet, N_("suppress progress reporting")), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1625 | OPT_CALLBACK_F(0, "recurse-submodules", NULL, |
Stefan Beller | 1fc458d | 2017-03-14 14:46:41 -0700 | [diff] [blame] | 1626 | "checkout", "control recursive updating of submodules", |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1627 | PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater), |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 1628 | OPT_BOOL(0, "progress", &opts->show_progress, N_("force progress reporting")), |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 1629 | OPT_BOOL('m', "merge", &opts->merge, N_("perform a 3-way merge with the new branch")), |
| 1630 | OPT_STRING(0, "conflict", &opts->conflict_style, N_("style"), |
Elijah Newren | ddfc44a8 | 2021-12-01 00:05:07 +0000 | [diff] [blame] | 1631 | N_("conflict style (merge, diff3, or zdiff3)")), |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 1632 | OPT_END() |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1633 | }; |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 1634 | struct option *newopts = parse_options_concat(prevopts, options); |
| 1635 | free(prevopts); |
| 1636 | return newopts; |
| 1637 | } |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1638 | |
Nguyễn Thái Ngọc Duy | b7b5fce | 2019-03-29 17:39:06 +0700 | [diff] [blame] | 1639 | static struct option *add_common_switch_branch_options( |
| 1640 | struct checkout_opts *opts, struct option *prevopts) |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 1641 | { |
| 1642 | struct option options[] = { |
Nguyễn Thái Ngọc Duy | 163e3b2 | 2019-03-29 17:39:12 +0700 | [diff] [blame] | 1643 | OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")), |
René Scharfe | 6327f0e | 2022-01-20 13:35:54 +0100 | [diff] [blame] | 1644 | OPT_CALLBACK_F('t', "track", &opts->track, "(direct|inherit)", |
Josh Steadmon | 15f0028 | 2022-01-18 12:49:46 -0800 | [diff] [blame] | 1645 | N_("set branch tracking configuration"), |
| 1646 | PARSE_OPT_OPTARG, |
Josh Steadmon | d311566 | 2021-12-20 19:30:23 -0800 | [diff] [blame] | 1647 | parse_opt_tracking_mode), |
Nguyễn Thái Ngọc Duy | a5e5f39 | 2019-04-25 16:45:53 +0700 | [diff] [blame] | 1648 | OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"), |
| 1649 | PARSE_OPT_NOCOMPLETE), |
Junio C Hamano | d44b517 | 2023-11-24 12:10:42 +0900 | [diff] [blame] | 1650 | OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unborn branch")), |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 1651 | OPT_BOOL_F(0, "overwrite-ignore", &opts->overwrite_ignore, |
| 1652 | N_("update ignored files (default)"), |
| 1653 | PARSE_OPT_NOCOMPLETE), |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 1654 | OPT_BOOL(0, "ignore-other-worktrees", &opts->ignore_other_worktrees, |
| 1655 | N_("do not check if another worktree is holding the given ref")), |
| 1656 | OPT_END() |
| 1657 | }; |
| 1658 | struct option *newopts = parse_options_concat(prevopts, options); |
| 1659 | free(prevopts); |
| 1660 | return newopts; |
| 1661 | } |
| 1662 | |
| 1663 | static struct option *add_checkout_path_options(struct checkout_opts *opts, |
| 1664 | struct option *prevopts) |
| 1665 | { |
| 1666 | struct option options[] = { |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1667 | OPT_SET_INT_F('2', "ours", &opts->writeout_stage, |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1668 | N_("checkout our version for unmerged files"), |
| 1669 | 2, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1670 | OPT_SET_INT_F('3', "theirs", &opts->writeout_stage, |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1671 | N_("checkout their version for unmerged files"), |
| 1672 | 3, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1673 | OPT_BOOL('p', "patch", &opts->patch_mode, N_("select hunks interactively")), |
| 1674 | OPT_BOOL(0, "ignore-skip-worktree-bits", &opts->ignore_skipworktree, |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1675 | N_("do not limit pathspecs to sparse entries only")), |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 1676 | OPT_PATHSPEC_FROM_FILE(&opts->pathspec_from_file), |
| 1677 | OPT_PATHSPEC_FILE_NUL(&opts->pathspec_file_nul), |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 1678 | OPT_END() |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1679 | }; |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 1680 | struct option *newopts = parse_options_concat(prevopts, options); |
| 1681 | free(prevopts); |
| 1682 | return newopts; |
| 1683 | } |
| 1684 | |
Denton Liu | 7c16ef7 | 2020-04-30 07:54:57 -0400 | [diff] [blame] | 1685 | /* create-branch option (either b or c) */ |
| 1686 | static char cb_option = 'b'; |
| 1687 | |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1688 | static int checkout_main(int argc, const char **argv, const char *prefix, |
| 1689 | struct checkout_opts *opts, struct option *options, |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1690 | const char * const usagestr[], |
| 1691 | struct branch_info *new_branch_info) |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 1692 | { |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 1693 | int parseopt_flags = 0; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1694 | |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1695 | opts->overwrite_ignore = 1; |
| 1696 | opts->prefix = prefix; |
| 1697 | opts->show_progress = -1; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1698 | |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1699 | git_config(git_checkout_config, opts); |
Johannes Schindelin | 059fda1 | 2022-02-08 11:21:53 +0000 | [diff] [blame] | 1700 | if (the_repository->gitdir) { |
| 1701 | prepare_repo_settings(the_repository); |
| 1702 | the_repository->settings.command_requires_full_index = 0; |
| 1703 | } |
Derrick Stolee | 1ba5f45 | 2021-06-29 02:13:06 +0000 | [diff] [blame] | 1704 | |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1705 | opts->track = BRANCH_TRACK_UNSPECIFIED; |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1706 | |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 1707 | if (!opts->accept_pathspec && !opts->accept_ref) |
| 1708 | BUG("make up your mind, you need to take _something_"); |
| 1709 | if (opts->accept_pathspec && opts->accept_ref) |
| 1710 | parseopt_flags = PARSE_OPT_KEEP_DASHDASH; |
Pierre Habouzit | 859fdab | 2008-07-23 12:15:33 +0200 | [diff] [blame] | 1711 | |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 1712 | argc = parse_options(argc, argv, prefix, options, |
| 1713 | usagestr, parseopt_flags); |
Pierre Habouzit | 859fdab | 2008-07-23 12:15:33 +0200 | [diff] [blame] | 1714 | |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1715 | if (opts->show_progress < 0) { |
| 1716 | if (opts->quiet) |
| 1717 | opts->show_progress = 0; |
Edmundo Carmona Antoranz | 870ebdb | 2015-11-01 15:19:05 -0600 | [diff] [blame] | 1718 | else |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1719 | opts->show_progress = isatty(2); |
Edmundo Carmona Antoranz | 870ebdb | 2015-11-01 15:19:05 -0600 | [diff] [blame] | 1720 | } |
| 1721 | |
Nguyễn Thái Ngọc Duy | 55cf704 | 2019-03-29 17:39:03 +0700 | [diff] [blame] | 1722 | if (opts->conflict_style) { |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1723 | struct key_value_info kvi = KVI_INIT; |
| 1724 | struct config_context ctx = { |
| 1725 | .kvi = &kvi, |
| 1726 | }; |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1727 | opts->merge = 1; /* implied */ |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1728 | git_xmerge_config("merge.conflictstyle", opts->conflict_style, |
| 1729 | &ctx, NULL); |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1730 | } |
Nguyễn Thái Ngọc Duy | a5e5f39 | 2019-04-25 16:45:53 +0700 | [diff] [blame] | 1731 | if (opts->force) { |
Nguyễn Thái Ngọc Duy | 3ec37ad | 2019-03-29 17:39:07 +0700 | [diff] [blame] | 1732 | opts->discard_changes = 1; |
Nguyễn Thái Ngọc Duy | a5e5f39 | 2019-04-25 16:45:53 +0700 | [diff] [blame] | 1733 | opts->ignore_unmerged_opt = "--force"; |
| 1734 | opts->ignore_unmerged = 1; |
Ævar Arnfjörð Bjarmason | e8a8a4d | 2011-02-22 23:41:39 +0000 | [diff] [blame] | 1735 | } |
Tay Ray Chuan | 02ac983 | 2010-06-24 03:29:00 +0800 | [diff] [blame] | 1736 | |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1737 | if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 1738 | die(_("options '-%c', '-%c', and '%s' cannot be used together"), |
| 1739 | cb_option, toupper(cb_option), "--orphan"); |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1740 | |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1741 | if (opts->overlay_mode == 1 && opts->patch_mode) |
Jean-Noël Avila | 43ea635 | 2022-01-05 20:02:14 +0000 | [diff] [blame] | 1742 | die(_("options '%s' and '%s' cannot be used together"), "-p", "--overlay"); |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 1743 | |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 1744 | if (opts->checkout_index >= 0 || opts->checkout_worktree >= 0) { |
| 1745 | if (opts->checkout_index < 0) |
| 1746 | opts->checkout_index = 0; |
| 1747 | if (opts->checkout_worktree < 0) |
| 1748 | opts->checkout_worktree = 0; |
| 1749 | } else { |
| 1750 | if (opts->checkout_index < 0) |
| 1751 | opts->checkout_index = -opts->checkout_index - 1; |
| 1752 | if (opts->checkout_worktree < 0) |
| 1753 | opts->checkout_worktree = -opts->checkout_worktree - 1; |
| 1754 | } |
| 1755 | if (opts->checkout_index < 0 || opts->checkout_worktree < 0) |
| 1756 | BUG("these flags should be non-negative by now"); |
Nguyễn Thái Ngọc Duy | 3a733ce | 2019-04-25 16:45:52 +0700 | [diff] [blame] | 1757 | /* |
Eric Sunshine | 088018e | 2020-05-05 03:17:16 -0400 | [diff] [blame] | 1758 | * convenient shortcut: "git restore --staged [--worktree]" equals |
| 1759 | * "git restore --staged [--worktree] --source HEAD" |
Nguyễn Thái Ngọc Duy | 3a733ce | 2019-04-25 16:45:52 +0700 | [diff] [blame] | 1760 | */ |
Eric Sunshine | 088018e | 2020-05-05 03:17:16 -0400 | [diff] [blame] | 1761 | if (!opts->from_treeish && opts->checkout_index) |
Nguyễn Thái Ngọc Duy | 3a733ce | 2019-04-25 16:45:52 +0700 | [diff] [blame] | 1762 | opts->from_treeish = "HEAD"; |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 1763 | |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1764 | /* |
| 1765 | * From here on, new_branch will contain the branch to be checked out, |
| 1766 | * and new_branch_force and new_orphan_branch will tell us which one of |
Denton Liu | 7c16ef7 | 2020-04-30 07:54:57 -0400 | [diff] [blame] | 1767 | * -b/-B/-c/-C/--orphan is being used. |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1768 | */ |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1769 | if (opts->new_branch_force) |
| 1770 | opts->new_branch = opts->new_branch_force; |
Tay Ray Chuan | 02ac983 | 2010-06-24 03:29:00 +0800 | [diff] [blame] | 1771 | |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1772 | if (opts->new_orphan_branch) |
| 1773 | opts->new_branch = opts->new_orphan_branch; |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 1774 | |
Denton Liu | 7c16ef7 | 2020-04-30 07:54:57 -0400 | [diff] [blame] | 1775 | /* --track without -c/-C/-b/-B/--orphan should DWIM */ |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1776 | if (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) { |
Alex Riesen | 9188ed8 | 2008-08-21 19:23:20 +0200 | [diff] [blame] | 1777 | const char *argv0 = argv[0]; |
| 1778 | if (!argc || !strcmp(argv0, "--")) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 1779 | die(_("--track needs a branch name")); |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 1780 | skip_prefix(argv0, "refs/", &argv0); |
| 1781 | skip_prefix(argv0, "remotes/", &argv0); |
Alex Riesen | 9188ed8 | 2008-08-21 19:23:20 +0200 | [diff] [blame] | 1782 | argv0 = strchr(argv0, '/'); |
| 1783 | if (!argv0 || !argv0[1]) |
Denton Liu | 7c16ef7 | 2020-04-30 07:54:57 -0400 | [diff] [blame] | 1784 | die(_("missing branch name; try -%c"), cb_option); |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1785 | opts->new_branch = argv0 + 1; |
Johannes Schindelin | bb0ceb6 | 2008-08-09 16:00:12 +0200 | [diff] [blame] | 1786 | } |
| 1787 | |
Pierre Habouzit | 859fdab | 2008-07-23 12:15:33 +0200 | [diff] [blame] | 1788 | /* |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1789 | * Extract branch name from command line arguments, so |
| 1790 | * all that is left is pathspecs. |
Pierre Habouzit | 859fdab | 2008-07-23 12:15:33 +0200 | [diff] [blame] | 1791 | * |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1792 | * Handle |
Pierre Habouzit | 859fdab | 2008-07-23 12:15:33 +0200 | [diff] [blame] | 1793 | * |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1794 | * 1) git checkout <tree> -- [<paths>] |
| 1795 | * 2) git checkout -- [<paths>] |
| 1796 | * 3) git checkout <something> [<paths>] |
Pierre Habouzit | 859fdab | 2008-07-23 12:15:33 +0200 | [diff] [blame] | 1797 | * |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1798 | * including "last branch" syntax and DWIM-ery for names of |
| 1799 | * remote branches, erroring out for invalid or ambiguous cases. |
Pierre Habouzit | 859fdab | 2008-07-23 12:15:33 +0200 | [diff] [blame] | 1800 | */ |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 1801 | if (argc && opts->accept_ref) { |
brian m. carlson | 60af769 | 2016-09-05 20:08:00 +0000 | [diff] [blame] | 1802 | struct object_id rev; |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1803 | int dwim_ok = |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1804 | !opts->patch_mode && |
Nguyễn Thái Ngọc Duy | ccb111b | 2019-03-29 17:39:13 +0700 | [diff] [blame] | 1805 | opts->dwim_new_local_branch && |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1806 | opts->track == BRANCH_TRACK_UNSPECIFIED && |
| 1807 | !opts->new_branch; |
Jonathan Nieder | f8bd36a | 2011-02-08 04:34:34 -0600 | [diff] [blame] | 1808 | int n = parse_branchname_arg(argc, argv, dwim_ok, |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1809 | new_branch_info, opts, &rev); |
Jonathan Nieder | 09ebad6 | 2011-02-08 04:29:09 -0600 | [diff] [blame] | 1810 | argv += n; |
| 1811 | argc -= n; |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 1812 | } else if (!opts->accept_ref && opts->from_treeish) { |
| 1813 | struct object_id rev; |
| 1814 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1815 | if (repo_get_oid_mb(the_repository, opts->from_treeish, &rev)) |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 1816 | die(_("could not resolve %s"), opts->from_treeish); |
| 1817 | |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1818 | setup_new_branch_info_and_source_tree(new_branch_info, |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 1819 | opts, &rev, |
| 1820 | opts->from_treeish); |
| 1821 | |
| 1822 | if (!opts->source_tree) |
| 1823 | die(_("reference is not a tree: %s"), opts->from_treeish); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1824 | } |
| 1825 | |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1826 | if (argc) { |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1827 | parse_pathspec(&opts->pathspec, 0, |
| 1828 | opts->patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0, |
Nguyễn Thái Ngọc Duy | 480ca64 | 2013-07-14 15:35:50 +0700 | [diff] [blame] | 1829 | prefix, argv); |
Alex Riesen | 301e42e | 2008-02-28 17:30:47 +0100 | [diff] [blame] | 1830 | |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1831 | if (!opts->pathspec.nr) |
Ævar Arnfjörð Bjarmason | e8a8a4d | 2011-02-22 23:41:39 +0000 | [diff] [blame] | 1832 | die(_("invalid path specification")); |
Alex Riesen | 301e42e | 2008-02-28 17:30:47 +0100 | [diff] [blame] | 1833 | |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1834 | /* |
| 1835 | * Try to give more helpful suggestion. |
| 1836 | * new_branch && argc > 1 will be caught later. |
| 1837 | */ |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1838 | if (opts->new_branch && argc == 1 && !new_branch_info->commit) |
Jean-Noel Avila | 6c48686 | 2017-05-11 14:06:32 +0200 | [diff] [blame] | 1839 | die(_("'%s' is not a commit and a branch '%s' cannot be created from it"), |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1840 | argv[0], opts->new_branch); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1841 | |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1842 | if (opts->force_detach) |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1843 | die(_("git checkout: --detach does not take a path argument '%s'"), |
| 1844 | argv[0]); |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 1845 | } |
Junio C Hamano | 3266967 | 2011-02-08 04:32:49 -0600 | [diff] [blame] | 1846 | |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 1847 | if (opts->pathspec_from_file) { |
| 1848 | if (opts->pathspec.nr) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 1849 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 1850 | |
| 1851 | if (opts->force_detach) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 1852 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--detach"); |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 1853 | |
| 1854 | if (opts->patch_mode) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 1855 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch"); |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 1856 | |
| 1857 | parse_pathspec_file(&opts->pathspec, 0, |
| 1858 | 0, |
| 1859 | prefix, opts->pathspec_from_file, opts->pathspec_file_nul); |
| 1860 | } else if (opts->pathspec_file_nul) { |
Jean-Noël Avila | 6fa00ee | 2022-01-05 20:02:19 +0000 | [diff] [blame] | 1861 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
René Scharfe | bfda204 | 2020-08-22 10:57:59 +0200 | [diff] [blame] | 1864 | opts->pathspec.recursive = 1; |
| 1865 | |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 1866 | if (opts->pathspec.nr) { |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1867 | if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) |
Nguyễn Thái Ngọc Duy | b6312c2 | 2012-08-30 19:45:50 +0700 | [diff] [blame] | 1868 | die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" |
| 1869 | "checking out of the index.")); |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 1870 | } else { |
| 1871 | if (opts->accept_pathspec && !opts->empty_pathspec_ok && |
| 1872 | !opts->patch_mode) /* patch mode is special */ |
| 1873 | die(_("you must specify path(s) to restore")); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1874 | } |
| 1875 | |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1876 | if (opts->new_branch) { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1877 | struct strbuf buf = STRBUF_INIT; |
Conrad Irwin | 55c4a67 | 2011-08-20 14:49:48 -0700 | [diff] [blame] | 1878 | |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1879 | if (opts->new_branch_force) |
| 1880 | opts->branch_exists = validate_branchname(opts->new_branch, &buf); |
Junio C Hamano | bc1c9c0 | 2017-10-13 13:45:40 +0900 | [diff] [blame] | 1881 | else |
Nguyễn Thái Ngọc Duy | b3edccb | 2019-03-29 17:39:02 +0700 | [diff] [blame] | 1882 | opts->branch_exists = |
| 1883 | validate_new_branchname(opts->new_branch, &buf, 0); |
Daniel Barkalow | 352eadc | 2008-09-21 14:36:06 -0400 | [diff] [blame] | 1884 | strbuf_release(&buf); |
| 1885 | } |
| 1886 | |
Alexandr Miloslavskiy | fa74180 | 2019-12-30 18:38:13 +0000 | [diff] [blame] | 1887 | if (opts->patch_mode || opts->pathspec.nr) |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1888 | return checkout_paths(opts, new_branch_info); |
Alexandr Miloslavskiy | fa74180 | 2019-12-30 18:38:13 +0000 | [diff] [blame] | 1889 | else |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1890 | return checkout_branch(opts, new_branch_info); |
Daniel Barkalow | 782c2d6 | 2008-02-07 11:40:23 -0500 | [diff] [blame] | 1891 | } |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1892 | |
| 1893 | int cmd_checkout(int argc, const char **argv, const char *prefix) |
| 1894 | { |
| 1895 | struct checkout_opts opts; |
Nguyễn Thái Ngọc Duy | b7b5fce | 2019-03-29 17:39:06 +0700 | [diff] [blame] | 1896 | struct option *options; |
| 1897 | struct option checkout_options[] = { |
| 1898 | OPT_STRING('b', NULL, &opts.new_branch, N_("branch"), |
| 1899 | N_("create and checkout a new branch")), |
| 1900 | OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"), |
| 1901 | N_("create/reset and checkout a branch")), |
Nguyễn Thái Ngọc Duy | 492eded | 2019-03-29 17:39:08 +0700 | [diff] [blame] | 1902 | OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")), |
Nguyễn Thái Ngọc Duy | ccb111b | 2019-03-29 17:39:13 +0700 | [diff] [blame] | 1903 | OPT_BOOL(0, "guess", &opts.dwim_new_local_branch, |
| 1904 | N_("second guess 'git checkout <no-such-branch>' (default)")), |
Nguyễn Thái Ngọc Duy | a6cfb9b | 2019-04-25 16:45:48 +0700 | [diff] [blame] | 1905 | OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")), |
Nguyễn Thái Ngọc Duy | b7b5fce | 2019-03-29 17:39:06 +0700 | [diff] [blame] | 1906 | OPT_END() |
| 1907 | }; |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1908 | int ret; |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1909 | struct branch_info new_branch_info = { 0 }; |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1910 | |
| 1911 | memset(&opts, 0, sizeof(opts)); |
Nguyễn Thái Ngọc Duy | ccb111b | 2019-03-29 17:39:13 +0700 | [diff] [blame] | 1912 | opts.dwim_new_local_branch = 1; |
Nguyễn Thái Ngọc Duy | e342c63 | 2019-03-29 17:39:10 +0700 | [diff] [blame] | 1913 | opts.switch_branch_doing_nothing_is_ok = 1; |
Nguyễn Thái Ngọc Duy | 65f099b | 2019-03-29 17:39:14 +0700 | [diff] [blame] | 1914 | opts.only_merge_on_switching_branches = 0; |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 1915 | opts.accept_ref = 1; |
Nguyễn Thái Ngọc Duy | 5c06e26 | 2019-03-29 17:39:09 +0700 | [diff] [blame] | 1916 | opts.accept_pathspec = 1; |
Nguyễn Thái Ngọc Duy | 7968bef | 2019-03-29 17:39:11 +0700 | [diff] [blame] | 1917 | opts.implicit_detach = 1; |
Nguyễn Thái Ngọc Duy | c45f0f5 | 2019-03-29 17:39:15 +0700 | [diff] [blame] | 1918 | opts.can_switch_when_in_progress = 1; |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 1919 | opts.orphan_from_empty_tree = 0; |
Nguyễn Thái Ngọc Duy | be8ed50 | 2019-04-25 16:45:47 +0700 | [diff] [blame] | 1920 | opts.empty_pathspec_ok = 1; |
Nguyễn Thái Ngọc Duy | a6cfb9b | 2019-04-25 16:45:48 +0700 | [diff] [blame] | 1921 | opts.overlay_mode = -1; |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 1922 | opts.checkout_index = -2; /* default on */ |
| 1923 | opts.checkout_worktree = -2; /* default on */ |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1924 | |
Derrick Stolee | 3136776 | 2019-08-29 10:01:32 -0700 | [diff] [blame] | 1925 | if (argc == 3 && !strcmp(argv[1], "-b")) { |
| 1926 | /* |
| 1927 | * User ran 'git checkout -b <branch>' and expects |
| 1928 | * the same behavior as 'git switch -c <branch>'. |
| 1929 | */ |
| 1930 | opts.switch_branch_doing_nothing_is_ok = 0; |
| 1931 | opts.only_merge_on_switching_branches = 1; |
| 1932 | } |
| 1933 | |
Nguyễn Thái Ngọc Duy | b7b5fce | 2019-03-29 17:39:06 +0700 | [diff] [blame] | 1934 | options = parse_options_dup(checkout_options); |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1935 | options = add_common_options(&opts, options); |
Nguyễn Thái Ngọc Duy | b7b5fce | 2019-03-29 17:39:06 +0700 | [diff] [blame] | 1936 | options = add_common_switch_branch_options(&opts, options); |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1937 | options = add_checkout_path_options(&opts, options); |
| 1938 | |
| 1939 | ret = checkout_main(argc, argv, prefix, &opts, |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1940 | options, checkout_usage, &new_branch_info); |
| 1941 | branch_info_release(&new_branch_info); |
| 1942 | clear_pathspec(&opts.pathspec); |
Jeff King | 7ce4088 | 2023-03-04 05:31:22 -0500 | [diff] [blame] | 1943 | free(opts.pathspec_from_file); |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1944 | FREE_AND_NULL(options); |
| 1945 | return ret; |
| 1946 | } |
| 1947 | |
| 1948 | int cmd_switch(int argc, const char **argv, const char *prefix) |
| 1949 | { |
| 1950 | struct checkout_opts opts; |
| 1951 | struct option *options = NULL; |
Nguyễn Thái Ngọc Duy | b7b5fce | 2019-03-29 17:39:06 +0700 | [diff] [blame] | 1952 | struct option switch_options[] = { |
| 1953 | OPT_STRING('c', "create", &opts.new_branch, N_("branch"), |
| 1954 | N_("create and switch to a new branch")), |
| 1955 | OPT_STRING('C', "force-create", &opts.new_branch_force, N_("branch"), |
| 1956 | N_("create/reset and switch to a branch")), |
Nguyễn Thái Ngọc Duy | ccb111b | 2019-03-29 17:39:13 +0700 | [diff] [blame] | 1957 | OPT_BOOL(0, "guess", &opts.dwim_new_local_branch, |
| 1958 | N_("second guess 'git switch <no-such-branch>'")), |
Nguyễn Thái Ngọc Duy | 3ec37ad | 2019-03-29 17:39:07 +0700 | [diff] [blame] | 1959 | OPT_BOOL(0, "discard-changes", &opts.discard_changes, |
| 1960 | N_("throw away local modifications")), |
Nguyễn Thái Ngọc Duy | b7b5fce | 2019-03-29 17:39:06 +0700 | [diff] [blame] | 1961 | OPT_END() |
| 1962 | }; |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1963 | int ret; |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1964 | struct branch_info new_branch_info = { 0 }; |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1965 | |
| 1966 | memset(&opts, 0, sizeof(opts)); |
Nguyễn Thái Ngọc Duy | ccb111b | 2019-03-29 17:39:13 +0700 | [diff] [blame] | 1967 | opts.dwim_new_local_branch = 1; |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 1968 | opts.accept_ref = 1; |
Nguyễn Thái Ngọc Duy | 5c06e26 | 2019-03-29 17:39:09 +0700 | [diff] [blame] | 1969 | opts.accept_pathspec = 0; |
Nguyễn Thái Ngọc Duy | e342c63 | 2019-03-29 17:39:10 +0700 | [diff] [blame] | 1970 | opts.switch_branch_doing_nothing_is_ok = 0; |
Nguyễn Thái Ngọc Duy | 65f099b | 2019-03-29 17:39:14 +0700 | [diff] [blame] | 1971 | opts.only_merge_on_switching_branches = 1; |
Nguyễn Thái Ngọc Duy | 7968bef | 2019-03-29 17:39:11 +0700 | [diff] [blame] | 1972 | opts.implicit_detach = 0; |
Nguyễn Thái Ngọc Duy | c45f0f5 | 2019-03-29 17:39:15 +0700 | [diff] [blame] | 1973 | opts.can_switch_when_in_progress = 0; |
Nguyễn Thái Ngọc Duy | 1806c29 | 2019-03-29 17:39:16 +0700 | [diff] [blame] | 1974 | opts.orphan_from_empty_tree = 1; |
Nguyễn Thái Ngọc Duy | a6cfb9b | 2019-04-25 16:45:48 +0700 | [diff] [blame] | 1975 | opts.overlay_mode = -1; |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1976 | |
Nguyễn Thái Ngọc Duy | b7b5fce | 2019-03-29 17:39:06 +0700 | [diff] [blame] | 1977 | options = parse_options_dup(switch_options); |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1978 | options = add_common_options(&opts, options); |
Nguyễn Thái Ngọc Duy | b7b5fce | 2019-03-29 17:39:06 +0700 | [diff] [blame] | 1979 | options = add_common_switch_branch_options(&opts, options); |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1980 | |
Denton Liu | 7c16ef7 | 2020-04-30 07:54:57 -0400 | [diff] [blame] | 1981 | cb_option = 'c'; |
| 1982 | |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1983 | ret = checkout_main(argc, argv, prefix, &opts, |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 1984 | options, switch_branch_usage, &new_branch_info); |
| 1985 | branch_info_release(&new_branch_info); |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 1986 | FREE_AND_NULL(options); |
| 1987 | return ret; |
| 1988 | } |
Nguyễn Thái Ngọc Duy | 46e91b6 | 2019-04-25 16:45:45 +0700 | [diff] [blame] | 1989 | |
| 1990 | int cmd_restore(int argc, const char **argv, const char *prefix) |
| 1991 | { |
| 1992 | struct checkout_opts opts; |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 1993 | struct option *options; |
| 1994 | struct option restore_options[] = { |
| 1995 | OPT_STRING('s', "source", &opts.from_treeish, "<tree-ish>", |
Jean-Noël Avila | 182f59d | 2019-08-06 19:19:52 +0200 | [diff] [blame] | 1996 | N_("which tree-ish to checkout from")), |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 1997 | OPT_BOOL('S', "staged", &opts.checkout_index, |
| 1998 | N_("restore the index")), |
| 1999 | OPT_BOOL('W', "worktree", &opts.checkout_worktree, |
| 2000 | N_("restore the working tree (default)")), |
Nguyễn Thái Ngọc Duy | a5e5f39 | 2019-04-25 16:45:53 +0700 | [diff] [blame] | 2001 | OPT_BOOL(0, "ignore-unmerged", &opts.ignore_unmerged, |
| 2002 | N_("ignore unmerged entries")), |
Nguyễn Thái Ngọc Duy | a6cfb9b | 2019-04-25 16:45:48 +0700 | [diff] [blame] | 2003 | OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode")), |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 2004 | OPT_END() |
| 2005 | }; |
Nguyễn Thái Ngọc Duy | 46e91b6 | 2019-04-25 16:45:45 +0700 | [diff] [blame] | 2006 | int ret; |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 2007 | struct branch_info new_branch_info = { 0 }; |
Nguyễn Thái Ngọc Duy | 46e91b6 | 2019-04-25 16:45:45 +0700 | [diff] [blame] | 2008 | |
| 2009 | memset(&opts, 0, sizeof(opts)); |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 2010 | opts.accept_ref = 0; |
Nguyễn Thái Ngọc Duy | 46e91b6 | 2019-04-25 16:45:45 +0700 | [diff] [blame] | 2011 | opts.accept_pathspec = 1; |
Nguyễn Thái Ngọc Duy | be8ed50 | 2019-04-25 16:45:47 +0700 | [diff] [blame] | 2012 | opts.empty_pathspec_ok = 0; |
Nguyễn Thái Ngọc Duy | a6cfb9b | 2019-04-25 16:45:48 +0700 | [diff] [blame] | 2013 | opts.overlay_mode = 0; |
Nguyễn Thái Ngọc Duy | 183fb44 | 2019-04-25 16:45:50 +0700 | [diff] [blame] | 2014 | opts.checkout_index = -1; /* default off */ |
| 2015 | opts.checkout_worktree = -2; /* default on */ |
Nguyễn Thái Ngọc Duy | a5e5f39 | 2019-04-25 16:45:53 +0700 | [diff] [blame] | 2016 | opts.ignore_unmerged_opt = "--ignore-unmerged"; |
Nguyễn Thái Ngọc Duy | 46e91b6 | 2019-04-25 16:45:45 +0700 | [diff] [blame] | 2017 | |
Nguyễn Thái Ngọc Duy | c9c935f | 2019-04-25 16:45:46 +0700 | [diff] [blame] | 2018 | options = parse_options_dup(restore_options); |
Nguyễn Thái Ngọc Duy | 46e91b6 | 2019-04-25 16:45:45 +0700 | [diff] [blame] | 2019 | options = add_common_options(&opts, options); |
| 2020 | options = add_checkout_path_options(&opts, options); |
| 2021 | |
| 2022 | ret = checkout_main(argc, argv, prefix, &opts, |
Ævar Arnfjörð Bjarmason | 9081a42 | 2021-11-16 19:27:38 +0100 | [diff] [blame] | 2023 | options, restore_usage, &new_branch_info); |
| 2024 | branch_info_release(&new_branch_info); |
Nguyễn Thái Ngọc Duy | 46e91b6 | 2019-04-25 16:45:45 +0700 | [diff] [blame] | 2025 | FREE_AND_NULL(options); |
| 2026 | return ret; |
| 2027 | } |