7 #include "run-command.h"
10 static int add_submodule_odb(const char *path
)
12 struct strbuf objects_directory
= STRBUF_INIT
;
13 struct alternate_object_database
*alt_odb
;
17 strbuf_addf(&objects_directory
, "%s/.git", path
);
18 git_dir
= read_gitfile_gently(objects_directory
.buf
);
20 strbuf_reset(&objects_directory
);
21 strbuf_addstr(&objects_directory
, git_dir
);
23 strbuf_addstr(&objects_directory
, "/objects/");
24 if (!is_directory(objects_directory
.buf
)) {
28 /* avoid adding it twice */
29 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
30 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
31 !strncmp(alt_odb
->base
, objects_directory
.buf
,
32 objects_directory
.len
))
35 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
36 alt_odb
->next
= alt_odb_list
;
37 strcpy(alt_odb
->base
, objects_directory
.buf
);
38 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
39 alt_odb
->name
[2] = '/';
40 alt_odb
->name
[40] = '\0';
41 alt_odb
->name
[41] = '\0';
42 alt_odb_list
= alt_odb
;
45 strbuf_release(&objects_directory
);
49 void show_submodule_summary(FILE *f
, const char *path
,
50 unsigned char one
[20], unsigned char two
[20],
51 unsigned dirty_submodule
,
52 const char *del
, const char *add
, const char *reset
)
55 struct commit
*commit
, *left
= left
, *right
= right
;
56 struct commit_list
*merge_bases
, *list
;
57 const char *message
= NULL
;
58 struct strbuf sb
= STRBUF_INIT
;
59 static const char *format
= " %m %s";
60 int fast_forward
= 0, fast_backward
= 0;
62 if (is_null_sha1(two
))
63 message
= "(submodule deleted)";
64 else if (add_submodule_odb(path
))
65 message
= "(not checked out)";
66 else if (is_null_sha1(one
))
67 message
= "(new submodule)";
68 else if (!(left
= lookup_commit_reference(one
)) ||
69 !(right
= lookup_commit_reference(two
)))
70 message
= "(commits not present)";
73 init_revisions(&rev
, NULL
);
74 setup_revisions(0, NULL
, &rev
, NULL
);
76 rev
.first_parent_only
= 1;
77 left
->object
.flags
|= SYMMETRIC_LEFT
;
78 add_pending_object(&rev
, &left
->object
, path
);
79 add_pending_object(&rev
, &right
->object
, path
);
80 merge_bases
= get_merge_bases(left
, right
, 1);
82 if (merge_bases
->item
== left
)
84 else if (merge_bases
->item
== right
)
87 for (list
= merge_bases
; list
; list
= list
->next
) {
88 list
->item
->object
.flags
|= UNINTERESTING
;
89 add_pending_object(&rev
, &list
->item
->object
,
90 sha1_to_hex(list
->item
->object
.sha1
));
92 if (prepare_revision_walk(&rev
))
93 message
= "(revision walker failed)";
96 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
97 fprintf(f
, "Submodule %s contains untracked content\n", path
);
98 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
99 fprintf(f
, "Submodule %s contains modified content\n", path
);
101 if (!hashcmp(one
, two
)) {
106 strbuf_addf(&sb
, "Submodule %s %s..", path
,
107 find_unique_abbrev(one
, DEFAULT_ABBREV
));
108 if (!fast_backward
&& !fast_forward
)
109 strbuf_addch(&sb
, '.');
110 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
112 strbuf_addf(&sb
, " %s\n", message
);
114 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
115 fwrite(sb
.buf
, sb
.len
, 1, f
);
118 while ((commit
= get_revision(&rev
))) {
119 struct pretty_print_context ctx
= {0};
120 ctx
.date_mode
= rev
.date_mode
;
121 strbuf_setlen(&sb
, 0);
122 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
124 strbuf_addstr(&sb
, del
);
127 strbuf_addstr(&sb
, add
);
128 format_commit_message(commit
, format
, &sb
, &ctx
);
130 strbuf_addstr(&sb
, reset
);
131 strbuf_addch(&sb
, '\n');
132 fprintf(f
, "%s", sb
.buf
);
134 clear_commit_marks(left
, ~0);
135 clear_commit_marks(right
, ~0);
140 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
143 struct child_process cp
;
144 const char *argv
[] = {
150 struct strbuf buf
= STRBUF_INIT
;
151 unsigned dirty_submodule
= 0;
152 const char *line
, *next_line
;
155 strbuf_addf(&buf
, "%s/.git", path
);
156 git_dir
= read_gitfile_gently(buf
.buf
);
159 if (!is_directory(git_dir
)) {
160 strbuf_release(&buf
);
161 /* The submodule is not checked out, so it is not modified */
167 if (ignore_untracked
)
170 memset(&cp
, 0, sizeof(cp
));
172 cp
.env
= local_repo_env
;
177 if (start_command(&cp
))
178 die("Could not run git status --porcelain");
180 len
= strbuf_read(&buf
, cp
.out
, 1024);
183 if ((line
[0] == '?') && (line
[1] == '?')) {
184 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
185 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
188 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
189 if (ignore_untracked
||
190 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
193 next_line
= strchr(line
, '\n');
197 len
-= (next_line
- line
);
202 if (finish_command(&cp
))
203 die("git status --porcelain failed");
205 strbuf_release(&buf
);
206 return dirty_submodule
;