7 #include "run-command.h"
9 int add_submodule_odb(const char *path
)
11 struct strbuf objects_directory
= STRBUF_INIT
;
12 struct alternate_object_database
*alt_odb
;
14 strbuf_addf(&objects_directory
, "%s/.git/objects/", path
);
15 if (!is_directory(objects_directory
.buf
))
18 /* avoid adding it twice */
19 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
20 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
21 !strncmp(alt_odb
->base
, objects_directory
.buf
,
22 objects_directory
.len
))
25 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
26 alt_odb
->next
= alt_odb_list
;
27 strcpy(alt_odb
->base
, objects_directory
.buf
);
28 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
29 alt_odb
->name
[2] = '/';
30 alt_odb
->name
[40] = '\0';
31 alt_odb
->name
[41] = '\0';
32 alt_odb_list
= alt_odb
;
37 void show_submodule_summary(FILE *f
, const char *path
,
38 unsigned char one
[20], unsigned char two
[20],
39 const char *del
, const char *add
, const char *reset
)
42 struct commit
*commit
, *left
= left
, *right
= right
;
43 struct commit_list
*merge_bases
, *list
;
44 const char *message
= NULL
;
45 struct strbuf sb
= STRBUF_INIT
;
46 static const char *format
= " %m %s";
47 int fast_forward
= 0, fast_backward
= 0;
49 if (is_null_sha1(two
))
50 message
= "(submodule deleted)";
51 else if (add_submodule_odb(path
))
52 message
= "(not checked out)";
53 else if (is_null_sha1(one
))
54 message
= "(new submodule)";
55 else if (!(left
= lookup_commit_reference(one
)) ||
56 !(right
= lookup_commit_reference(two
)))
57 message
= "(commits not present)";
60 init_revisions(&rev
, NULL
);
61 setup_revisions(0, NULL
, &rev
, NULL
);
63 rev
.first_parent_only
= 1;
64 left
->object
.flags
|= SYMMETRIC_LEFT
;
65 add_pending_object(&rev
, &left
->object
, path
);
66 add_pending_object(&rev
, &right
->object
, path
);
67 merge_bases
= get_merge_bases(left
, right
, 1);
69 if (merge_bases
->item
== left
)
71 else if (merge_bases
->item
== right
)
74 for (list
= merge_bases
; list
; list
= list
->next
) {
75 list
->item
->object
.flags
|= UNINTERESTING
;
76 add_pending_object(&rev
, &list
->item
->object
,
77 sha1_to_hex(list
->item
->object
.sha1
));
79 if (prepare_revision_walk(&rev
))
80 message
= "(revision walker failed)";
83 strbuf_addf(&sb
, "Submodule %s %s..", path
,
84 find_unique_abbrev(one
, DEFAULT_ABBREV
));
85 if (!fast_backward
&& !fast_forward
)
86 strbuf_addch(&sb
, '.');
87 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
89 strbuf_addf(&sb
, " %s\n", message
);
91 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
92 fwrite(sb
.buf
, sb
.len
, 1, f
);
95 while ((commit
= get_revision(&rev
))) {
96 struct pretty_print_context ctx
= {0};
97 ctx
.date_mode
= rev
.date_mode
;
98 strbuf_setlen(&sb
, 0);
99 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
101 strbuf_addstr(&sb
, del
);
104 strbuf_addstr(&sb
, add
);
105 format_commit_message(commit
, format
, &sb
, &ctx
);
107 strbuf_addstr(&sb
, reset
);
108 strbuf_addch(&sb
, '\n');
109 fprintf(f
, "%s", sb
.buf
);
111 clear_commit_marks(left
, ~0);
112 clear_commit_marks(right
, ~0);
117 int is_submodule_modified(const char *path
)
120 struct child_process cp
;
121 const char *argv
[] = {
127 struct strbuf buf
= STRBUF_INIT
;
129 strbuf_addf(&buf
, "%s/.git/", path
);
130 if (!is_directory(buf
.buf
)) {
131 strbuf_release(&buf
);
132 /* The submodule is not checked out, so it is not modified */
138 strbuf_addf(&buf
, "GIT_WORK_TREE=%s", path
);
139 env
[0] = strbuf_detach(&buf
, NULL
);
140 strbuf_addf(&buf
, "GIT_DIR=%s/.git", path
);
141 env
[1] = strbuf_detach(&buf
, NULL
);
144 memset(&cp
, 0, sizeof(cp
));
146 cp
.env
= (const char *const *)env
;
150 if (start_command(&cp
))
151 die("Could not run 'git status --porcelain' in submodule %s", path
);
153 len
= strbuf_read(&buf
, cp
.out
, 1024);
156 if (finish_command(&cp
))
157 die("'git status --porcelain' failed in submodule %s", path
);
161 strbuf_release(&buf
);