notes: check number of parameters to "git notes copy"
[git/jnareb-git.git] / submodule.c
blob676d48fb33119f393befcaf8998ad38741366525
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"
10 static int add_submodule_odb(const char *path)
12 struct strbuf objects_directory = STRBUF_INIT;
13 struct alternate_object_database *alt_odb;
14 int ret = 0;
15 const char *git_dir;
17 strbuf_addf(&objects_directory, "%s/.git", path);
18 git_dir = read_gitfile_gently(objects_directory.buf);
19 if (git_dir) {
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)) {
25 ret = -1;
26 goto done;
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))
33 goto done;
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;
43 prepare_alt_odb();
44 done:
45 strbuf_release(&objects_directory);
46 return ret;
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)
54 struct rev_info rev;
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)";
72 if (!message) {
73 init_revisions(&rev, NULL);
74 setup_revisions(0, NULL, &rev, NULL);
75 rev.left_right = 1;
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);
81 if (merge_bases) {
82 if (merge_bases->item == left)
83 fast_forward = 1;
84 else if (merge_bases->item == right)
85 fast_backward = 1;
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)) {
102 strbuf_release(&sb);
103 return;
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));
111 if (message)
112 strbuf_addf(&sb, " %s\n", message);
113 else
114 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
115 fwrite(sb.buf, sb.len, 1, f);
117 if (!message) {
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) {
123 if (del)
124 strbuf_addstr(&sb, del);
126 else if (add)
127 strbuf_addstr(&sb, add);
128 format_commit_message(commit, format, &sb, &ctx);
129 if (reset)
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);
137 strbuf_release(&sb);
140 unsigned is_submodule_modified(const char *path, int ignore_untracked)
142 ssize_t len;
143 struct child_process cp;
144 const char *argv[] = {
145 "status",
146 "--porcelain",
147 NULL,
148 NULL,
150 struct strbuf buf = STRBUF_INIT;
151 unsigned dirty_submodule = 0;
152 const char *line, *next_line;
153 const char *git_dir;
155 strbuf_addf(&buf, "%s/.git", path);
156 git_dir = read_gitfile_gently(buf.buf);
157 if (!git_dir)
158 git_dir = buf.buf;
159 if (!is_directory(git_dir)) {
160 strbuf_release(&buf);
161 /* The submodule is not checked out, so it is not modified */
162 return 0;
165 strbuf_reset(&buf);
167 if (ignore_untracked)
168 argv[2] = "-uno";
170 memset(&cp, 0, sizeof(cp));
171 cp.argv = argv;
172 cp.env = local_repo_env;
173 cp.git_cmd = 1;
174 cp.no_stdin = 1;
175 cp.out = -1;
176 cp.dir = path;
177 if (start_command(&cp))
178 die("Could not run git status --porcelain");
180 len = strbuf_read(&buf, cp.out, 1024);
181 line = buf.buf;
182 while (len > 2) {
183 if ((line[0] == '?') && (line[1] == '?')) {
184 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
185 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
186 break;
187 } else {
188 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
189 if (ignore_untracked ||
190 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
191 break;
193 next_line = strchr(line, '\n');
194 if (!next_line)
195 break;
196 next_line++;
197 len -= (next_line - line);
198 line = next_line;
200 close(cp.out);
202 if (finish_command(&cp))
203 die("git status --porcelain failed");
205 strbuf_release(&buf);
206 return dirty_submodule;