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 handle_ignore_submodules_arg(struct diff_options
*diffopt
,
52 if (!strcmp(arg
, "all"))
53 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
54 else if (!strcmp(arg
, "untracked"))
55 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
56 else if (!strcmp(arg
, "dirty"))
57 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
59 die("bad --ignore-submodules argument: %s", arg
);
62 void show_submodule_summary(FILE *f
, const char *path
,
63 unsigned char one
[20], unsigned char two
[20],
64 unsigned dirty_submodule
,
65 const char *del
, const char *add
, const char *reset
)
68 struct commit
*commit
, *left
= left
, *right
= right
;
69 struct commit_list
*merge_bases
, *list
;
70 const char *message
= NULL
;
71 struct strbuf sb
= STRBUF_INIT
;
72 static const char *format
= " %m %s";
73 int fast_forward
= 0, fast_backward
= 0;
75 if (is_null_sha1(two
))
76 message
= "(submodule deleted)";
77 else if (add_submodule_odb(path
))
78 message
= "(not checked out)";
79 else if (is_null_sha1(one
))
80 message
= "(new submodule)";
81 else if (!(left
= lookup_commit_reference(one
)) ||
82 !(right
= lookup_commit_reference(two
)))
83 message
= "(commits not present)";
86 init_revisions(&rev
, NULL
);
87 setup_revisions(0, NULL
, &rev
, NULL
);
89 rev
.first_parent_only
= 1;
90 left
->object
.flags
|= SYMMETRIC_LEFT
;
91 add_pending_object(&rev
, &left
->object
, path
);
92 add_pending_object(&rev
, &right
->object
, path
);
93 merge_bases
= get_merge_bases(left
, right
, 1);
95 if (merge_bases
->item
== left
)
97 else if (merge_bases
->item
== right
)
100 for (list
= merge_bases
; list
; list
= list
->next
) {
101 list
->item
->object
.flags
|= UNINTERESTING
;
102 add_pending_object(&rev
, &list
->item
->object
,
103 sha1_to_hex(list
->item
->object
.sha1
));
105 if (prepare_revision_walk(&rev
))
106 message
= "(revision walker failed)";
109 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
110 fprintf(f
, "Submodule %s contains untracked content\n", path
);
111 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
112 fprintf(f
, "Submodule %s contains modified content\n", path
);
114 if (!hashcmp(one
, two
)) {
119 strbuf_addf(&sb
, "Submodule %s %s..", path
,
120 find_unique_abbrev(one
, DEFAULT_ABBREV
));
121 if (!fast_backward
&& !fast_forward
)
122 strbuf_addch(&sb
, '.');
123 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
125 strbuf_addf(&sb
, " %s\n", message
);
127 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
128 fwrite(sb
.buf
, sb
.len
, 1, f
);
131 while ((commit
= get_revision(&rev
))) {
132 struct pretty_print_context ctx
= {0};
133 ctx
.date_mode
= rev
.date_mode
;
134 strbuf_setlen(&sb
, 0);
135 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
137 strbuf_addstr(&sb
, del
);
140 strbuf_addstr(&sb
, add
);
141 format_commit_message(commit
, format
, &sb
, &ctx
);
143 strbuf_addstr(&sb
, reset
);
144 strbuf_addch(&sb
, '\n');
145 fprintf(f
, "%s", sb
.buf
);
147 clear_commit_marks(left
, ~0);
148 clear_commit_marks(right
, ~0);
153 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
156 struct child_process cp
;
157 const char *argv
[] = {
163 struct strbuf buf
= STRBUF_INIT
;
164 unsigned dirty_submodule
= 0;
165 const char *line
, *next_line
;
168 strbuf_addf(&buf
, "%s/.git", path
);
169 git_dir
= read_gitfile_gently(buf
.buf
);
172 if (!is_directory(git_dir
)) {
173 strbuf_release(&buf
);
174 /* The submodule is not checked out, so it is not modified */
180 if (ignore_untracked
)
183 memset(&cp
, 0, sizeof(cp
));
185 cp
.env
= local_repo_env
;
190 if (start_command(&cp
))
191 die("Could not run git status --porcelain");
193 len
= strbuf_read(&buf
, cp
.out
, 1024);
196 if ((line
[0] == '?') && (line
[1] == '?')) {
197 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
198 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
201 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
202 if (ignore_untracked
||
203 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
206 next_line
= strchr(line
, '\n');
210 len
-= (next_line
- line
);
215 if (finish_command(&cp
))
216 die("git status --porcelain failed");
218 strbuf_release(&buf
);
219 return dirty_submodule
;