Skip to content

Commit 52b9b48

Browse files
committed
Merge branch 'js/reset'
* js/reset: builtin-reset: avoid forking "update-index --refresh" builtin-reset: do not call "ls-files --unmerged"
2 parents 91febfb + 620a6cd commit 52b9b48

File tree

2 files changed

+56
-44
lines changed

2 files changed

+56
-44
lines changed

builtin-reset.c

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,14 @@ static inline int is_merge(void)
4646

4747
static int unmerged_files(void)
4848
{
49-
char b;
50-
ssize_t len;
51-
struct child_process cmd;
52-
const char *argv_ls_files[] = {"ls-files", "--unmerged", NULL};
53-
54-
memset(&cmd, 0, sizeof(cmd));
55-
cmd.argv = argv_ls_files;
56-
cmd.git_cmd = 1;
57-
cmd.out = -1;
58-
59-
if (start_command(&cmd))
60-
die("Could not run sub-command: git ls-files");
61-
62-
len = xread(cmd.out, &b, 1);
63-
if (len < 0)
64-
die("Could not read output from git ls-files: %s",
65-
strerror(errno));
66-
finish_command(&cmd);
67-
68-
return len;
49+
int i;
50+
read_cache();
51+
for (i = 0; i < active_nr; i++) {
52+
struct cache_entry *ce = active_cache[i];
53+
if (ce_stage(ce))
54+
return 1;
55+
}
56+
return 0;
6957
}
7058

7159
static int reset_index_file(const unsigned char *sha1, int is_hard_reset)
@@ -107,26 +95,34 @@ static void print_new_head_line(struct commit *commit)
10795
printf("\n");
10896
}
10997

110-
static int update_index_refresh(void)
98+
static int update_index_refresh(int fd, struct lock_file *index_lock)
11199
{
112-
const char *argv_update_index[] = {"update-index", "--refresh", NULL};
113-
return run_command_v_opt(argv_update_index, RUN_GIT_CMD);
114-
}
100+
int result;
115101

116-
struct update_cb_data {
117-
int index_fd;
118-
struct lock_file *lock;
119-
int exit_code;
120-
};
102+
if (!index_lock) {
103+
index_lock = xcalloc(1, sizeof(struct lock_file));
104+
fd = hold_locked_index(index_lock, 1);
105+
}
106+
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+
}
121116

122117
static void update_index_from_diff(struct diff_queue_struct *q,
123118
struct diff_options *opt, void *data)
124119
{
125120
int i;
126-
struct update_cb_data *cb = data;
121+
int *discard_flag = data;
127122

128123
/* do_diff_cache() mangled the index */
129124
discard_cache();
125+
*discard_flag = 1;
130126
read_cache();
131127

132128
for (i = 0; i < q->nr; i++) {
@@ -140,34 +136,33 @@ static void update_index_from_diff(struct diff_queue_struct *q,
140136
} else
141137
remove_file_from_cache(one->path);
142138
}
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);
147139
}
148140

149141
static int read_from_tree(const char *prefix, const char **argv,
150142
unsigned char *tree_sha1)
151143
{
144+
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
145+
int index_fd, index_was_discarded = 0;
152146
struct diff_options opt;
153-
struct update_cb_data cb;
154147

155148
memset(&opt, 0, sizeof(opt));
156149
diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
157150
opt.output_format = DIFF_FORMAT_CALLBACK;
158151
opt.format_callback = update_index_from_diff;
159-
opt.format_callback_data = &cb;
152+
opt.format_callback_data = &index_was_discarded;
160153

161-
cb.lock = xcalloc(1, sizeof(struct lock_file));
162-
cb.index_fd = hold_locked_index(cb.lock, 1);
163-
cb.exit_code = 0;
154+
index_fd = hold_locked_index(lock, 1);
155+
index_was_discarded = 0;
164156
read_cache();
165157
if (do_diff_cache(tree_sha1, &opt))
166158
return 1;
167159
diffcore_std(&opt);
168160
diff_flush(&opt);
169161

170-
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);
171166
}
172167

173168
static void prepend_reflog_action(const char *action, char *buf, size_t size)
@@ -243,9 +238,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
243238
else if (reset_type != NONE)
244239
die("Cannot do %s reset with paths.",
245240
reset_type_names[reset_type]);
246-
if (read_from_tree(prefix, argv + i, sha1))
247-
return 1;
248-
return update_index_refresh() ? 1 : 0;
241+
return read_from_tree(prefix, argv + i, sha1);
249242
}
250243
if (reset_type == NONE)
251244
reset_type = MIXED; /* by default */
@@ -282,7 +275,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
282275
case SOFT: /* Nothing else to do. */
283276
break;
284277
case MIXED: /* Report what has not been updated. */
285-
update_index_refresh();
278+
update_index_refresh(0, NULL);
286279
break;
287280
}
288281

t/t7102-reset.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ test_expect_success 'giving a non existing revision should fail' '
5959
check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
6060
'
6161

62+
test_expect_success 'reset --soft with unmerged index should fail' '
63+
touch .git/MERGE_HEAD &&
64+
echo "100644 44c5b5884550c17758737edcced463447b91d42b 1 un" |
65+
git update-index --index-info &&
66+
! git reset --soft HEAD &&
67+
rm .git/MERGE_HEAD &&
68+
git rm --cached -- un
69+
'
70+
6271
test_expect_success \
6372
'giving paths with options different than --mixed should fail' '
6473
! git reset --soft -- first &&
@@ -409,4 +418,14 @@ test_expect_success 'resetting an unmodified path is a no-op' '
409418
git diff-index --cached --exit-code HEAD
410419
'
411420

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+
412431
test_done

0 commit comments

Comments
 (0)