Skip to content

Commit

Permalink
use persistent memory for rejected paths
Browse files Browse the repository at this point in the history
An aborted merge prints the list of rejected paths as part of the
error message. Since commit f66caaf (do not overwrite files in
leading path), some of those paths do not have static buffers, so
we have to keep a copy. Use string_list's to accomplish this.

This changes the order of the list to the order in which the paths
are processed. Previously, it was reversed.

Signed-off-by: Clemens Buchacher <drizzd@aon.at>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Clemens Buchacher authored and gitster committed Dec 14, 2010
1 parent b1735b1 commit 3055d78
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 42 deletions.
19 changes: 16 additions & 3 deletions t/t7607-merge-overwrite.sh
Expand Up @@ -15,7 +15,9 @@ test_expect_success 'setup' '
git reset --hard c0 &&
mkdir sub &&
echo "sub/f" > sub/f &&
git add sub/f &&
mkdir sub2 &&
echo "sub2/f" > sub2/f &&
git add sub/f sub2/f &&
git commit -m sub &&
git tag sub &&
echo "VERY IMPORTANT CHANGES" > important
Expand Down Expand Up @@ -100,13 +102,24 @@ test_expect_success 'will not overwrite untracked subtree' '
test_cmp important sub/f/important
'

cat >expect <<\EOF
error: The following untracked working tree files would be overwritten by merge:
sub
sub2
Please move or remove them before you can merge.
EOF

test_expect_success 'will not overwrite untracked file in leading path' '
git reset --hard c0 &&
rm -rf sub &&
cp important sub &&
test_must_fail git merge sub &&
cp important sub2 &&
test_must_fail git merge sub 2>out &&
test_cmp out expect &&
test_path_is_missing .git/MERGE_HEAD &&
test_cmp important sub
test_cmp important sub &&
test_cmp important sub2 &&
rm -f sub sub2
'

test_expect_failure SYMLINKS 'will not overwrite untracked symlink in leading path' '
Expand Down
16 changes: 8 additions & 8 deletions t/t7609-merge-co-error-msgs.sh
Expand Up @@ -27,10 +27,10 @@ test_expect_success 'setup' '

cat >expect <<\EOF
error: The following untracked working tree files would be overwritten by merge:
two
three
four
five
four
three
two
Please move or remove them before you can merge.
EOF

Expand All @@ -49,9 +49,9 @@ test_expect_success 'untracked files overwritten by merge (fast and non-fast for

cat >expect <<\EOF
error: Your local changes to the following files would be overwritten by merge:
two
three
four
three
two
Please, commit your changes or stash them before you can merge.
error: The following untracked working tree files would be overwritten by merge:
five
Expand All @@ -68,8 +68,8 @@ test_expect_success 'untracked files or local changes ovewritten by merge' '

cat >expect <<\EOF
error: Your local changes to the following files would be overwritten by checkout:
rep/two
rep/one
rep/two
Please, commit your changes or stash them before you can switch branches.
EOF

Expand All @@ -89,8 +89,8 @@ test_expect_success 'cannot switch branches because of local changes' '

cat >expect <<\EOF
error: Your local changes to the following files would be overwritten by checkout:
rep/two
rep/one
rep/two
Please, commit your changes or stash them before you can switch branches.
EOF

Expand All @@ -102,8 +102,8 @@ test_expect_success 'not uptodate file porcelain checkout error' '

cat >expect <<\EOF
error: Updating the following directories would lose untracked files in it:
rep2
rep
rep2
EOF

Expand Down
36 changes: 11 additions & 25 deletions unpack-trees.c
Expand Up @@ -53,6 +53,7 @@ const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
const char *cmd)
{
int i;
const char **msgs = opts->msgs;
const char *msg;
char *tmp;
Expand Down Expand Up @@ -96,6 +97,9 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
"The following Working tree files would be removed by sparse checkout update:\n%s";

opts->show_all_errors = 1;
/* rejected paths may not have a static buffer */
for (i = 0; i < ARRAY_SIZE(opts->unpack_rejects); i++)
opts->unpack_rejects[i].strdup_strings = 1;
}

static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
Expand Down Expand Up @@ -124,53 +128,35 @@ static int add_rejected_path(struct unpack_trees_options *o,
enum unpack_trees_error_types e,
const char *path)
{
struct rejected_paths_list *newentry;
if (!o->show_all_errors)
return error(ERRORMSG(o, e), path);

/*
* Otherwise, insert in a list for future display by
* display_error_msgs()
*/
newentry = xmalloc(sizeof(struct rejected_paths_list));
newentry->path = (char *)path;
newentry->next = o->unpack_rejects[e];
o->unpack_rejects[e] = newentry;
string_list_append(&o->unpack_rejects[e], path);
return -1;
}

/*
* free all the structures allocated for the error <e>
*/
static void free_rejected_paths(struct unpack_trees_options *o,
enum unpack_trees_error_types e)
{
while (o->unpack_rejects[e]) {
struct rejected_paths_list *del = o->unpack_rejects[e];
o->unpack_rejects[e] = o->unpack_rejects[e]->next;
free(del);
}
free(o->unpack_rejects[e]);
}

/*
* display all the error messages stored in a nice way
*/
static void display_error_msgs(struct unpack_trees_options *o)
{
int e;
int e, i;
int something_displayed = 0;
for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
if (o->unpack_rejects[e]) {
struct rejected_paths_list *rp;
struct string_list *rejects = &o->unpack_rejects[e];
if (rejects->nr > 0) {
struct strbuf path = STRBUF_INIT;
something_displayed = 1;
for (rp = o->unpack_rejects[e]; rp; rp = rp->next)
strbuf_addf(&path, "\t%s\n", rp->path);
for (i = 0; i < rejects->nr; i++)
strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
error(ERRORMSG(o, e), path.buf);
strbuf_release(&path);
free_rejected_paths(o, e);
}
string_list_clear(rejects, 0);
}
if (something_displayed)
printf("Aborting\n");
Expand Down
9 changes: 3 additions & 6 deletions unpack-trees.h
@@ -1,6 +1,8 @@
#ifndef UNPACK_TREES_H
#define UNPACK_TREES_H

#include "string-list.h"

#define MAX_UNPACK_TREES 8

struct unpack_trees_options;
Expand Down Expand Up @@ -29,11 +31,6 @@ enum unpack_trees_error_types {
void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
const char *cmd);

struct rejected_paths_list {
char *path;
struct rejected_paths_list *next;
};

struct unpack_trees_options {
unsigned int reset,
merge,
Expand All @@ -59,7 +56,7 @@ struct unpack_trees_options {
* Store error messages in an array, each case
* corresponding to a error message type
*/
struct rejected_paths_list *unpack_rejects[NB_UNPACK_TREES_ERROR_TYPES];
struct string_list unpack_rejects[NB_UNPACK_TREES_ERROR_TYPES];

int head_idx;
int merge_size;
Expand Down

0 comments on commit 3055d78

Please sign in to comment.