]>
Commit | Line | Data |
---|---|---|
752c0c24 JS |
1 | #include "cache.h" |
2 | #include "submodule.h" | |
3 | #include "dir.h" | |
4 | #include "diff.h" | |
5 | #include "commit.h" | |
6 | #include "revision.h" | |
ee6fc514 | 7 | #include "run-command.h" |
752c0c24 | 8 | |
cb58c932 | 9 | static int add_submodule_odb(const char *path) |
752c0c24 JS |
10 | { |
11 | struct strbuf objects_directory = STRBUF_INIT; | |
12 | struct alternate_object_database *alt_odb; | |
de7a7960 | 13 | int ret = 0; |
752c0c24 JS |
14 | |
15 | strbuf_addf(&objects_directory, "%s/.git/objects/", path); | |
de7a7960 JL |
16 | if (!is_directory(objects_directory.buf)) { |
17 | ret = -1; | |
18 | goto done; | |
19 | } | |
752c0c24 JS |
20 | /* avoid adding it twice */ |
21 | for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next) | |
22 | if (alt_odb->name - alt_odb->base == objects_directory.len && | |
23 | !strncmp(alt_odb->base, objects_directory.buf, | |
24 | objects_directory.len)) | |
de7a7960 | 25 | goto done; |
752c0c24 JS |
26 | |
27 | alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb)); | |
28 | alt_odb->next = alt_odb_list; | |
29 | strcpy(alt_odb->base, objects_directory.buf); | |
30 | alt_odb->name = alt_odb->base + objects_directory.len; | |
31 | alt_odb->name[2] = '/'; | |
32 | alt_odb->name[40] = '\0'; | |
33 | alt_odb->name[41] = '\0'; | |
34 | alt_odb_list = alt_odb; | |
35 | prepare_alt_odb(); | |
de7a7960 JL |
36 | done: |
37 | strbuf_release(&objects_directory); | |
38 | return ret; | |
752c0c24 JS |
39 | } |
40 | ||
41 | void show_submodule_summary(FILE *f, const char *path, | |
42 | unsigned char one[20], unsigned char two[20], | |
721ceec1 | 43 | unsigned dirty_submodule, |
752c0c24 JS |
44 | const char *del, const char *add, const char *reset) |
45 | { | |
46 | struct rev_info rev; | |
75b9a8a6 | 47 | struct commit *commit, *left = left, *right = right; |
752c0c24 JS |
48 | struct commit_list *merge_bases, *list; |
49 | const char *message = NULL; | |
50 | struct strbuf sb = STRBUF_INIT; | |
51 | static const char *format = " %m %s"; | |
52 | int fast_forward = 0, fast_backward = 0; | |
53 | ||
54 | if (is_null_sha1(two)) | |
55 | message = "(submodule deleted)"; | |
56 | else if (add_submodule_odb(path)) | |
57 | message = "(not checked out)"; | |
58 | else if (is_null_sha1(one)) | |
59 | message = "(new submodule)"; | |
60 | else if (!(left = lookup_commit_reference(one)) || | |
61 | !(right = lookup_commit_reference(two))) | |
62 | message = "(commits not present)"; | |
63 | ||
64 | if (!message) { | |
65 | init_revisions(&rev, NULL); | |
66 | setup_revisions(0, NULL, &rev, NULL); | |
67 | rev.left_right = 1; | |
68 | rev.first_parent_only = 1; | |
69 | left->object.flags |= SYMMETRIC_LEFT; | |
70 | add_pending_object(&rev, &left->object, path); | |
71 | add_pending_object(&rev, &right->object, path); | |
72 | merge_bases = get_merge_bases(left, right, 1); | |
73 | if (merge_bases) { | |
74 | if (merge_bases->item == left) | |
75 | fast_forward = 1; | |
76 | else if (merge_bases->item == right) | |
77 | fast_backward = 1; | |
78 | } | |
79 | for (list = merge_bases; list; list = list->next) { | |
80 | list->item->object.flags |= UNINTERESTING; | |
81 | add_pending_object(&rev, &list->item->object, | |
82 | sha1_to_hex(list->item->object.sha1)); | |
83 | } | |
84 | if (prepare_revision_walk(&rev)) | |
85 | message = "(revision walker failed)"; | |
86 | } | |
87 | ||
88 | strbuf_addf(&sb, "Submodule %s %s..", path, | |
89 | find_unique_abbrev(one, DEFAULT_ABBREV)); | |
90 | if (!fast_backward && !fast_forward) | |
91 | strbuf_addch(&sb, '.'); | |
92 | strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV)); | |
721ceec1 JL |
93 | if (dirty_submodule) |
94 | strbuf_add(&sb, "-dirty", 6); | |
752c0c24 JS |
95 | if (message) |
96 | strbuf_addf(&sb, " %s\n", message); | |
97 | else | |
98 | strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : ""); | |
99 | fwrite(sb.buf, sb.len, 1, f); | |
100 | ||
101 | if (!message) { | |
102 | while ((commit = get_revision(&rev))) { | |
5f809ff5 JH |
103 | struct pretty_print_context ctx = {0}; |
104 | ctx.date_mode = rev.date_mode; | |
752c0c24 JS |
105 | strbuf_setlen(&sb, 0); |
106 | if (commit->object.flags & SYMMETRIC_LEFT) { | |
107 | if (del) | |
108 | strbuf_addstr(&sb, del); | |
109 | } | |
110 | else if (add) | |
111 | strbuf_addstr(&sb, add); | |
5f809ff5 | 112 | format_commit_message(commit, format, &sb, &ctx); |
752c0c24 JS |
113 | if (reset) |
114 | strbuf_addstr(&sb, reset); | |
115 | strbuf_addch(&sb, '\n'); | |
116 | fprintf(f, "%s", sb.buf); | |
117 | } | |
118 | clear_commit_marks(left, ~0); | |
119 | clear_commit_marks(right, ~0); | |
120 | } | |
121 | strbuf_release(&sb); | |
122 | } | |
ee6fc514 JL |
123 | |
124 | int is_submodule_modified(const char *path) | |
125 | { | |
126 | int len; | |
127 | struct child_process cp; | |
128 | const char *argv[] = { | |
129 | "status", | |
130 | "--porcelain", | |
131 | NULL, | |
132 | }; | |
b0883aa6 | 133 | char *env[4]; |
ee6fc514 JL |
134 | struct strbuf buf = STRBUF_INIT; |
135 | ||
136 | strbuf_addf(&buf, "%s/.git/", path); | |
137 | if (!is_directory(buf.buf)) { | |
138 | strbuf_release(&buf); | |
139 | /* The submodule is not checked out, so it is not modified */ | |
140 | return 0; | |
141 | ||
142 | } | |
143 | strbuf_reset(&buf); | |
144 | ||
145 | strbuf_addf(&buf, "GIT_WORK_TREE=%s", path); | |
146 | env[0] = strbuf_detach(&buf, NULL); | |
147 | strbuf_addf(&buf, "GIT_DIR=%s/.git", path); | |
148 | env[1] = strbuf_detach(&buf, NULL); | |
b0883aa6 JH |
149 | strbuf_addf(&buf, "GIT_INDEX_FILE"); |
150 | env[2] = strbuf_detach(&buf, NULL); | |
151 | env[3] = NULL; | |
ee6fc514 JL |
152 | |
153 | memset(&cp, 0, sizeof(cp)); | |
154 | cp.argv = argv; | |
155 | cp.env = (const char *const *)env; | |
156 | cp.git_cmd = 1; | |
157 | cp.no_stdin = 1; | |
158 | cp.out = -1; | |
159 | if (start_command(&cp)) | |
160 | die("Could not run git status --porcelain"); | |
161 | ||
162 | len = strbuf_read(&buf, cp.out, 1024); | |
163 | close(cp.out); | |
164 | ||
165 | if (finish_command(&cp)) | |
166 | die("git status --porcelain failed"); | |
167 | ||
168 | free(env[0]); | |
169 | free(env[1]); | |
b0883aa6 | 170 | free(env[2]); |
ee6fc514 JL |
171 | strbuf_release(&buf); |
172 | return len != 0; | |
173 | } |