Merge branch 'il/maint-xmallocz'
[git/dkf.git] / submodule.c
blobca0527fbcbdf4838a50956704148744630b65775
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"
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))
16 return -1;
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))
23 return 0;
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;
33 prepare_alt_odb();
34 return 0;
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)
42 struct rev_info rev;
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)";
60 if (!message) {
61 init_revisions(&rev, NULL);
62 setup_revisions(0, NULL, &rev, NULL);
63 rev.left_right = 1;
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);
69 if (merge_bases) {
70 if (merge_bases->item == left)
71 fast_forward = 1;
72 else if (merge_bases->item == right)
73 fast_backward = 1;
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));
89 if (dirty_submodule)
90 strbuf_add(&sb, "-dirty", 6);
91 if (message)
92 strbuf_addf(&sb, " %s\n", message);
93 else
94 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
95 fwrite(sb.buf, sb.len, 1, f);
97 if (!message) {
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) {
103 if (del)
104 strbuf_addstr(&sb, del);
106 else if (add)
107 strbuf_addstr(&sb, add);
108 format_commit_message(commit, format, &sb, &ctx);
109 if (reset)
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);
117 strbuf_release(&sb);
120 int is_submodule_modified(const char *path)
122 int len;
123 struct child_process cp;
124 const char *argv[] = {
125 "status",
126 "--porcelain",
127 NULL,
129 char *env[3];
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 */
136 return 0;
139 strbuf_reset(&buf);
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);
145 env[2] = NULL;
147 memset(&cp, 0, sizeof(cp));
148 cp.argv = argv;
149 cp.env = (const char *const *)env;
150 cp.git_cmd = 1;
151 cp.no_stdin = 1;
152 cp.out = -1;
153 if (start_command(&cp))
154 die("Could not run git status --porcelain");
156 len = strbuf_read(&buf, cp.out, 1024);
157 close(cp.out);
159 if (finish_command(&cp))
160 die("git status --porcelain failed");
162 free(env[0]);
163 free(env[1]);
164 strbuf_release(&buf);
165 return len != 0;