7
#include "run-command.h"
8
#include "strbuf.h"
9
#include "string-list.h"
10
+#include "cache.h"
11
+#include "cache-tree.h"
12
+#include "lockfile.h"
13
+#include "resolve-undo.h"
14
+#include "unpack-trees.h"
15
16
static char const * const builtin_sparse_checkout_usage[] = {
17
N_("git sparse-checkout (init|list|set|disable) <options>"),
65
return 0;
66
}
67
63
-static int update_working_directory(void)
68
+static int update_working_directory(struct pattern_list *pl)
69
{
65
- struct argv_array argv = ARGV_ARRAY_INIT;
70
int result = 0;
67
- argv_array_pushl(&argv, "read-tree", "-m", "-u", "HEAD", NULL);
71
+ struct unpack_trees_options o;
72
+ struct lock_file lock_file = LOCK_INIT;
73
+ struct object_id oid;
74
+ struct tree *tree;
75
+ struct tree_desc t;
76
+ struct repository *r = the_repository;
77
69
- if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
70
- error(_("failed to update index with new sparse-checkout patterns"));
71
- result = 1;
72
- }
78
+ if (repo_read_index_unmerged(r))
79
+ die(_("you need to resolve your current index first"));
80
+
81
+ if (get_oid("HEAD", &oid))
82
+ return 0;
83
+
84
+ tree = parse_tree_indirect(&oid);
85
+ parse_tree(tree);
86
+ init_tree_desc(&t, tree->buffer, tree->size);
87
+
88
+ memset(&o, 0, sizeof(o));
89
+ o.verbose_update = isatty(2);
90
+ o.merge = 1;
91
+ o.update = 1;
92
+ o.fn = oneway_merge;
93
+ o.head_idx = -1;
94
+ o.src_index = r->index;
95
+ o.dst_index = r->index;
96
+ o.skip_sparse_checkout = 0;
97
+ o.pl = pl;
98
+ o.keep_pattern_list = !!pl;
99
+
100
+ resolve_undo_clear_index(r->index);
101
+ setup_work_tree();
102
+
103
+ cache_tree_free(&r->index->cache_tree);
104
+
105
+ repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
106
+
107
+ core_apply_sparse_checkout = 1;
108
+ result = unpack_trees(1, &t, &o);
109
+
110
+ if (!result) {
111
+ prime_cache_tree(r, r->index, tree);
112
+ write_locked_index(r->index, &lock_file, COMMIT_LOCK);
113
+ } else
114
+ rollback_lock_file(&lock_file);
115
74
- argv_array_clear(&argv);
116
return result;
117
}
118
170
{
171
char *sparse_filename;
172
FILE *fp;
173
+ int result;
174
+
175
+ result = update_working_directory(pl);
176
+
177
+ if (result) {
178
+ clear_pattern_list(pl);
179
+ update_working_directory(NULL);
180
+ return result;
181
+ }
182
183
sparse_filename = get_sparse_checkout_filename();
184
fp = fopen(sparse_filename, "w");
189
write_patterns_to_file(fp, pl);
190
191
fclose(fp);
192
+
193
free(sparse_filename);
194
+ clear_pattern_list(pl);
195
144
- return update_working_directory();
196
+ return 0;
197
}
198
199
enum sparse_checkout_mode {
251
builtin_sparse_checkout_init_options,
252
builtin_sparse_checkout_init_usage, 0);
253
202
- mode = init_opts.cone_mode ? MODE_CONE_PATTERNS : MODE_ALL_PATTERNS;
254
+ if (init_opts.cone_mode) {
255
+ mode = MODE_CONE_PATTERNS;
256
+ core_sparse_checkout_cone = 1;
257
+ } else
258
+ mode = MODE_ALL_PATTERNS;
259
260
if (set_config(mode))
261
return 1;
286
}
287
288
reset_dir:
233
- return update_working_directory();
289
+ core_apply_sparse_checkout = 1;
290
+ return update_working_directory(NULL);
291
}
292
293
static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
368
369
hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
370
hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
371
+ pl.use_cone_patterns = 1;
372
373
if (set_opts.use_stdin) {
374
while (!strbuf_getline(&line, stdin))
423
fprintf(fp, "/*\n");
424
fclose(fp);
425
368
- if (update_working_directory())
426
+ core_apply_sparse_checkout = 1;
427
+ if (update_working_directory(NULL))
428
die(_("error while refreshing working directory"));
429
430
unlink(sparse_filename);