Re: [RFC/PATCH] Add multiple workdir support to branch/checkout
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Oct 5, 2011, 04:07 UTC
- Message-ID
- <7vmxdg9j3r.fsf@alter.siamese.dyndns.org>
- In-Reply-To
- <1317786204-57335-1-git-send-email-jaysoffian@gmail.com>
Jay Soffian <jaysoffian@gmail.com> writes:
Show 46 quoted lines
> diff --git a/builtin/checkout.c b/builtin/checkout.c
> index 5e356a6c61..26259a41a7 100644
> --- a/builtin/checkout.c
> +++ b/builtin/checkout.c
> @@ -709,12 +710,35 @@ static void orphaned_commit_warning(struct commit *commit)
> for_each_ref(clear_commit_marks_from_one_ref, NULL);
> }
>
> +static void record_checkout(const char *name, const char *work_tree)
> +{
> + struct strbuf key = STRBUF_INIT;
> + strbuf_addf(&key, "branch.%s.checkout", name);
> + git_config_set(key.buf, work_tree);
> + strbuf_release(&key);
> +}
> +
> +static void check_if_checked_out(struct checkout_opts *opts, const char *name)
> +{
> + struct branch *branch = branch_get(name);
> + if (branch->work_tree && strlen(branch->work_tree) &&
> + strcmp(branch->work_tree, get_git_work_tree())) {
> + if (opts->force)
> + warning(_("branch '%s' is currently checked out"
> + " in '%s'"), name, branch->work_tree);
> + else
> + die(_("branch '%s' is currently checked out"
> + " in '%s'"), name, branch->work_tree);
> + }
> +}
> +
> static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
> {
> int ret = 0;
> struct branch_info old;
> unsigned char rev[20];
> int flag;
> +
> memset(&old, 0, sizeof(old));
> old.path = xstrdup(resolve_ref("HEAD", rev, 0, &flag));
> old.commit = lookup_commit_reference_gently(rev, 1);
> @@ -734,6 +758,9 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
> parse_commit(new->commit);
> }
>
> + if (opts->record_checkouts)
> + check_if_checked_out(opts, new->name);The close brace we can see in the context closes "if (!new->name) {", so this codepath is very well prepared to be called with new->name == NULL.
Is check_if_checked_out() prepared to be called with name == NULL and do the right thing?
Show 12 quoted lines
> @@ -743,6 +770,14 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
>
> update_refs_for_switch(opts, &old, new);
>
> + if (opts->record_checkouts) {
> + const char *work_tree = get_git_work_tree();
> + struct branch *branch = branch_get(old.name);
> + if (branch->work_tree && !strcmp(branch->work_tree, work_tree))
> + record_checkout(old.name, "");
> + record_checkout(new->name, work_tree);
> + }
> +Likewise for new->name, but also old.name which is only set when old.path is set and begins with "refs/heads/" and otherwise NULL.