7 #include "run-command.h"
9 #include "string-list.h"
11 struct string_list config_ignore
;
13 static int add_submodule_odb(const char *path
)
15 struct strbuf objects_directory
= STRBUF_INIT
;
16 struct alternate_object_database
*alt_odb
;
20 strbuf_addf(&objects_directory
, "%s/.git", path
);
21 git_dir
= read_gitfile_gently(objects_directory
.buf
);
23 strbuf_reset(&objects_directory
);
24 strbuf_addstr(&objects_directory
, git_dir
);
26 strbuf_addstr(&objects_directory
, "/objects/");
27 if (!is_directory(objects_directory
.buf
)) {
31 /* avoid adding it twice */
32 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
33 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
34 !strncmp(alt_odb
->base
, objects_directory
.buf
,
35 objects_directory
.len
))
38 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
39 alt_odb
->next
= alt_odb_list
;
40 strcpy(alt_odb
->base
, objects_directory
.buf
);
41 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
42 alt_odb
->name
[2] = '/';
43 alt_odb
->name
[40] = '\0';
44 alt_odb
->name
[41] = '\0';
45 alt_odb_list
= alt_odb
;
48 strbuf_release(&objects_directory
);
52 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
55 struct string_list_item
*ignore_option
;
56 ignore_option
= unsorted_string_list_lookup(&config_ignore
, path
);
58 handle_ignore_submodules_arg(diffopt
, ignore_option
->util
);
61 static int submodule_config(const char *var
, const char *value
, void *cb
)
63 if (!prefixcmp(var
, "submodule."))
64 return parse_submodule_config_option(var
, value
);
68 void gitmodules_config()
70 const char *work_tree
= get_git_work_tree();
72 struct strbuf gitmodules_path
= STRBUF_INIT
;
73 strbuf_addstr(&gitmodules_path
, work_tree
);
74 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
75 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
76 strbuf_release(&gitmodules_path
);
80 int parse_submodule_config_option(const char *var
, const char *value
)
83 struct string_list_item
*ignore_option
;
84 struct strbuf path
= STRBUF_INIT
;
86 var
+= 10; /* Skip "submodule." */
89 if ((len
> 7) && !strcmp(var
+ len
- 7, ".ignore")) {
90 if (strcmp(value
, "untracked") && strcmp(value
, "dirty") &&
91 strcmp(value
, "all") && strcmp(value
, "none")) {
92 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value
, var
);
96 strbuf_add(&path
, var
, len
- 7);
97 ignore_option
= unsorted_string_list_lookup(&config_ignore
, path
.buf
);
99 free(ignore_option
->util
);
101 ignore_option
= string_list_append(&config_ignore
,
102 strbuf_detach(&path
, NULL
));
103 strbuf_release(&path
);
104 ignore_option
->util
= xstrdup(value
);
110 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
113 if (!strcmp(arg
, "all"))
114 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
115 else if (!strcmp(arg
, "untracked"))
116 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
117 else if (!strcmp(arg
, "dirty"))
118 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
119 else if (strcmp(arg
, "none"))
120 die("bad --ignore-submodules argument: %s", arg
);
123 void show_submodule_summary(FILE *f
, const char *path
,
124 unsigned char one
[20], unsigned char two
[20],
125 unsigned dirty_submodule
,
126 const char *del
, const char *add
, const char *reset
)
129 struct commit
*commit
, *left
= left
, *right
= right
;
130 struct commit_list
*merge_bases
, *list
;
131 const char *message
= NULL
;
132 struct strbuf sb
= STRBUF_INIT
;
133 static const char *format
= " %m %s";
134 int fast_forward
= 0, fast_backward
= 0;
136 if (is_null_sha1(two
))
137 message
= "(submodule deleted)";
138 else if (add_submodule_odb(path
))
139 message
= "(not checked out)";
140 else if (is_null_sha1(one
))
141 message
= "(new submodule)";
142 else if (!(left
= lookup_commit_reference(one
)) ||
143 !(right
= lookup_commit_reference(two
)))
144 message
= "(commits not present)";
147 init_revisions(&rev
, NULL
);
148 setup_revisions(0, NULL
, &rev
, NULL
);
150 rev
.first_parent_only
= 1;
151 left
->object
.flags
|= SYMMETRIC_LEFT
;
152 add_pending_object(&rev
, &left
->object
, path
);
153 add_pending_object(&rev
, &right
->object
, path
);
154 merge_bases
= get_merge_bases(left
, right
, 1);
156 if (merge_bases
->item
== left
)
158 else if (merge_bases
->item
== right
)
161 for (list
= merge_bases
; list
; list
= list
->next
) {
162 list
->item
->object
.flags
|= UNINTERESTING
;
163 add_pending_object(&rev
, &list
->item
->object
,
164 sha1_to_hex(list
->item
->object
.sha1
));
166 if (prepare_revision_walk(&rev
))
167 message
= "(revision walker failed)";
170 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
171 fprintf(f
, "Submodule %s contains untracked content\n", path
);
172 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
173 fprintf(f
, "Submodule %s contains modified content\n", path
);
175 if (!hashcmp(one
, two
)) {
180 strbuf_addf(&sb
, "Submodule %s %s..", path
,
181 find_unique_abbrev(one
, DEFAULT_ABBREV
));
182 if (!fast_backward
&& !fast_forward
)
183 strbuf_addch(&sb
, '.');
184 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
186 strbuf_addf(&sb
, " %s\n", message
);
188 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
189 fwrite(sb
.buf
, sb
.len
, 1, f
);
192 while ((commit
= get_revision(&rev
))) {
193 struct pretty_print_context ctx
= {0};
194 ctx
.date_mode
= rev
.date_mode
;
195 strbuf_setlen(&sb
, 0);
196 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
198 strbuf_addstr(&sb
, del
);
201 strbuf_addstr(&sb
, add
);
202 format_commit_message(commit
, format
, &sb
, &ctx
);
204 strbuf_addstr(&sb
, reset
);
205 strbuf_addch(&sb
, '\n');
206 fprintf(f
, "%s", sb
.buf
);
208 clear_commit_marks(left
, ~0);
209 clear_commit_marks(right
, ~0);
214 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
217 struct child_process cp
;
218 const char *argv
[] = {
224 struct strbuf buf
= STRBUF_INIT
;
225 unsigned dirty_submodule
= 0;
226 const char *line
, *next_line
;
229 strbuf_addf(&buf
, "%s/.git", path
);
230 git_dir
= read_gitfile_gently(buf
.buf
);
233 if (!is_directory(git_dir
)) {
234 strbuf_release(&buf
);
235 /* The submodule is not checked out, so it is not modified */
241 if (ignore_untracked
)
244 memset(&cp
, 0, sizeof(cp
));
246 cp
.env
= local_repo_env
;
251 if (start_command(&cp
))
252 die("Could not run git status --porcelain");
254 len
= strbuf_read(&buf
, cp
.out
, 1024);
257 if ((line
[0] == '?') && (line
[1] == '?')) {
258 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
259 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
262 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
263 if (ignore_untracked
||
264 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
267 next_line
= strchr(line
, '\n');
271 len
-= (next_line
- line
);
276 if (finish_command(&cp
))
277 die("git status --porcelain failed");
279 strbuf_release(&buf
);
280 return dirty_submodule
;