Reduce parse-options.o dependencies
Currently parse-options.o pulls quite a big bunch of dependencies. his complicates it's usage in contrib/ because it pulls external dependencies and it also increases executables size. Split off less generic and more internal to git part of parse-options.c to parse-options-cb.c. Move prefix_filename function from setup.c to abspath.c. abspath.o and wrapper.o pull each other, so it's unlikely to increase the dependencies. It was a dependency of parse-options.o that pulled many others. Now parse-options.o pulls just abspath.o, ctype.o, strbuf.o, usage.o, wrapper.o, libc directly and strlcpy.o indirectly. Signed-off-by: Dmitry Ivankov <divanorama@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Dmitry Ivankov committed
Aug 11, 2011 at 15:15 UTC
068762846634dff73a18d71188736e0ded03a1cc
5 files changed
+155
-150
Makefile
+2
-1
@@ -642,6 +642,7 @@ LIB_OBJS += pack-revindex.o
642
LIB_OBJS += pack-write.o
643
LIB_OBJS += pager.o
644
LIB_OBJS += parse-options.o
645
+LIB_OBJS += parse-options-cb.o
646
LIB_OBJS += patch-delta.o
647
LIB_OBJS += patch-ids.o
648
LIB_OBJS += path.o
@@ -2204,7 +2205,7 @@ test-delta$X: diff-delta.o patch-delta.o
2205
2206
test-line-buffer$X: vcs-svn/lib.a
2207
2207
-test-parse-options$X: parse-options.o
2208
+test-parse-options$X: parse-options.o parse-options-cb.o
2209
2210
test-string-pool$X: vcs-svn/lib.a
2211
abspath.c
+28
@@ -139,3 +139,31 @@ const char *absolute_path(const char *path)
139
}
140
return buf;
141
}
142
+
143
+/*
144
+ * Unlike prefix_path, this should be used if the named file does
145
+ * not have to interact with index entry; i.e. name of a random file
146
+ * on the filesystem.
147
+ */
148
+const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
149
+{
150
+ static char path[PATH_MAX];
151
+#ifndef WIN32
152
+ if (!pfx_len || is_absolute_path(arg))
153
+ return arg;
154
+ memcpy(path, pfx, pfx_len);
155
+ strcpy(path + pfx_len, arg);
156
+#else
157
+ char *p;
158
+ /* don't add prefix to absolute paths, but still replace '\' by '/' */
159
+ if (is_absolute_path(arg))
160
+ pfx_len = 0;
161
+ else if (pfx_len)
162
+ memcpy(path, pfx, pfx_len);
163
+ strcpy(path + pfx_len, arg);
164
+ for (p = path + pfx_len; *p; p++)
165
+ if (*p == '\\')
166
+ *p = '/';
167
+#endif
168
+ return path;
169
+}
parse-options-cb.c
new
+125
@@ -0,0 +1,125 @@
1
+#include "git-compat-util.h"
2
+#include "parse-options.h"
3
+#include "cache.h"
4
+#include "commit.h"
5
+#include "color.h"
6
+#include "string-list.h"
7
+
8
+/*----- some often used options -----*/
9
+
10
+int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
11
+{
12
+ int v;
13
+
14
+ if (!arg) {
15
+ v = unset ? 0 : DEFAULT_ABBREV;
16
+ } else {
17
+ v = strtol(arg, (char **)&arg, 10);
18
+ if (*arg)
19
+ return opterror(opt, "expects a numerical value", 0);
20
+ if (v && v < MINIMUM_ABBREV)
21
+ v = MINIMUM_ABBREV;
22
+ else if (v > 40)
23
+ v = 40;
24
+ }
25
+ *(int *)(opt->value) = v;
26
+ return 0;
27
+}
28
+
29
+int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
30
+ int unset)
31
+{
32
+ *(unsigned long *)(opt->value) = approxidate(arg);
33
+ return 0;
34
+}
35
+
36
+int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
37
+ int unset)
38
+{
39
+ int value;
40
+
41
+ if (!arg)
42
+ arg = unset ? "never" : (const char *)opt->defval;
43
+ value = git_config_colorbool(NULL, arg, -1);
44
+ if (value < 0)
45
+ return opterror(opt,
46
+ "expects \"always\", \"auto\", or \"never\"", 0);
47
+ *(int *)opt->value = value;
48
+ return 0;
49
+}
50
+
51
+int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
52
+ int unset)
53
+{
54
+ int *target = opt->value;
55
+
56
+ if (unset)
57
+ /* --no-quiet, --no-verbose */
58
+ *target = 0;
59
+ else if (opt->short_name == 'v') {
60
+ if (*target >= 0)
61
+ (*target)++;
62
+ else
63
+ *target = 1;
64
+ } else {
65
+ if (*target <= 0)
66
+ (*target)--;
67
+ else
68
+ *target = -1;
69
+ }
70
+ return 0;
71
+}
72
+
73
+int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
74
+{
75
+ unsigned char sha1[20];
76
+ struct commit *commit;
77
+
78
+ if (!arg)
79
+ return -1;
80
+ if (get_sha1(arg, sha1))
81
+ return error("malformed object name %s", arg);
82
+ commit = lookup_commit_reference(sha1);
83
+ if (!commit)
84
+ return error("no such commit %s", arg);
85
+ commit_list_insert(commit, opt->value);
86
+ return 0;
87
+}
88
+
89
+int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
90
+{
91
+ int *target = opt->value;
92
+ *target = unset ? 2 : 1;
93
+ return 0;
94
+}
95
+
96
+int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
97
+{
98
+ int i, j;
99
+
100
+ for (i = 0; i < dst_size; i++)
101
+ if (dst[i].type == OPTION_END)
102
+ break;
103
+ for (j = 0; i < dst_size; i++, j++) {
104
+ dst[i] = src[j];
105
+ if (src[j].type == OPTION_END)
106
+ return 0;
107
+ }
108
+ return -1;
109
+}
110
+
111
+int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
112
+{
113
+ struct string_list *v = opt->value;
114
+
115
+ if (unset) {
116
+ string_list_clear(v, 0);
117
+ return 0;
118
+ }
119
+
120
+ if (!arg)
121
+ return -1;
122
+
123
+ string_list_append(v, xstrdup(arg));
124
+ return 0;
125
+}
parse-options.c
-121
@@ -3,7 +3,6 @@
3
#include "cache.h"
4
#include "commit.h"
5
#include "color.h"
6
-#include "string-list.h"
6
7
static int parse_options_usage(struct parse_opt_ctx_t *ctx,
8
const char * const *usagestr,
@@ -584,123 +583,3 @@ static int parse_options_usage(struct parse_opt_ctx_t *ctx,
583
return usage_with_options_internal(ctx, usagestr, opts, 0, err);
584
}
585
587
-
588
-/*----- some often used options -----*/
589
-#include "cache.h"
590
-
591
-int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
592
-{
593
- int v;
594
-
595
- if (!arg) {
596
- v = unset ? 0 : DEFAULT_ABBREV;
597
- } else {
598
- v = strtol(arg, (char **)&arg, 10);
599
- if (*arg)
600
- return opterror(opt, "expects a numerical value", 0);
601
- if (v && v < MINIMUM_ABBREV)
602
- v = MINIMUM_ABBREV;
603
- else if (v > 40)
604
- v = 40;
605
- }
606
- *(int *)(opt->value) = v;
607
- return 0;
608
-}
609
-
610
-int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
611
- int unset)
612
-{
613
- *(unsigned long *)(opt->value) = approxidate(arg);
614
- return 0;
615
-}
616
-
617
-int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
618
- int unset)
619
-{
620
- int value;
621
-
622
- if (!arg)
623
- arg = unset ? "never" : (const char *)opt->defval;
624
- value = git_config_colorbool(NULL, arg, -1);
625
- if (value < 0)
626
- return opterror(opt,
627
- "expects \"always\", \"auto\", or \"never\"", 0);
628
- *(int *)opt->value = value;
629
- return 0;
630
-}
631
-
632
-int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
633
- int unset)
634
-{
635
- int *target = opt->value;
636
-
637
- if (unset)
638
- /* --no-quiet, --no-verbose */
639
- *target = 0;
640
- else if (opt->short_name == 'v') {
641
- if (*target >= 0)
642
- (*target)++;
643
- else
644
- *target = 1;
645
- } else {
646
- if (*target <= 0)
647
- (*target)--;
648
- else
649
- *target = -1;
650
- }
651
- return 0;
652
-}
653
-
654
-int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
655
-{
656
- unsigned char sha1[20];
657
- struct commit *commit;
658
-
659
- if (!arg)
660
- return -1;
661
- if (get_sha1(arg, sha1))
662
- return error("malformed object name %s", arg);
663
- commit = lookup_commit_reference(sha1);
664
- if (!commit)
665
- return error("no such commit %s", arg);
666
- commit_list_insert(commit, opt->value);
667
- return 0;
668
-}
669
-
670
-int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
671
-{
672
- int *target = opt->value;
673
- *target = unset ? 2 : 1;
674
- return 0;
675
-}
676
-
677
-int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
678
-{
679
- int i, j;
680
-
681
- for (i = 0; i < dst_size; i++)
682
- if (dst[i].type == OPTION_END)
683
- break;
684
- for (j = 0; i < dst_size; i++, j++) {
685
- dst[i] = src[j];
686
- if (src[j].type == OPTION_END)
687
- return 0;
688
- }
689
- return -1;
690
-}
691
-
692
-int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
693
-{
694
- struct string_list *v = opt->value;
695
-
696
- if (unset) {
697
- string_list_clear(v, 0);
698
- return 0;
699
- }
700
-
701
- if (!arg)
702
- return -1;
703
-
704
- string_list_append(v, xstrdup(arg));
705
- return 0;
706
-}
setup.c
-28
@@ -40,34 +40,6 @@ char *prefix_path(const char *prefix, int len, const char *path)
40
return sanitized;
41
}
42
43
-/*
44
- * Unlike prefix_path, this should be used if the named file does
45
- * not have to interact with index entry; i.e. name of a random file
46
- * on the filesystem.
47
- */
48
-const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
49
-{
50
- static char path[PATH_MAX];
51
-#ifndef WIN32
52
- if (!pfx_len || is_absolute_path(arg))
53
- return arg;
54
- memcpy(path, pfx, pfx_len);
55
- strcpy(path + pfx_len, arg);
56
-#else
57
- char *p;
58
- /* don't add prefix to absolute paths, but still replace '\' by '/' */
59
- if (is_absolute_path(arg))
60
- pfx_len = 0;
61
- else if (pfx_len)
62
- memcpy(path, pfx, pfx_len);
63
- strcpy(path + pfx_len, arg);
64
- for (p = path + pfx_len; *p; p++)
65
- if (*p == '\\')
66
- *p = '/';
67
-#endif
68
- return path;
69
-}
70
-
43
int check_filename(const char *prefix, const char *arg)
44
{
45
const char *name;