@matheusbsbs / matheus-bagliano / commits / 620a6cd42e
Fork 0

builtin-reset: avoid forking "update-index --refresh"

Instead of forking update-index, call refresh_cache() directly. 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 15:21 UTC 620a6cd42ed5ea94684b22714181bf03871733dd
2 files changed +39 -24
builtin-reset.c
+29 -24
@@ -95,26 +95,34 @@ static void print_new_head_line(struct commit *commit)
95 printf("\n");
96 }
97
98 -static int update_index_refresh(void)
98 +static int update_index_refresh(int fd, struct lock_file *index_lock)
99 {
100 - const char *argv_update_index[] = {"update-index", "--refresh", NULL};
101 - return run_command_v_opt(argv_update_index, RUN_GIT_CMD);
102 -}
100 + int result;
101 +
102 + if (!index_lock) {
103 + index_lock = xcalloc(1, sizeof(struct lock_file));
104 + fd = hold_locked_index(index_lock, 1);
105 + }
106
104 -struct update_cb_data {
105 - int index_fd;
106 - struct lock_file *lock;
107 - int exit_code;
108 -};
107 + if (read_cache() < 0)
108 + return error("Could not read index");
109 + result = refresh_cache(0) ? 1 : 0;
110 + if (write_cache(fd, active_cache, active_nr) ||
111 + close(fd) ||
112 + commit_locked_index(index_lock))
113 + return error ("Could not refresh index");
114 + return result;
115 +}
116
117 static void update_index_from_diff(struct diff_queue_struct *q,
118 struct diff_options *opt, void *data)
119 {
120 int i;
114 - struct update_cb_data *cb = data;
121 + int *discard_flag = data;
122
123 /* do_diff_cache() mangled the index */
124 discard_cache();
125 + *discard_flag = 1;
126 read_cache();
127
128 for (i = 0; i < q->nr; i++) {
@@ -128,34 +136,33 @@ static void update_index_from_diff(struct diff_queue_struct *q,
136 } else
137 remove_file_from_cache(one->path);
138 }
131 -
132 - cb->exit_code = write_cache(cb->index_fd, active_cache, active_nr) ||
133 - close(cb->index_fd) ||
134 - commit_locked_index(cb->lock);
139 }
140
141 static int read_from_tree(const char *prefix, const char **argv,
142 unsigned char *tree_sha1)
143 {
144 + struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
145 + int index_fd, index_was_discarded = 0;
146 struct diff_options opt;
141 - struct update_cb_data cb;
147
148 memset(&opt, 0, sizeof(opt));
149 diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
150 opt.output_format = DIFF_FORMAT_CALLBACK;
151 opt.format_callback = update_index_from_diff;
147 - opt.format_callback_data = &cb;
152 + opt.format_callback_data = &index_was_discarded;
153
149 - cb.lock = xcalloc(1, sizeof(struct lock_file));
150 - cb.index_fd = hold_locked_index(cb.lock, 1);
151 - cb.exit_code = 0;
154 + index_fd = hold_locked_index(lock, 1);
155 + index_was_discarded = 0;
156 read_cache();
157 if (do_diff_cache(tree_sha1, &opt))
158 return 1;
159 diffcore_std(&opt);
160 diff_flush(&opt);
161
158 - return cb.exit_code;
162 + if (!index_was_discarded)
163 + /* The index is still clobbered from do_diff_cache() */
164 + discard_cache();
165 + return update_index_refresh(index_fd, lock);
166 }
167
168 static void prepend_reflog_action(const char *action, char *buf, size_t size)
@@ -225,9 +232,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
232 else if (reset_type != NONE)
233 die("Cannot do %s reset with paths.",
234 reset_type_names[reset_type]);
228 - if (read_from_tree(prefix, argv + i, sha1))
229 - return 1;
230 - return update_index_refresh() ? 1 : 0;
235 + return read_from_tree(prefix, argv + i, sha1);
236 }
237 if (reset_type == NONE)
238 reset_type = MIXED; /* by default */
@@ -264,7 +269,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
269 case SOFT: /* Nothing else to do. */
270 break;
271 case MIXED: /* Report what has not been updated. */
267 - update_index_refresh();
272 + update_index_refresh(0, NULL);
273 break;
274 }
275
t/t7102-reset.sh
+10
@@ -418,4 +418,14 @@ test_expect_success 'resetting an unmodified path is a no-op' '
418 git diff-index --cached --exit-code HEAD
419 '
420
421 +cat > expect << EOF
422 +file2: needs update
423 +EOF
424 +
425 +test_expect_success '--mixed refreshes the index' '
426 + echo 123 >> file2 &&
427 + git reset --mixed HEAD > output &&
428 + git diff --exit-code expect output
429 +'
430 +
431 test_done