reset: introduce dry-run mode
In a subsequent commit we'll add another caller to `reset_working_tree()` that wants to perform a dry-run check of whether it would be possible to update the index and working tree when moving to a new commit. Introduce a new flag that lets the caller perform this operation. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Jul 1, 2026 at 13:35 UTC
8e435d99b56e8c7be32d7f46101d18e88b9fc4ac
2 files changed
+39
-11
reset.c
+33
-11
@@ -93,11 +93,14 @@ int reset_working_tree(struct repository *r,
93
unsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;
94
unsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;
95
unsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;
96
+ unsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;
97
struct object_id *head = NULL, head_oid;
98
struct tree_desc desc[2] = { { NULL }, { NULL } };
99
struct lock_file lock = LOCK_INIT;
100
struct unpack_trees_options unpack_tree_opts = { 0 };
101
struct tree *tree;
102
+ struct index_state scratch_index = INDEX_STATE_INIT(r);
103
+ struct index_state *istate;
104
const char *action;
105
int ret = 0, nr = 0;
106
@@ -110,7 +113,7 @@ int reset_working_tree(struct repository *r,
113
if (opts->branch_msg && !opts->branch)
114
BUG("branch reflog message given without a branch");
115
113
- if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
116
+ if (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
117
ret = -1;
118
goto leave_reset_head;
119
}
@@ -125,16 +128,36 @@ int reset_working_tree(struct repository *r,
128
if (!oid)
129
oid = &head_oid;
130
128
- if (refs_only)
129
- return update_refs(r, opts, oid, head);
131
+ if (refs_only) {
132
+ if (!dry_run)
133
+ return update_refs(r, opts, oid, head);
134
+ return 0;
135
+ }
136
+
137
+ if (dry_run) {
138
+ if (read_index_from(&scratch_index, r->index_file, r->gitdir) < 0 ||
139
+ index_state_unmerged_to_stage0(&scratch_index) < 0) {
140
+ ret = error(_("could not read index"));
141
+ goto leave_reset_head;
142
+ }
143
+
144
+ istate = &scratch_index;
145
+ } else {
146
+ if (repo_read_index_unmerged(r) < 0) {
147
+ ret = error(_("could not read index"));
148
+ goto leave_reset_head;
149
+ }
150
+ istate = r->index;
151
+ }
152
153
action = reset_hard ? "reset" : "checkout";
154
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
155
unpack_tree_opts.head_idx = 1;
134
- unpack_tree_opts.src_index = r->index;
135
- unpack_tree_opts.dst_index = r->index;
156
+ unpack_tree_opts.src_index = istate;
157
+ unpack_tree_opts.dst_index = istate;
158
unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
137
- unpack_tree_opts.update = 1;
159
+ unpack_tree_opts.update = !dry_run;
160
+ unpack_tree_opts.dry_run = dry_run;
161
unpack_tree_opts.merge = 1;
162
unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
163
unpack_tree_opts.skip_cache_tree_update = 1;
@@ -142,11 +165,6 @@ int reset_working_tree(struct repository *r,
165
if (reset_hard)
166
unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
167
145
- if (repo_read_index_unmerged(r) < 0) {
146
- ret = error(_("could not read index"));
147
- goto leave_reset_head;
148
- }
149
-
168
if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
169
ret = error(_("failed to find tree of %s"),
170
oid_to_hex(&head_oid));
@@ -163,6 +181,9 @@ int reset_working_tree(struct repository *r,
181
goto leave_reset_head;
182
}
183
184
+ if (dry_run)
185
+ goto leave_reset_head;
186
+
187
tree = repo_parse_tree_indirect(r, oid);
188
if (!tree) {
189
ret = error(_("unable to read tree (%s)"), oid_to_hex(oid));
@@ -182,6 +203,7 @@ int reset_working_tree(struct repository *r,
203
leave_reset_head:
204
rollback_lock_file(&lock);
205
clear_unpack_trees_porcelain(&unpack_tree_opts);
206
+ release_index(&scratch_index);
207
while (nr)
208
free((void *)desc[--nr].buffer);
209
return ret;
reset.h
+6
@@ -21,6 +21,12 @@ enum reset_working_tree_flags {
21
22
/* Update ORIG_HEAD as well as HEAD */
23
RESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),
24
+
25
+ /*
26
+ * Perform a dry-run by performing the operation without updating
27
+ * any user-visible state.
28
+ */
29
+ RESET_WORKING_TREE_DRY_RUN = (1 << 5),
30
};
31
32
struct reset_working_tree_options {