7 #include "run-command.h"
10 #include "string-list.h"
12 struct string_list config_name_for_path
;
13 struct string_list config_ignore_for_name
;
14 static int config_fetch_recurse_submodules
;
16 static int add_submodule_odb(const char *path
)
18 struct strbuf objects_directory
= STRBUF_INIT
;
19 struct alternate_object_database
*alt_odb
;
23 strbuf_addf(&objects_directory
, "%s/.git", path
);
24 git_dir
= read_gitfile_gently(objects_directory
.buf
);
26 strbuf_reset(&objects_directory
);
27 strbuf_addstr(&objects_directory
, git_dir
);
29 strbuf_addstr(&objects_directory
, "/objects/");
30 if (!is_directory(objects_directory
.buf
)) {
34 /* avoid adding it twice */
35 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
36 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
37 !strncmp(alt_odb
->base
, objects_directory
.buf
,
38 objects_directory
.len
))
41 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
42 alt_odb
->next
= alt_odb_list
;
43 strcpy(alt_odb
->base
, objects_directory
.buf
);
44 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
45 alt_odb
->name
[2] = '/';
46 alt_odb
->name
[40] = '\0';
47 alt_odb
->name
[41] = '\0';
48 alt_odb_list
= alt_odb
;
51 strbuf_release(&objects_directory
);
55 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
58 struct string_list_item
*path_option
, *ignore_option
;
59 path_option
= unsorted_string_list_lookup(&config_name_for_path
, path
);
61 ignore_option
= unsorted_string_list_lookup(&config_ignore_for_name
, path_option
->util
);
63 handle_ignore_submodules_arg(diffopt
, ignore_option
->util
);
67 int submodule_config(const char *var
, const char *value
, void *cb
)
69 if (!prefixcmp(var
, "submodule."))
70 return parse_submodule_config_option(var
, value
);
71 else if (!strcmp(var
, "fetch.recursesubmodules")) {
72 config_fetch_recurse_submodules
= git_config_bool(var
, value
);
78 void gitmodules_config(void)
80 const char *work_tree
= get_git_work_tree();
82 struct strbuf gitmodules_path
= STRBUF_INIT
;
83 strbuf_addstr(&gitmodules_path
, work_tree
);
84 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
85 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
86 strbuf_release(&gitmodules_path
);
90 int parse_submodule_config_option(const char *var
, const char *value
)
93 struct string_list_item
*config
;
94 struct strbuf submodname
= STRBUF_INIT
;
96 var
+= 10; /* Skip "submodule." */
99 if ((len
> 5) && !strcmp(var
+ len
- 5, ".path")) {
100 strbuf_add(&submodname
, var
, len
- 5);
101 config
= unsorted_string_list_lookup(&config_name_for_path
, value
);
105 config
= string_list_append(&config_name_for_path
, xstrdup(value
));
106 config
->util
= strbuf_detach(&submodname
, NULL
);
107 strbuf_release(&submodname
);
108 } else if ((len
> 7) && !strcmp(var
+ len
- 7, ".ignore")) {
109 if (strcmp(value
, "untracked") && strcmp(value
, "dirty") &&
110 strcmp(value
, "all") && strcmp(value
, "none")) {
111 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value
, var
);
115 strbuf_add(&submodname
, var
, len
- 7);
116 config
= unsorted_string_list_lookup(&config_ignore_for_name
, submodname
.buf
);
120 config
= string_list_append(&config_ignore_for_name
,
121 strbuf_detach(&submodname
, NULL
));
122 strbuf_release(&submodname
);
123 config
->util
= xstrdup(value
);
129 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
132 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
133 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
134 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
136 if (!strcmp(arg
, "all"))
137 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
138 else if (!strcmp(arg
, "untracked"))
139 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
140 else if (!strcmp(arg
, "dirty"))
141 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
142 else if (strcmp(arg
, "none"))
143 die("bad --ignore-submodules argument: %s", arg
);
146 void show_submodule_summary(FILE *f
, const char *path
,
147 unsigned char one
[20], unsigned char two
[20],
148 unsigned dirty_submodule
,
149 const char *del
, const char *add
, const char *reset
)
152 struct commit
*commit
, *left
= left
, *right
= right
;
153 struct commit_list
*merge_bases
, *list
;
154 const char *message
= NULL
;
155 struct strbuf sb
= STRBUF_INIT
;
156 static const char *format
= " %m %s";
157 int fast_forward
= 0, fast_backward
= 0;
159 if (is_null_sha1(two
))
160 message
= "(submodule deleted)";
161 else if (add_submodule_odb(path
))
162 message
= "(not checked out)";
163 else if (is_null_sha1(one
))
164 message
= "(new submodule)";
165 else if (!(left
= lookup_commit_reference(one
)) ||
166 !(right
= lookup_commit_reference(two
)))
167 message
= "(commits not present)";
170 init_revisions(&rev
, NULL
);
171 setup_revisions(0, NULL
, &rev
, NULL
);
173 rev
.first_parent_only
= 1;
174 left
->object
.flags
|= SYMMETRIC_LEFT
;
175 add_pending_object(&rev
, &left
->object
, path
);
176 add_pending_object(&rev
, &right
->object
, path
);
177 merge_bases
= get_merge_bases(left
, right
, 1);
179 if (merge_bases
->item
== left
)
181 else if (merge_bases
->item
== right
)
184 for (list
= merge_bases
; list
; list
= list
->next
) {
185 list
->item
->object
.flags
|= UNINTERESTING
;
186 add_pending_object(&rev
, &list
->item
->object
,
187 sha1_to_hex(list
->item
->object
.sha1
));
189 if (prepare_revision_walk(&rev
))
190 message
= "(revision walker failed)";
193 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
194 fprintf(f
, "Submodule %s contains untracked content\n", path
);
195 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
196 fprintf(f
, "Submodule %s contains modified content\n", path
);
198 if (!hashcmp(one
, two
)) {
203 strbuf_addf(&sb
, "Submodule %s %s..", path
,
204 find_unique_abbrev(one
, DEFAULT_ABBREV
));
205 if (!fast_backward
&& !fast_forward
)
206 strbuf_addch(&sb
, '.');
207 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
209 strbuf_addf(&sb
, " %s\n", message
);
211 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
212 fwrite(sb
.buf
, sb
.len
, 1, f
);
215 while ((commit
= get_revision(&rev
))) {
216 struct pretty_print_context ctx
= {0};
217 ctx
.date_mode
= rev
.date_mode
;
218 strbuf_setlen(&sb
, 0);
219 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
221 strbuf_addstr(&sb
, del
);
224 strbuf_addstr(&sb
, add
);
225 format_commit_message(commit
, format
, &sb
, &ctx
);
227 strbuf_addstr(&sb
, reset
);
228 strbuf_addch(&sb
, '\n');
229 fprintf(f
, "%s", sb
.buf
);
231 clear_commit_marks(left
, ~0);
232 clear_commit_marks(right
, ~0);
237 void set_config_fetch_recurse_submodules(int value
)
239 config_fetch_recurse_submodules
= value
;
242 int fetch_populated_submodules(int num_options
, const char **options
,
243 const char *prefix
, int ignore_config
,
246 int i
, result
= 0, argc
= 0;
247 struct child_process cp
;
249 struct string_list_item
*name_for_path
;
250 const char *work_tree
= get_git_work_tree();
254 if (!the_index
.initialized
)
255 if (read_cache() < 0)
256 die("index file corrupt");
258 argv
= xcalloc(num_options
+ 5, sizeof(const char *));
259 argv
[argc
++] = "fetch";
260 for (i
= 0; i
< num_options
; i
++)
261 argv
[argc
++] = options
[i
];
262 argv
[argc
++] = "--submodule-prefix";
264 memset(&cp
, 0, sizeof(cp
));
266 cp
.env
= local_repo_env
;
270 for (i
= 0; i
< active_nr
; i
++) {
271 struct strbuf submodule_path
= STRBUF_INIT
;
272 struct strbuf submodule_git_dir
= STRBUF_INIT
;
273 struct strbuf submodule_prefix
= STRBUF_INIT
;
274 struct cache_entry
*ce
= active_cache
[i
];
275 const char *git_dir
, *name
;
277 if (!S_ISGITLINK(ce
->ce_mode
))
281 name_for_path
= unsorted_string_list_lookup(&config_name_for_path
, ce
->name
);
283 name
= name_for_path
->util
;
285 if (!ignore_config
) {
286 if (!config_fetch_recurse_submodules
)
290 strbuf_addf(&submodule_path
, "%s/%s", work_tree
, ce
->name
);
291 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
292 strbuf_addf(&submodule_prefix
, "%s%s/", prefix
, ce
->name
);
293 git_dir
= read_gitfile_gently(submodule_git_dir
.buf
);
295 git_dir
= submodule_git_dir
.buf
;
296 if (is_directory(git_dir
)) {
298 printf("Fetching submodule %s%s\n", prefix
, ce
->name
);
299 cp
.dir
= submodule_path
.buf
;
300 argv
[argc
] = submodule_prefix
.buf
;
301 if (run_command(&cp
))
304 strbuf_release(&submodule_path
);
305 strbuf_release(&submodule_git_dir
);
306 strbuf_release(&submodule_prefix
);
312 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
315 struct child_process cp
;
316 const char *argv
[] = {
322 struct strbuf buf
= STRBUF_INIT
;
323 unsigned dirty_submodule
= 0;
324 const char *line
, *next_line
;
327 strbuf_addf(&buf
, "%s/.git", path
);
328 git_dir
= read_gitfile_gently(buf
.buf
);
331 if (!is_directory(git_dir
)) {
332 strbuf_release(&buf
);
333 /* The submodule is not checked out, so it is not modified */
339 if (ignore_untracked
)
342 memset(&cp
, 0, sizeof(cp
));
344 cp
.env
= local_repo_env
;
349 if (start_command(&cp
))
350 die("Could not run git status --porcelain");
352 len
= strbuf_read(&buf
, cp
.out
, 1024);
355 if ((line
[0] == '?') && (line
[1] == '?')) {
356 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
357 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
360 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
361 if (ignore_untracked
||
362 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
365 next_line
= strchr(line
, '\n');
369 len
-= (next_line
- line
);
374 if (finish_command(&cp
))
375 die("git status --porcelain failed");
377 strbuf_release(&buf
);
378 return dirty_submodule
;
381 static int find_first_merges(struct object_array
*result
, const char *path
,
382 struct commit
*a
, struct commit
*b
)
385 struct object_array merges
;
386 struct commit
*commit
;
387 int contains_another
;
389 char merged_revision
[42];
390 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
391 "--all", merged_revision
, NULL
};
392 struct rev_info revs
;
393 struct setup_revision_opt rev_opts
;
395 memset(&merges
, 0, sizeof(merges
));
396 memset(result
, 0, sizeof(struct object_array
));
397 memset(&rev_opts
, 0, sizeof(rev_opts
));
399 /* get all revisions that merge commit a */
400 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
401 sha1_to_hex(a
->object
.sha1
));
402 init_revisions(&revs
, NULL
);
403 rev_opts
.submodule
= path
;
404 setup_revisions(sizeof(rev_args
)/sizeof(char *)-1, rev_args
, &revs
, &rev_opts
);
406 /* save all revisions from the above list that contain b */
407 if (prepare_revision_walk(&revs
))
408 die("revision walk setup failed");
409 while ((commit
= get_revision(&revs
)) != NULL
) {
410 struct object
*o
= &(commit
->object
);
411 if (in_merge_bases(b
, &commit
, 1))
412 add_object_array(o
, NULL
, &merges
);
415 /* Now we've got all merges that contain a and b. Prune all
416 * merges that contain another found merge and save them in
419 for (i
= 0; i
< merges
.nr
; i
++) {
420 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
422 contains_another
= 0;
423 for (j
= 0; j
< merges
.nr
; j
++) {
424 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
425 if (i
!= j
&& in_merge_bases(m2
, &m1
, 1)) {
426 contains_another
= 1;
431 if (!contains_another
)
432 add_object_array(merges
.objects
[i
].item
,
433 merges
.objects
[i
].name
, result
);
436 free(merges
.objects
);
440 static void print_commit(struct commit
*commit
)
442 struct strbuf sb
= STRBUF_INIT
;
443 struct pretty_print_context ctx
= {0};
444 ctx
.date_mode
= DATE_NORMAL
;
445 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
446 fprintf(stderr
, "%s\n", sb
.buf
);
450 #define MERGE_WARNING(path, msg) \
451 warning("Failed to merge submodule %s (%s)", path, msg);
453 int merge_submodule(unsigned char result
[20], const char *path
,
454 const unsigned char base
[20], const unsigned char a
[20],
455 const unsigned char b
[20])
457 struct commit
*commit_base
, *commit_a
, *commit_b
;
459 struct object_array merges
;
463 /* store a in result in case we fail */
466 /* we can not handle deletion conflicts */
467 if (is_null_sha1(base
))
474 if (add_submodule_odb(path
)) {
475 MERGE_WARNING(path
, "not checked out");
479 if (!(commit_base
= lookup_commit_reference(base
)) ||
480 !(commit_a
= lookup_commit_reference(a
)) ||
481 !(commit_b
= lookup_commit_reference(b
))) {
482 MERGE_WARNING(path
, "commits not present");
486 /* check whether both changes are forward */
487 if (!in_merge_bases(commit_base
, &commit_a
, 1) ||
488 !in_merge_bases(commit_base
, &commit_b
, 1)) {
489 MERGE_WARNING(path
, "commits don't follow merge-base");
493 /* Case #1: a is contained in b or vice versa */
494 if (in_merge_bases(commit_a
, &commit_b
, 1)) {
498 if (in_merge_bases(commit_b
, &commit_a
, 1)) {
504 * Case #2: There are one or more merges that contain a and b in
505 * the submodule. If there is only one, then present it as a
506 * suggestion to the user, but leave it marked unmerged so the
507 * user needs to confirm the resolution.
510 /* find commit which merges them */
511 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
512 switch (parent_count
) {
514 MERGE_WARNING(path
, "merge following commits not found");
518 MERGE_WARNING(path
, "not fast-forward");
519 fprintf(stderr
, "Found a possible merge resolution "
520 "for the submodule:\n");
521 print_commit((struct commit
*) merges
.objects
[0].item
);
523 "If this is correct simply add it to the index "
526 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
527 "which will accept this suggestion.\n",
528 sha1_to_hex(merges
.objects
[0].item
->sha1
), path
);
532 MERGE_WARNING(path
, "multiple merges found");
533 for (i
= 0; i
< merges
.nr
; i
++)
534 print_commit((struct commit
*) merges
.objects
[i
].item
);
537 free(merges
.objects
);