Skip to content

Commit 13fa77b

Browse files
committed
Merge branch 'ak/protect-any-current-branch'
"git fetch" without the "--update-head-ok" option ought to protect a checked out branch from getting updated, to prevent the working tree that checks it out to go out of sync. The code was written before the use of "git worktree" got widespread, and only checked the branch that was checked out in the current worktree, which has been updated. (originally called ak/fetch-not-overwrite-any-current-branch) * ak/protect-any-current-branch: branch: protect branches checked out in all worktrees receive-pack: protect current branch for bare repository worktree receive-pack: clean dead code from update_worktree() fetch: protect branches checked out in all worktrees worktree: simplify find_shared_symref() memory ownership model branch: lowercase error messages receive-pack: lowercase error messages fetch: lowercase error messages
2 parents ee1dc49 + 593a2a5 commit 13fa77b

File tree

11 files changed

+198
-131
lines changed

11 files changed

+198
-131
lines changed

branch.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int install_branch_config(int flag, const char *local, const char *origin, const
6464
if (skip_prefix(remote, "refs/heads/", &shortname)
6565
&& !strcmp(local, shortname)
6666
&& !origin) {
67-
warning(_("Not setting branch %s as its own upstream."),
67+
warning(_("not setting branch %s as its own upstream"),
6868
local);
6969
return 0;
7070
}
@@ -116,7 +116,7 @@ int install_branch_config(int flag, const char *local, const char *origin, const
116116

117117
out_err:
118118
strbuf_release(&key);
119-
error(_("Unable to write upstream branch configuration"));
119+
error(_("unable to write upstream branch configuration"));
120120

121121
advise(_(tracking_advice),
122122
origin ? origin : "",
@@ -153,7 +153,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
153153
}
154154

155155
if (tracking.matches > 1)
156-
die(_("Not tracking: ambiguous information for ref %s"),
156+
die(_("not tracking: ambiguous information for ref %s"),
157157
orig_ref);
158158

159159
if (install_branch_config(config_flags, new_ref, tracking.remote,
@@ -186,7 +186,7 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name)
186186
int validate_branchname(const char *name, struct strbuf *ref)
187187
{
188188
if (strbuf_check_branch_ref(ref, name))
189-
die(_("'%s' is not a valid branch name."), name);
189+
die(_("'%s' is not a valid branch name"), name);
190190

191191
return ref_exists(ref->buf);
192192
}
@@ -199,18 +199,23 @@ int validate_branchname(const char *name, struct strbuf *ref)
199199
*/
200200
int validate_new_branchname(const char *name, struct strbuf *ref, int force)
201201
{
202-
const char *head;
202+
struct worktree **worktrees;
203+
const struct worktree *wt;
203204

204205
if (!validate_branchname(name, ref))
205206
return 0;
206207

207208
if (!force)
208-
die(_("A branch named '%s' already exists."),
209+
die(_("a branch named '%s' already exists"),
209210
ref->buf + strlen("refs/heads/"));
210211

211-
head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
212-
if (!is_bare_repository() && head && !strcmp(head, ref->buf))
213-
die(_("Cannot force update the current branch."));
212+
worktrees = get_worktrees();
213+
wt = find_shared_symref(worktrees, "HEAD", ref->buf);
214+
if (wt && !wt->is_bare)
215+
die(_("cannot force update the branch '%s'"
216+
"checked out at '%s'"),
217+
ref->buf + strlen("refs/heads/"), wt->path);
218+
free_worktrees(worktrees);
214219

215220
return 1;
216221
}
@@ -230,7 +235,7 @@ static int validate_remote_tracking_branch(char *ref)
230235
}
231236

232237
static const char upstream_not_branch[] =
233-
N_("Cannot setup tracking information; starting point '%s' is not a branch.");
238+
N_("cannot set up tracking information; starting point '%s' is not a branch");
234239
static const char upstream_missing[] =
235240
N_("the requested upstream branch '%s' does not exist");
236241
static const char upstream_advice[] =
@@ -278,7 +283,7 @@ void create_branch(struct repository *r,
278283
}
279284
die(_(upstream_missing), start_name);
280285
}
281-
die(_("Not a valid object name: '%s'."), start_name);
286+
die(_("not a valid object name: '%s'"), start_name);
282287
}
283288

284289
switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
@@ -298,12 +303,12 @@ void create_branch(struct repository *r,
298303
}
299304
break;
300305
default:
301-
die(_("Ambiguous object name: '%s'."), start_name);
306+
die(_("ambiguous object name: '%s'"), start_name);
302307
break;
303308
}
304309

305310
if ((commit = lookup_commit_reference(r, &oid)) == NULL)
306-
die(_("Not a valid branch point: '%s'."), start_name);
311+
die(_("not a valid branch point: '%s'"), start_name);
307312
oidcpy(&oid, &commit->object.oid);
308313

309314
if (reflog)
@@ -357,14 +362,16 @@ void remove_branch_state(struct repository *r, int verbose)
357362

358363
void die_if_checked_out(const char *branch, int ignore_current_worktree)
359364
{
365+
struct worktree **worktrees = get_worktrees();
360366
const struct worktree *wt;
361367

362-
wt = find_shared_symref("HEAD", branch);
363-
if (!wt || (ignore_current_worktree && wt->is_current))
364-
return;
365-
skip_prefix(branch, "refs/heads/", &branch);
366-
die(_("'%s' is already checked out at '%s'"),
367-
branch, wt->path);
368+
wt = find_shared_symref(worktrees, "HEAD", branch);
369+
if (wt && (!ignore_current_worktree || !wt->is_current)) {
370+
skip_prefix(branch, "refs/heads/", &branch);
371+
die(_("'%s' is already checked out at '%s'"), branch, wt->path);
372+
}
373+
374+
free_worktrees(worktrees);
368375
}
369376

370377
int replace_each_worktree_head_symref(const char *oldref, const char *newref,

builtin/branch.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ static void delete_branch_config(const char *branchname)
192192
static int delete_branches(int argc, const char **argv, int force, int kinds,
193193
int quiet)
194194
{
195+
struct worktree **worktrees;
195196
struct commit *head_rev = NULL;
196197
struct object_id oid;
197198
char *name = NULL;
@@ -228,6 +229,9 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
228229
if (!head_rev)
229230
die(_("Couldn't look up commit object for HEAD"));
230231
}
232+
233+
worktrees = get_worktrees();
234+
231235
for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
232236
char *target = NULL;
233237
int flags = 0;
@@ -238,7 +242,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
238242

239243
if (kinds == FILTER_REFS_BRANCHES) {
240244
const struct worktree *wt =
241-
find_shared_symref("HEAD", name);
245+
find_shared_symref(worktrees, "HEAD", name);
242246
if (wt) {
243247
error(_("Cannot delete branch '%s' "
244248
"checked out at '%s'"),
@@ -299,6 +303,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
299303

300304
free(name);
301305
strbuf_release(&bname);
306+
free_worktrees(worktrees);
302307

303308
return ret;
304309
}

0 commit comments

Comments
 (0)