From: Clemens Buchacher <drizzd@aon.at>
To: git@vger.kernel.org
Cc: Gert Palok <gert@planc.ee>, Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v2] do not overwrite untracked during merge from unborn branch
Date: Sun, 14 Nov 2010 22:53:15 +0100 [thread overview]
Message-ID: <20101114215315.GB29579@localhost> (raw)
In-Reply-To: <20101114214927.GA29579@localhost>
In case HEAD does not point to a valid commit yet, merge is
implemented as a hard reset. This will cause untracked files to be
overwritten.
Instead, assume the empty tree for HEAD and do a regular merge. An
untracked file will cause the merge to abort and do nothing. If no
conflicting files are present, the merge will have the same effect
as a hard reset.
Signed-off-by: Clemens Buchacher <drizzd@aon.at>
---
On Sun, Nov 14, 2010 at 10:49:27PM +0100, Clemens Buchacher wrote:
> On Sun, Nov 14, 2010 at 10:46:02PM +0100, Clemens Buchacher wrote:
> > --MfFXiAuoTsnnDAfZ
> > Content-Type: text/plain; charset=us-ascii
> > Content-Disposition: inline
> > Content-Transfer-Encoding: quoted-printable
> >
> > This is based on next and does not apply cleanly to master because
> > of t7607. I can rebase it to master, but then it will conflict with
> > next later.
>
> Without signature this time. Oops.
Wrong again! (With args[6] instead of args[7].) I wonder how many
times I can get it wrong.
Clemens
builtin/merge.c | 20 +++++++++++++++++++-
t/t7607-merge-overwrite.sh | 16 ++++++++++++++++
2 files changed, 35 insertions(+), 1 deletions(-)
diff --git a/builtin/merge.c b/builtin/merge.c
index 10f091b..613543e 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -234,6 +234,24 @@ static void save_state(void)
die("not a valid object: %s", buffer.buf);
}
+static void read_empty(unsigned const char *sha1, int verbose)
+{
+ int i = 0;
+ const char *args[7];
+
+ args[i++] = "read-tree";
+ if (verbose)
+ args[i++] = "-v";
+ args[i++] = "-m";
+ args[i++] = "-u";
+ args[i++] = EMPTY_TREE_SHA1_HEX;
+ args[i++] = sha1_to_hex(sha1);
+ args[i] = NULL;
+
+ if (run_command_v_opt(args, RUN_GIT_CMD))
+ die("read-tree failed");
+}
+
static void reset_hard(unsigned const char *sha1, int verbose)
{
int i = 0;
@@ -985,7 +1003,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
die("%s - not something we can merge", argv[0]);
update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0,
DIE_ON_ERR);
- reset_hard(remote_head->sha1, 0);
+ read_empty(remote_head->sha1, 0);
return 0;
} else {
struct strbuf merge_names = STRBUF_INIT;
diff --git a/t/t7607-merge-overwrite.sh b/t/t7607-merge-overwrite.sh
index e49dd80..d4a499d 100755
--- a/t/t7607-merge-overwrite.sh
+++ b/t/t7607-merge-overwrite.sh
@@ -127,4 +127,20 @@ test_expect_success SYMLINKS 'will not be confused by symlink in leading path' '
git checkout sub
'
+cat >expect <<\EOF
+error: Untracked working tree file 'c0.c' would be overwritten by merge.
+fatal: read-tree failed
+EOF
+
+test_expect_success 'will not overwrite untracked file on unborn branch' '
+ git reset --hard c0 &&
+ git rm -fr . &&
+ git checkout --orphan new &&
+ cp important c0.c &&
+ test_must_fail git merge c0 2>out &&
+ test_cmp out expect &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_cmp important c0.c
+'
+
test_done
--
1.7.3.1.105.g84915
next prev parent reply other threads:[~2010-11-14 21:53 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-10-21 13:18 pulling the root commit overwrites untracked files without warning (1.7.2.3) Gert Palok
2010-10-22 21:14 ` Clemens Buchacher
2010-10-22 21:17 ` Clemens Buchacher
2010-11-14 21:34 ` Clemens Buchacher
2010-11-14 21:46 ` [PATCH] do not overwrite untracked during merge from unborn branch Clemens Buchacher
2010-11-14 21:49 ` [PATCH w/o PGP] " Clemens Buchacher
2010-11-14 21:53 ` Clemens Buchacher [this message]
2010-11-15 1:51 ` [PATCH v2] " Miles Bader
[not found] ` <20101114214953.GB16413@burratino>
2010-11-14 22:07 ` [PATCH v3] " Clemens Buchacher
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20101114215315.GB29579@localhost \
--to=drizzd@aon.at \
--cc=gert@planc.ee \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).