Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/worktree.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / worktree.c` 1541 lines ยท 44631 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "builtin.h" 5 #include "abspath.h" 6 #include "advice.h" 7 #include "checkout.h" 8 #include "config.h" 9 #include "copy.h" 10 #include "dir.h" 11 #include "environment.h" 12 #include "gettext.h" 13 #include "hex.h" 14 #include "object-file.h" 15 #include "object-name.h" 16 #include "parse-options.h" 17 #include "path.h" 18 #include "strvec.h" 19 #include "branch.h" 20 #include "read-cache-ll.h" 21 #include "refs.h" 22 #include "remote.h" 23 #include "run-command.h" 24 #include "hook.h" 25 #include "sigchain.h" 26 #include "submodule.h" 27 #include "utf8.h" 28 #include "worktree.h" 29 #include "quote.h" 30 31 #define BUILTIN_WORKTREE_ADD_USAGE \ 32 N_("git worktree add [-f] [--detach] [--checkout] [--lock [--reason ]]\n" \ 33 " [--orphan] [(-b | -B) ] []") 34 35 #define BUILTIN_WORKTREE_LIST_USAGE \ 36 N_("git worktree list [-v | --porcelain [-z]]") 37 #define BUILTIN_WORKTREE_LOCK_USAGE \ 38 N_("git worktree lock [--reason ] ") 39 #define BUILTIN_WORKTREE_MOVE_USAGE \ 40 N_("git worktree move ") 41 #define BUILTIN_WORKTREE_PRUNE_USAGE \ 42 N_("git worktree prune [-n] [-v] [--expire ]") 43 #define BUILTIN_WORKTREE_REMOVE_USAGE \ 44 N_("git worktree remove [-f] ") 45 #define BUILTIN_WORKTREE_REPAIR_USAGE \ 46 N_("git worktree repair [...]") 47 #define BUILTIN_WORKTREE_UNLOCK_USAGE \ 48 N_("git worktree unlock ") 49 50 #define WORKTREE_ADD_DWIM_ORPHAN_INFER_TEXT \ 51 _("No possible source branch, inferring '--orphan'") 52 53 #define WORKTREE_ADD_ORPHAN_WITH_DASH_B_HINT_TEXT \ 54 _("If you meant to create a worktree containing a new unborn branch\n" \ 55 "(branch with no commits) for this repository, you can do so\n" \ 56 "using the --orphan flag:\n" \ 57 "\n" \ 58 " git worktree add --orphan -b %s %s\n") 59 60 #define WORKTREE_ADD_ORPHAN_NO_DASH_B_HINT_TEXT \ 61 _("If you meant to create a worktree containing a new unborn branch\n" \ 62 "(branch with no commits) for this repository, you can do so\n" \ 63 "using the --orphan flag:\n" \ 64 "\n" \ 65 " git worktree add --orphan %s\n") 66 67 static const char * const git_worktree_usage[] = { 68 BUILTIN_WORKTREE_ADD_USAGE, 69 BUILTIN_WORKTREE_LIST_USAGE, 70 BUILTIN_WORKTREE_LOCK_USAGE, 71 BUILTIN_WORKTREE_MOVE_USAGE, 72 BUILTIN_WORKTREE_PRUNE_USAGE, 73 BUILTIN_WORKTREE_REMOVE_USAGE, 74 BUILTIN_WORKTREE_REPAIR_USAGE, 75 BUILTIN_WORKTREE_UNLOCK_USAGE, 76 NULL 77 }; 78 79 static const char * const git_worktree_add_usage[] = { 80 BUILTIN_WORKTREE_ADD_USAGE, 81 NULL, 82 }; 83 84 static const char * const git_worktree_list_usage[] = { 85 BUILTIN_WORKTREE_LIST_USAGE, 86 NULL 87 }; 88 89 static const char * const git_worktree_lock_usage[] = { 90 BUILTIN_WORKTREE_LOCK_USAGE, 91 NULL 92 }; 93 94 static const char * const git_worktree_move_usage[] = { 95 BUILTIN_WORKTREE_MOVE_USAGE, 96 NULL 97 }; 98 99 static const char * const git_worktree_prune_usage[] = { 100 BUILTIN_WORKTREE_PRUNE_USAGE, 101 NULL 102 }; 103 104 static const char * const git_worktree_remove_usage[] = { 105 BUILTIN_WORKTREE_REMOVE_USAGE, 106 NULL 107 }; 108 109 static const char * const git_worktree_repair_usage[] = { 110 BUILTIN_WORKTREE_REPAIR_USAGE, 111 NULL 112 }; 113 114 static const char * const git_worktree_unlock_usage[] = { 115 BUILTIN_WORKTREE_UNLOCK_USAGE, 116 NULL 117 }; 118 119 struct add_opts { 120 int force; 121 int detach; 122 int quiet; 123 int checkout; 124 int orphan; 125 int relative_paths; 126 const char *keep_locked; 127 }; 128 129 static int show_only; 130 static int verbose; 131 static int guess_remote; 132 static int use_relative_paths; 133 static timestamp_t expire; 134 135 static int git_worktree_config(const char *var, const char *value, 136 const struct config_context *ctx, void *cb) 137 { 138 if (!strcmp(var, "worktree.guessremote")) { 139 guess_remote = git_config_bool(var, value); 140 return 0; 141 } else if (!strcmp(var, "worktree.userelativepaths")) { 142 use_relative_paths = git_config_bool(var, value); 143 return 0; 144 } 145 146 return git_default_config(var, value, ctx, cb); 147 } 148 149 static int delete_git_dir(const char *id) 150 { 151 struct strbuf sb = STRBUF_INIT; 152 int ret; 153 154 repo_common_path_append(the_repository, &sb, "worktrees/%s", id); 155 ret = remove_dir_recursively(&sb, 0); 156 if (ret < 0 && errno == ENOTDIR) 157 ret = unlink(sb.buf); 158 if (ret) 159 error_errno(_("failed to delete '%s'"), sb.buf); 160 strbuf_release(&sb); 161 return ret; 162 } 163 164 static void delete_worktrees_dir_if_empty(void) 165 { 166 char *path = repo_git_path(the_repository, "worktrees"); 167 rmdir(path); /* ignore failed removal */ 168 free(path); 169 } 170 171 static void prune_worktree(const char *id, const char *reason) 172 { 173 if (show_only || verbose) 174 fprintf_ln(stderr, _("Removing %s/%s: %s"), "worktrees", id, reason); 175 if (!show_only) 176 delete_git_dir(id); 177 } 178 179 static int prune_cmp(const void *a, const void *b) 180 { 181 const struct string_list_item *x = a; 182 const struct string_list_item *y = b; 183 int c; 184 185 if ((c = fspathcmp(x->string, y->string))) 186 return c; 187 /* 188 * paths same; prune_dupes() removes all but the first worktree entry 189 * having the same path, so sort main worktree ('util' is NULL) above 190 * linked worktrees ('util' not NULL) since main worktree can't be 191 * removed 192 */ 193 if (!x->util) 194 return -1; 195 if (!y->util) 196 return 1; 197 /* paths same; sort by .git/worktrees/ */ 198 return strcmp(x->util, y->util); 199 } 200 201 static void prune_dups(struct string_list *l) 202 { 203 int i; 204 205 QSORT(l->items, l->nr, prune_cmp); 206 for (i = 1; i < l->nr; i++) { 207 if (!fspathcmp(l->items[i].string, l->items[i - 1].string)) 208 prune_worktree(l->items[i].util, "duplicate entry"); 209 } 210 } 211 212 static void prune_worktrees(void) 213 { 214 struct strbuf reason = STRBUF_INIT; 215 struct strbuf main_path = STRBUF_INIT; 216 struct string_list kept = STRING_LIST_INIT_DUP; 217 char *path; 218 DIR *dir; 219 struct dirent *d; 220 221 path = repo_git_path(the_repository, "worktrees"); 222 dir = opendir(path); 223 free(path); 224 if (!dir) 225 return; 226 while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) { 227 char *path; 228 strbuf_reset(&reason); 229 if (should_prune_worktree(the_repository, d->d_name, &reason, &path, expire)) 230 prune_worktree(d->d_name, reason.buf); 231 else if (path) 232 string_list_append_nodup(&kept, path)->util = xstrdup(d->d_name); 233 } 234 closedir(dir); 235 236 strbuf_add_absolute_path(&main_path, repo_get_common_dir(the_repository)); 237 /* massage main worktree absolute path to match 'gitdir' content */ 238 strbuf_strip_suffix(&main_path, "/."); 239 string_list_append_nodup(&kept, strbuf_detach(&main_path, NULL)); 240 prune_dups(&kept); 241 string_list_clear(&kept, 1); 242 243 if (!show_only) 244 delete_worktrees_dir_if_empty(); 245 strbuf_release(&reason); 246 } 247 248 static int prune(int ac, const char **av, const char *prefix, 249 struct repository *repo UNUSED) 250 { 251 struct option options[] = { 252 OPT__DRY_RUN(&show_only, N_("do not remove, show only")), 253 OPT__VERBOSE(&verbose, N_("report pruned working trees")), 254 OPT_EXPIRY_DATE(0, "expire", &expire, 255 N_("prune missing working trees older than