7 #include "run-command.h"
9 static 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 unsigned dirty_submodule
,
40 const char *del
, const char *add
, const char *reset
)
43 struct commit
*commit
, *left
= left
, *right
= right
;
44 struct commit_list
*merge_bases
, *list
;
45 const char *message
= NULL
;
46 struct strbuf sb
= STRBUF_INIT
;
47 static const char *format
= " %m %s";
48 int fast_forward
= 0, fast_backward
= 0;
50 if (is_null_sha1(two
))
51 message
= "(submodule deleted)";
52 else if (add_submodule_odb(path
))
53 message
= "(not checked out)";
54 else if (is_null_sha1(one
))
55 message
= "(new submodule)";
56 else if (!(left
= lookup_commit_reference(one
)) ||
57 !(right
= lookup_commit_reference(two
)))
58 message
= "(commits not present)";
61 init_revisions(&rev
, NULL
);
62 setup_revisions(0, NULL
, &rev
, NULL
);
64 rev
.first_parent_only
= 1;
65 left
->object
.flags
|= SYMMETRIC_LEFT
;
66 add_pending_object(&rev
, &left
->object
, path
);
67 add_pending_object(&rev
, &right
->object
, path
);
68 merge_bases
= get_merge_bases(left
, right
, 1);
70 if (merge_bases
->item
== left
)
72 else if (merge_bases
->item
== right
)
75 for (list
= merge_bases
; list
; list
= list
->next
) {
76 list
->item
->object
.flags
|= UNINTERESTING
;
77 add_pending_object(&rev
, &list
->item
->object
,
78 sha1_to_hex(list
->item
->object
.sha1
));
80 if (prepare_revision_walk(&rev
))
81 message
= "(revision walker failed)";
84 strbuf_addf(&sb
, "Submodule %s %s..", path
,
85 find_unique_abbrev(one
, DEFAULT_ABBREV
));
86 if (!fast_backward
&& !fast_forward
)
87 strbuf_addch(&sb
, '.');
88 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
90 strbuf_add(&sb
, "-dirty", 6);
92 strbuf_addf(&sb
, " %s\n", message
);
94 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
95 fwrite(sb
.buf
, sb
.len
, 1, f
);
98 while ((commit
= get_revision(&rev
))) {
99 struct pretty_print_context ctx
= {0};
100 ctx
.date_mode
= rev
.date_mode
;
101 strbuf_setlen(&sb
, 0);
102 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
104 strbuf_addstr(&sb
, del
);
107 strbuf_addstr(&sb
, add
);
108 format_commit_message(commit
, format
, &sb
, &ctx
);
110 strbuf_addstr(&sb
, reset
);
111 strbuf_addch(&sb
, '\n');
112 fprintf(f
, "%s", sb
.buf
);
114 clear_commit_marks(left
, ~0);
115 clear_commit_marks(right
, ~0);
120 int is_submodule_modified(const char *path
)
123 struct child_process cp
;
124 const char *argv
[] = {
130 struct strbuf buf
= STRBUF_INIT
;
132 strbuf_addf(&buf
, "%s/.git/", path
);
133 if (!is_directory(buf
.buf
)) {
134 strbuf_release(&buf
);
135 /* The submodule is not checked out, so it is not modified */
141 strbuf_addf(&buf
, "GIT_WORK_TREE=%s", path
);
142 env
[0] = strbuf_detach(&buf
, NULL
);
143 strbuf_addf(&buf
, "GIT_DIR=%s/.git", path
);
144 env
[1] = strbuf_detach(&buf
, NULL
);
147 memset(&cp
, 0, sizeof(cp
));
149 cp
.env
= (const char *const *)env
;
153 if (start_command(&cp
))
154 die("Could not run git status --porcelain");
156 len
= strbuf_read(&buf
, cp
.out
, 1024);
159 if (finish_command(&cp
))
160 die("git status --porcelain failed");
164 strbuf_release(&buf
);