8000 checkout: 'autostash' for branch switching by HaraldNordgren · Pull Request #2234 · git/git · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
58 changes: 31 additions & 27 deletions Documentation/git-checkout.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,19 @@ working tree, by copying them from elsewhere, extracting a tarball, etc.
are different between the current branch and the branch to
which you are switching, the command refuses to switch
branches in order to preserve your modifications in context.
However, with this option, a three-way merge between the current
branch, your working tree contents, and the new branch
is done, and you will be on the new branch.
+
When a merge conflict happens, the index entries for conflicting
paths are left unmerged, and you need to resolve the conflicts
and mark the resolved paths with `git add` (or `git rm` if the merge
should result in deletion of the path).
With this option, the conflicting local changes are
automatically stashed before the switch and reapplied
afterwards. If the local changes do not overlap with the
differences between branches, the switch proceeds without
stashing. If reapplying the stash results in conflicts, the
entry is saved to the stash list. Resolve the conflicts
and run `git stash drop` when done, or clear the working
tree (e.g. with `git reset --hard`) before running `git stash
pop` later to re-apply your changes.
+
When checking out paths from the index, this option lets you recreate
the conflicted merge in the specified paths. This option cannot be
used when checking out paths from a tree-ish.
+
When switching branches with `--merge`, staged changes may be lost.

`--conflict=<style>`::
The same as `--merge` option above, but changes the way the
Expand Down Expand Up @@ -578,39 +577,44 @@ $ git checkout mytopic
error: You have local changes to 'frotz'; not switching branches.
------------

You can give the `-m` flag to the command, which would try a
three-way merge:
You can give the `-m` flag to the command, which would carry your local
changes to the new branch:

------------
$ git checkout -m mytopic
Auto-merging frotz
Switched to branch 'mytopic'
------------

After this three-way merge, the local modifications are _not_
After the switch, the local modifications are reapplied and are _not_
registered in your index file, so `git diff` would show you what
changes you made since the tip of the new branch.

=== 3. Merge conflict

When a merge conflict happens during switching branches with
the `-m` option, you would see something like this:
When the `--merge` (`-m`) option is in effect and the locally
modified files overlap with files that need to be updated by the
branch switch, the changes are stashed and reapplied after the
switch. If the stash application results in conflicts, they are not
resolved and the stash is saved to the stash list:

------------
$ git checkout -m mytopic
Auto-merging frotz
ERROR: Merge conflict in frotz
fatal: merge program failed
------------
Your local changes are stashed, however, applying it to carry
forward your local changes resulted in conflicts:

At this point, `git diff` shows the changes cleanly merged as in
the previous example, as well as the changes in the conflicted
files. Edit and resolve the conflict and mark it resolved with
`git add` as usual:
- You can try resolving them now. If you resolved them
successfully, discard the stash entry with "git stash drop".

- Alternatively you can "git reset --hard" if you do not want
to deal with them right now, and later "git stash pop" to
recover your local changes.
------------
$ edit frotz
$ git add frotz
------------

You can try resolving the conflicts now. Edit the conflicting files
and mark them resolved with `git add` as usual, then run `git stash
drop` to discard the stash entry. Alternatively, you can clear the
working tree with `git reset --hard` and recover your local changes
later with `git stash pop`.

CONFIGURATION
-------------
Expand Down
11 changes: 10 additions & 1 deletion Documentation/git-stash.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ git stash list [<log-options>]
git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]
git stash drop [-q | --quiet] [<stash>]
git stash pop [--index] [-q | --quiet] [<stash>]
git stash apply [--index] [-q | --quiet] [<stash>]
git stash apply [--index] [-q | --quiet] [--ours-label=<label>] [--theirs-label=<label>] [--base-label=<label>] [<stash>]
git stash branch <branchname> [<stash>]
git stash [push [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]
[-u | --include-untracked] [-a | --all] [(-m | --message) <message>]
Expand Down Expand Up @@ -197,6 +197,15 @@ the index's ones. However, this can fail, when you have conflicts
(which are stored in the index, where you therefore can no longer
apply the changes as they were originally).

`--ours-label=<label>`::
`--theirs-label=<label>`::
`--base-label=<label>`::
These options are only valid for the `apply` command.
+
Use the given labels in conflict markers instead of the default
"Updated upstream", "Stashed changes", and "Stash base".
`--base-label` only has an effect with merge.conflictStyle=diff3.

`-k`::
`--keep-index`::
`--no-keep-index`::
Expand Down
27 changes: 14 additions & 13 deletions Documentation/git-switch.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,16 @@ variable.
If you have local modifications to one or more files that are
different between the current branch and the branch to which
you are switching, the command refuses to switch branches in
order to preserve your modifications in context. However,
with this option, a three-way merge between the current
branch, your working tree contents, and the new branch is
done, and you will be on the new branch.
+
When a merge conflict happens, the index entries for conflicting
paths are left unmerged, and you need to resolve the conflicts
and mark the resolved paths with `git add` (or `git rm` if the merge
should result in deletion of the path).
order to preserve your modifications in context. With this
option, the conflicting local changes are automatically
stashed before the switch and reapplied afterwards. If the
local changes do not overlap with the differences between
branches, the switch proceeds without stashing. If
reapplying the stash results in conflicts, the entry is
saved to the stash list. Resolve the conflicts and run
`git stash drop` when done, or clear the working tree
(e.g. with `git reset --hard`) before running `git stash pop`
later to re-apply your changes.

`--conflict=<style>`::
The same as `--merge` option above, but changes the way the
Expand Down Expand Up @@ -217,15 +218,15 @@ $ git switch mytopic
error: You have local changes to 'frotz'; not switching branches.
------------

You can give the `-m` flag to the command, which would try a three-way
merge:
You can give the `-m` flag to the command, which would carry your local
changes to the new branch:

------------
$ git switch -m mytopic
Auto-merging frotz
Switched to branch 'mytopic'
------------

After this three-way merge, the local modifications are _not_
After the switch, the local modifications are reapplied and are _not_
registered in your index file, so `git diff` would show you what
changes you made since the tip of the new branch.

Expand Down
180 changes: 102 additions & 78 deletions builtin/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "merge-ll.h"
#include "lockfile.h"
#include "mem-pool.h"
#include "merge-ort-wrappers.h"
#include "object-file.h"
#include "object-name.h"
#include "odb.h"
Expand All @@ -30,6 +29,7 @@
#include "repo-settings.h"
#include "resolve-undo.h"
#include "revision.h"
#include "sequencer.h"
#include "setup.h"
#include "submodule.h"
#include "symlinks.h"
Expand Down Expand Up @@ -845,83 +845,8 @@ static int merge_working_tree(const struct checkout_opts *opts,

ret = unpack_trees(2, trees, &topts);
clear_unpack_trees_porcelain(&topts);
if (ret == -1) {
/*
* Unpack couldn't do a trivial merge; either
* give up or do a real merge, depending on
* whether the merge flag was used.
*/
struct tree *work;
struct tree *old_tree;
struct merge_options o;
struct strbuf sb = STRBUF_INIT;
struct strbuf old_commit_shortname = STRBUF_INIT;

if (!opts->merge)
return 1;

/*
* Without old_branch_info->commit, the below is the same as
* the two-tree unpack we already tried and failed.
*/
if (!old_branch_info->commit)
return 1;
old_tree = repo_get_commit_tree(the_repository,
old_branch_info->commit);

if (repo_index_has_changes(the_repository, old_tree, &sb))
die(_("cannot continue with staged changes in "
"the following files:\n%s"), sb.buf);
strbuf_release(&sb);

/* Do more real merge */

/*
* We update the index fully, then write the
* tree from the index, then merge the new
* branch with the current tree, with the old
* branch as the base. Then we reset the index
* (but not the working tree) to the new
* branch, leaving the working tree as the
* merged version, but skipping unmerged
* entries in the index.
*/

add_files_to_cache(the_repository, NULL, NULL, NULL, 0,
0, 0);
init_ui_merge_options(&o, the_repository);
o.verbosity = 0;
work = write_in_core_index_as_tree(the_repository);

ret = reset_tree(new_tree,
opts, 1,
writeout_error, new_branch_info);
if (ret)
return ret;
o.ancestor = old_branch_info->name;
if (!old_branch_info->name) {
strbuf_add_unique_abbrev(&old_commit_shortname,
&old_branch_info->commit->object.oid,
DEFAULT_ABBREV);
o.ancestor = old_commit_shortname.buf;
}
o.branch1 = new_branch_info->name;
o.branch2 = "local";
o.conflict_style = opts->conflict_style;
ret = merge_ort_nonrecursive(&o,
new_tree,
work,
old_tree);
if (ret < 0)
die(NULL);
ret = reset_tree(new_tree,
opts, 0,
writeout_error, new_branch_info);
strbuf_release(&o.obuf);
strbuf_release(&old_commit_shortname);
if (ret)
return ret;
}
if (ret == -1)
return 1;
}

if (!cache_tree_fully_valid(the_repository->index->cache_tree))
Expand Down Expand Up @@ -1157,6 +1082,55 @@ static void orphaned_commit_warning(struct commit *old_commit, struct commit *ne
release_revisions(&revs);
}

static int checkout_would_clobber_changes(struct branch_info *old_branch_info,
struct branch_info *new_branch_info)
{
struct tree_desc trees[2];
struct tree *old_tree, *new_tree;
struct unpack_trees_options topts;
struct index_state tmp_index = INDEX_STATE_INIT(the_repository);
const struct object_id *old_commit_oid;
int ret;

if (!new_branch_info->commit)
return 0;

old_commit_oid = old_branch_info->commit ?
&old_branch_info->commit->object.oid :
the_hash_algo->empty_tree;
old_tree = repo_parse_tree_indirect(the_repository, old_commit_oid);
if (!old_tree)
return 0;

new_tree = repo_get_commit_tree(the_repository,
new_branch_info->commit);
if (!new_tree)
return 0;
if (repo_parse_tree(the_repository, new_tree) < 0)
return 0;

memset(&topts, 0, sizeof(topts));
topts.head_idx = -1;
topts.src_index = the_repository->index;
topts.dst_index = &tmp_index;
topts.initial_checkout = is_index_unborn(the_repository->index);
topts.merge = 1;
topts.update = 1;
topts.dry_run = 1;
topts.quiet = 1;
topts.fn = twoway_merge;

init_tree_desc(&trees[0], &old_tree->object.oid,
old_tree->buffer, old_tree->size);
init_tree_desc(&trees[1], &new_tree->object.oid,
new_tree->buffer, new_tree->size);

ret = unpack_trees(2, trees, &topts);
discard_index(&tmp_index);

return ret != 0;
}

static int switch_branches(const struct checkout_opts *opts,
struct branch_info *new_branch_info)
{
Expand All @@ -1165,6 +1139,9 @@ static int switch_branches(const struct checkout_opts *opts,
struct object_id rev;
int flag, writeout_error = 0;
int do_merge = 1;
int created_autostash = 0;
struct strbuf old_commit_shortname = STRBUF_INIT;
const char *stash_label_ancestor = NULL;

trace2_cmd_mode("branch");

Expand Down Expand Up @@ -1202,10 +1179,36 @@ static int switch_branches(const struct checkout_opts *opts,
do_merge = 0;
}

if (old_branch_info.name)
stash_label_ancestor = old_branch_info.name;
else if (old_branch_info.commit) {
strbuf_add_unique_abbrev(&old_commit_shortname,
&old_branch_info.commit->object.oid,
DEFAULT_ABBREV);
stash_label_ancestor = old_commit_shortname.buf;
}

if (opts->merge) {
if (repo_read_index(the_repository) < 0)
die(_("index file corrupt"));
if (checkout_would_clobber_changes(&old_branch_info,
new_branch_info)) {
create_autostash_ref_silent(the_repository,
"CHECKOUT_AUTOSTASH");
created_autostash = 1;
}
}

if (do_merge) {
ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error);
if (ret) {
apply_autostash_ref_with_labels(the_repository,
"CHECKOUT_AUTOSTASH",
new_branch_info->name,
"local",
stash_label_ancestor);
branch_info_release(&old_branch_info);
strbuf_release(&old_commit_shortname);
return ret;
}
}
Expand All @@ -1215,8 +1218,29 @@ static int switch_branches(const struct checkout_opts *opts,

update_refs_for_switch(opts, &old_branch_info, new_branch_info);

if (opts->conflict_style >= 0) {
struct strbuf cfg = STRBUF_INIT;
strbuf_addf(&cfg, "merge.conflictStyle=%s",
conflict_style_name(opts->conflict_style));
git_config_push_parameter(cfg.buf);
strbuf_release(&cfg);
}
apply_autostash_ref_with_labels(the_repository, "CHECKOUT_AUTOSTASH",
new_branch_info->name, "local",
stash_label_ancestor);

discard_index(the_repository->index);
if (repo_read_index(the_repository) < 0)
die(_("index file corrupt"));

if (created_autostash && !opts->discard_changes && !opts->quiet &&
new_branch_info->commit)
show_local_changes(&new_branch_info->commit->object,
&opts->diff_options);

ret = post_checkout_hook(old_branch_info.commit, new_branch_info->commit, 1);
branch_info_release(&old_branch_info);
strbuf_release(&old_commit_shortname);

return ret || writeout_error;
}
Expand Down
Loading
Loading
0