@matheusbsbs / matheus-bagliano / commits / 041aee31be
Fork 0

builtin-add.c: restructure the code for maintainability

A private function add_files_to_cache() in builtin-add.c was borrowed by checkout and commit re-implementors without getting properly refactored to more library-ish place. This does the refactoring. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 21, 2008 at 01:24 UTC 041aee31be378b3b38e3a0913b29970a7f78873b
3 files changed +62 -57
builtin-add.c
-57
@@ -8,10 +8,6 @@
8 #include "dir.h"
9 #include "exec_cmd.h"
10 #include "cache-tree.h"
11 -#include "diff.h"
12 -#include "diffcore.h"
13 -#include "commit.h"
14 -#include "revision.h"
11 #include "run-command.h"
12 #include "parse-options.h"
13
@@ -79,59 +75,6 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec,
75 prune_directory(dir, pathspec, baselen);
76 }
77
82 -struct update_callback_data
83 -{
84 - int flags;
85 - int add_errors;
86 -};
87 -
88 -static void update_callback(struct diff_queue_struct *q,
89 - struct diff_options *opt, void *cbdata)
90 -{
91 - int i;
92 - struct update_callback_data *data = cbdata;
93 -
94 - for (i = 0; i < q->nr; i++) {
95 - struct diff_filepair *p = q->queue[i];
96 - const char *path = p->one->path;
97 - switch (p->status) {
98 - default:
99 - die("unexpected diff status %c", p->status);
100 - case DIFF_STATUS_UNMERGED:
101 - case DIFF_STATUS_MODIFIED:
102 - case DIFF_STATUS_TYPE_CHANGED:
103 - if (add_file_to_cache(path, data->flags)) {
104 - if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
105 - die("updating files failed");
106 - data->add_errors++;
107 - }
108 - break;
109 - case DIFF_STATUS_DELETED:
110 - if (!(data->flags & ADD_CACHE_PRETEND))
111 - remove_file_from_cache(path);
112 - if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
113 - printf("remove '%s'\n", path);
114 - break;
115 - }
116 - }
117 -}
118 -
119 -int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
120 -{
121 - struct update_callback_data data;
122 - struct rev_info rev;
123 - init_revisions(&rev, prefix);
124 - setup_revisions(0, NULL, &rev, NULL);
125 - rev.prune_data = pathspec;
126 - rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
127 - rev.diffopt.format_callback = update_callback;
128 - data.flags = flags;
129 - data.add_errors = 0;
130 - rev.diffopt.format_callback_data = &data;
131 - run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
132 - return !!data.add_errors;
133 -}
134 -
78 static void refresh(int verbose, const char **pathspec)
79 {
80 char *seen;
cache.h
+1
@@ -375,6 +375,7 @@ extern int remove_file_from_index(struct index_state *, const char *path);
375 #define ADD_CACHE_VERBOSE 1
376 #define ADD_CACHE_PRETEND 2
377 #define ADD_CACHE_IGNORE_ERRORS 4
378 +#define ADD_CACHE_IGNORE_REMOVAL 8
379 extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
380 extern int add_file_to_index(struct index_state *, const char *path, int flags);
381 extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
read-cache.c
+61
@@ -8,6 +8,11 @@
8 #include "cache-tree.h"
9 #include "refs.h"
10 #include "dir.h"
11 +#include "tree.h"
12 +#include "commit.h"
13 +#include "diff.h"
14 +#include "diffcore.h"
15 +#include "revision.h"
16
17 /* Index extensions.
18 *
@@ -1444,3 +1449,59 @@ int read_index_unmerged(struct index_state *istate)
1449 istate->cache_nr = dst - istate->cache;
1450 return !!last;
1451 }
1452 +
1453 +struct update_callback_data
1454 +{
1455 + int flags;
1456 + int add_errors;
1457 +};
1458 +
1459 +static void update_callback(struct diff_queue_struct *q,
1460 + struct diff_options *opt, void *cbdata)
1461 +{
1462 + int i;
1463 + struct update_callback_data *data = cbdata;
1464 +
1465 + for (i = 0; i < q->nr; i++) {
1466 + struct diff_filepair *p = q->queue[i];
1467 + const char *path = p->one->path;
1468 + switch (p->status) {
1469 + default:
1470 + die("unexpected diff status %c", p->status);
1471 + case DIFF_STATUS_UNMERGED:
1472 + case DIFF_STATUS_MODIFIED:
1473 + case DIFF_STATUS_TYPE_CHANGED:
1474 + if (add_file_to_index(&the_index, path, data->flags)) {
1475 + if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
1476 + die("updating files failed");
1477 + data->add_errors++;
1478 + }
1479 + break;
1480 + case DIFF_STATUS_DELETED:
1481 + if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
1482 + break;
1483 + if (!(data->flags & ADD_CACHE_PRETEND))
1484 + remove_file_from_index(&the_index, path);
1485 + if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
1486 + printf("remove '%s'\n", path);
1487 + break;
1488 + }
1489 + }
1490 +}
1491 +
1492 +int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
1493 +{
1494 + struct update_callback_data data;
1495 + struct rev_info rev;
1496 + init_revisions(&rev, prefix);
1497 + setup_revisions(0, NULL, &rev, NULL);
1498 + rev.prune_data = pathspec;
1499 + rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1500 + rev.diffopt.format_callback = update_callback;
1501 + data.flags = flags;
1502 + data.add_errors = 0;
1503 + rev.diffopt.format_callback_data = &data;
1504 + run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
1505 + return !!data.add_errors;
1506 +}
1507 +