Code

Submodules: Use "ignore" settings from .gitmodules too for diff and status
[git.git] / submodule.c
1 #include "cache.h"
2 #include "submodule.h"
3 #include "dir.h"
4 #include "diff.h"
5 #include "commit.h"
6 #include "revision.h"
7 #include "run-command.h"
8 #include "diffcore.h"
9 #include "string-list.h"
11 struct string_list config_name_for_path;
12 struct string_list config_ignore_for_name;
14 static int add_submodule_odb(const char *path)
15 {
16         struct strbuf objects_directory = STRBUF_INIT;
17         struct alternate_object_database *alt_odb;
18         int ret = 0;
19         const char *git_dir;
21         strbuf_addf(&objects_directory, "%s/.git", path);
22         git_dir = read_gitfile_gently(objects_directory.buf);
23         if (git_dir) {
24                 strbuf_reset(&objects_directory);
25                 strbuf_addstr(&objects_directory, git_dir);
26         }
27         strbuf_addstr(&objects_directory, "/objects/");
28         if (!is_directory(objects_directory.buf)) {
29                 ret = -1;
30                 goto done;
31         }
32         /* avoid adding it twice */
33         for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
34                 if (alt_odb->name - alt_odb->base == objects_directory.len &&
35                                 !strncmp(alt_odb->base, objects_directory.buf,
36                                         objects_directory.len))
37                         goto done;
39         alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
40         alt_odb->next = alt_odb_list;
41         strcpy(alt_odb->base, objects_directory.buf);
42         alt_odb->name = alt_odb->base + objects_directory.len;
43         alt_odb->name[2] = '/';
44         alt_odb->name[40] = '\0';
45         alt_odb->name[41] = '\0';
46         alt_odb_list = alt_odb;
47         prepare_alt_odb();
48 done:
49         strbuf_release(&objects_directory);
50         return ret;
51 }
53 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
54                                              const char *path)
55 {
56         struct string_list_item *path_option, *ignore_option;
57         path_option = unsorted_string_list_lookup(&config_name_for_path, path);
58         if (path_option) {
59                 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
60                 if (ignore_option)
61                         handle_ignore_submodules_arg(diffopt, ignore_option->util);
62         }
63 }
65 static int submodule_config(const char *var, const char *value, void *cb)
66 {
67         if (!prefixcmp(var, "submodule."))
68                 return parse_submodule_config_option(var, value);
69         return 0;
70 }
72 void gitmodules_config(void)
73 {
74         const char *work_tree = get_git_work_tree();
75         if (work_tree) {
76                 struct strbuf gitmodules_path = STRBUF_INIT;
77                 strbuf_addstr(&gitmodules_path, work_tree);
78                 strbuf_addstr(&gitmodules_path, "/.gitmodules");
79                 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
80                 strbuf_release(&gitmodules_path);
81         }
82 }
84 int parse_submodule_config_option(const char *var, const char *value)
85 {
86         int len;
87         struct string_list_item *config;
88         struct strbuf submodname = STRBUF_INIT;
90         var += 10;              /* Skip "submodule." */
92         len = strlen(var);
93         if ((len > 5) && !strcmp(var + len - 5, ".path")) {
94                 strbuf_add(&submodname, var, len - 5);
95                 config = unsorted_string_list_lookup(&config_name_for_path, value);
96                 if (config)
97                         free(config->util);
98                 else
99                         config = string_list_append(&config_name_for_path, xstrdup(value));
100                 config->util = strbuf_detach(&submodname, NULL);
101                 strbuf_release(&submodname);
102         } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
103                 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
104                     strcmp(value, "all") && strcmp(value, "none")) {
105                         warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
106                         return 0;
107                 }
109                 strbuf_add(&submodname, var, len - 7);
110                 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
111                 if (config)
112                         free(config->util);
113                 else
114                         config = string_list_append(&config_ignore_for_name,
115                                                     strbuf_detach(&submodname, NULL));
116                 strbuf_release(&submodname);
117                 config->util = xstrdup(value);
118                 return 0;
119         }
120         return 0;
123 void handle_ignore_submodules_arg(struct diff_options *diffopt,
124                                   const char *arg)
126         if (!strcmp(arg, "all"))
127                 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
128         else if (!strcmp(arg, "untracked"))
129                 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
130         else if (!strcmp(arg, "dirty"))
131                 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
132         else if (strcmp(arg, "none"))
133                 die("bad --ignore-submodules argument: %s", arg);
136 void show_submodule_summary(FILE *f, const char *path,
137                 unsigned char one[20], unsigned char two[20],
138                 unsigned dirty_submodule,
139                 const char *del, const char *add, const char *reset)
141         struct rev_info rev;
142         struct commit *commit, *left = left, *right = right;
143         struct commit_list *merge_bases, *list;
144         const char *message = NULL;
145         struct strbuf sb = STRBUF_INIT;
146         static const char *format = "  %m %s";
147         int fast_forward = 0, fast_backward = 0;
149         if (is_null_sha1(two))
150                 message = "(submodule deleted)";
151         else if (add_submodule_odb(path))
152                 message = "(not checked out)";
153         else if (is_null_sha1(one))
154                 message = "(new submodule)";
155         else if (!(left = lookup_commit_reference(one)) ||
156                  !(right = lookup_commit_reference(two)))
157                 message = "(commits not present)";
159         if (!message) {
160                 init_revisions(&rev, NULL);
161                 setup_revisions(0, NULL, &rev, NULL);
162                 rev.left_right = 1;
163                 rev.first_parent_only = 1;
164                 left->object.flags |= SYMMETRIC_LEFT;
165                 add_pending_object(&rev, &left->object, path);
166                 add_pending_object(&rev, &right->object, path);
167                 merge_bases = get_merge_bases(left, right, 1);
168                 if (merge_bases) {
169                         if (merge_bases->item == left)
170                                 fast_forward = 1;
171                         else if (merge_bases->item == right)
172                                 fast_backward = 1;
173                 }
174                 for (list = merge_bases; list; list = list->next) {
175                         list->item->object.flags |= UNINTERESTING;
176                         add_pending_object(&rev, &list->item->object,
177                                 sha1_to_hex(list->item->object.sha1));
178                 }
179                 if (prepare_revision_walk(&rev))
180                         message = "(revision walker failed)";
181         }
183         if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
184                 fprintf(f, "Submodule %s contains untracked content\n", path);
185         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
186                 fprintf(f, "Submodule %s contains modified content\n", path);
188         if (!hashcmp(one, two)) {
189                 strbuf_release(&sb);
190                 return;
191         }
193         strbuf_addf(&sb, "Submodule %s %s..", path,
194                         find_unique_abbrev(one, DEFAULT_ABBREV));
195         if (!fast_backward && !fast_forward)
196                 strbuf_addch(&sb, '.');
197         strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
198         if (message)
199                 strbuf_addf(&sb, " %s\n", message);
200         else
201                 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
202         fwrite(sb.buf, sb.len, 1, f);
204         if (!message) {
205                 while ((commit = get_revision(&rev))) {
206                         struct pretty_print_context ctx = {0};
207                         ctx.date_mode = rev.date_mode;
208                         strbuf_setlen(&sb, 0);
209                         if (commit->object.flags & SYMMETRIC_LEFT) {
210                                 if (del)
211                                         strbuf_addstr(&sb, del);
212                         }
213                         else if (add)
214                                 strbuf_addstr(&sb, add);
215                         format_commit_message(commit, format, &sb, &ctx);
216                         if (reset)
217                                 strbuf_addstr(&sb, reset);
218                         strbuf_addch(&sb, '\n');
219                         fprintf(f, "%s", sb.buf);
220                 }
221                 clear_commit_marks(left, ~0);
222                 clear_commit_marks(right, ~0);
223         }
224         strbuf_release(&sb);
227 unsigned is_submodule_modified(const char *path, int ignore_untracked)
229         ssize_t len;
230         struct child_process cp;
231         const char *argv[] = {
232                 "status",
233                 "--porcelain",
234                 NULL,
235                 NULL,
236         };
237         struct strbuf buf = STRBUF_INIT;
238         unsigned dirty_submodule = 0;
239         const char *line, *next_line;
240         const char *git_dir;
242         strbuf_addf(&buf, "%s/.git", path);
243         git_dir = read_gitfile_gently(buf.buf);
244         if (!git_dir)
245                 git_dir = buf.buf;
246         if (!is_directory(git_dir)) {
247                 strbuf_release(&buf);
248                 /* The submodule is not checked out, so it is not modified */
249                 return 0;
251         }
252         strbuf_reset(&buf);
254         if (ignore_untracked)
255                 argv[2] = "-uno";
257         memset(&cp, 0, sizeof(cp));
258         cp.argv = argv;
259         cp.env = local_repo_env;
260         cp.git_cmd = 1;
261         cp.no_stdin = 1;
262         cp.out = -1;
263         cp.dir = path;
264         if (start_command(&cp))
265                 die("Could not run git status --porcelain");
267         len = strbuf_read(&buf, cp.out, 1024);
268         line = buf.buf;
269         while (len > 2) {
270                 if ((line[0] == '?') && (line[1] == '?')) {
271                         dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
272                         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
273                                 break;
274                 } else {
275                         dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
276                         if (ignore_untracked ||
277                             (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
278                                 break;
279                 }
280                 next_line = strchr(line, '\n');
281                 if (!next_line)
282                         break;
283                 next_line++;
284                 len -= (next_line - line);
285                 line = next_line;
286         }
287         close(cp.out);
289         if (finish_command(&cp))
290                 die("git status --porcelain failed");
292         strbuf_release(&buf);
293         return dirty_submodule;