git-reset: do not be confused if there is nothing to reset
The purpose of the function update_index_from_diff() (which is the callback function we give do_diff_cache()) is to update those index entries which differ from the given commit. Since do_diff_cache() plays games with the in-memory index, this function discarded the cache and reread it. Then, back in the function read_from_tree() we wrote the index. Of course, this broke down when there were no changes and update_index_from_diff() was not called, and therefore the mangled index was not discarded. The solution is to move the index writing into the function update_index_from_diff(). Noticed by Björn Steinbrink. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Nov 3, 2007 at 13:12 UTC
2e7a9785c27f00a8f7a06edc1d4c9b2f3fa2eeb9
2 files changed
+25
-6
builtin-reset.c
+18
-6
@@ -113,10 +113,17 @@ static int update_index_refresh(void)
113
return run_command_v_opt(argv_update_index, RUN_GIT_CMD);
114
}
115
116
+struct update_cb_data {
117
+ int index_fd;
118
+ struct lock_file *lock;
119
+ int exit_code;
120
+};
121
+
122
static void update_index_from_diff(struct diff_queue_struct *q,
123
struct diff_options *opt, void *data)
124
{
125
int i;
126
+ struct update_cb_data *cb = data;
127
128
/* do_diff_cache() mangled the index */
129
discard_cache();
@@ -133,29 +140,34 @@ static void update_index_from_diff(struct diff_queue_struct *q,
140
} else
141
remove_file_from_cache(one->path);
142
}
143
+
144
+ cb->exit_code = write_cache(cb->index_fd, active_cache, active_nr) ||
145
+ close(cb->index_fd) ||
146
+ commit_locked_index(cb->lock);
147
}
148
149
static int read_from_tree(const char *prefix, const char **argv,
150
unsigned char *tree_sha1)
151
{
141
- struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
142
- int index_fd;
152
struct diff_options opt;
153
+ struct update_cb_data cb;
154
155
memset(&opt, 0, sizeof(opt));
156
diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
157
opt.output_format = DIFF_FORMAT_CALLBACK;
158
opt.format_callback = update_index_from_diff;
159
+ opt.format_callback_data = &cb;
160
150
- index_fd = hold_locked_index(lock, 1);
161
+ cb.lock = xcalloc(1, sizeof(struct lock_file));
162
+ cb.index_fd = hold_locked_index(cb.lock, 1);
163
+ cb.exit_code = 0;
164
read_cache();
165
if (do_diff_cache(tree_sha1, &opt))
166
return 1;
167
diffcore_std(&opt);
168
diff_flush(&opt);
156
- return write_cache(index_fd, active_cache, active_nr) ||
157
- close(index_fd) ||
158
- commit_locked_index(lock);
169
+
170
+ return cb.exit_code;
171
}
172
173
static void prepend_reflog_action(const char *action, char *buf, size_t size)
t/t7102-reset.sh
+7
@@ -402,4 +402,11 @@ test_expect_success 'test resetting the index at give paths' '
402
403
'
404
405
+test_expect_success 'resetting an unmodified path is a no-op' '
406
+ git reset --hard &&
407
+ git reset -- file1 &&
408
+ git diff-files --exit-code &&
409
+ git diff-index --cached --exit-code HEAD
410
+'
411
+
412
test_done