Avoid specifying (on Windows unimplemented) child_process.dir
[git/dscho.git] / submodule.c
blob8df17ffe8233505e974bceca02ba78985a304118
1 #include "cache.h"
2 #include "submodule.h"
3 #include "run-command.h"
5 int add_submodule_odb(const char *path)
7 struct strbuf objects_directory = STRBUF_INIT;
8 struct alternate_object_database *alt_odb;
10 strbuf_addf(&objects_directory, "%s/.git/objects/", path);
11 if (!is_directory(objects_directory.buf))
12 return -1;
14 /* avoid adding it twice */
15 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
16 if (alt_odb->name - alt_odb->base == objects_directory.len &&
17 !strncmp(alt_odb->base, objects_directory.buf,
18 objects_directory.len))
19 return 0;
21 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
22 alt_odb->next = alt_odb_list;
23 strcpy(alt_odb->base, objects_directory.buf);
24 alt_odb->name = alt_odb->base + objects_directory.len;
25 alt_odb->name[2] = '/';
26 alt_odb->name[40] = '\0';
27 alt_odb->name[41] = '\0';
28 alt_odb_list = alt_odb;
29 prepare_alt_odb();
30 return 0;
33 void show_submodule_summary(FILE *f, const char *path,
34 unsigned char one[20], unsigned char two[20],
35 const char *del, const char *add, const char *reset)
37 struct strbuf sb = STRBUF_INIT;
38 const char *message = NULL;
40 if (add_submodule_odb(path))
41 message = "(not checked out)";
42 else if (is_null_sha1(one))
43 message = "(new submodule)";
44 else if (is_null_sha1(two))
45 message = "(submodule deleted)";
47 if (!message) {
48 struct child_process child;
49 struct strbuf range = STRBUF_INIT, git_dir = STRBUF_INIT;
50 const char* argv[] = {
51 NULL, "log", "--pretty=format: %m %s", "--left-right",
52 NULL, "--", NULL
55 strbuf_addf(&range, "%s...%s",
56 sha1_to_hex(one), sha1_to_hex(two));
57 strbuf_addf(&git_dir, "--git-dir=%s/.git", path);
58 argv[0] = git_dir.buf;
59 argv[4] = range.buf;
60 memset(&child, 0, sizeof(child));
61 child.argv = argv;
62 child.no_stdin = 1;
63 child.out = dup(fileno(f));
64 child.err = dup(fileno(stderr));
65 child.git_cmd = 1;
67 fprintf(f, "Submodule %s %s:\n", path, range.buf);
68 fflush(f);
69 run_command(&child);
70 fputc('\n', f);
71 strbuf_release(&range);
72 return;
75 strbuf_addf(&sb, "Submodule %s %s...%s %s\n", path,
76 find_unique_abbrev(one, DEFAULT_ABBREV),
77 find_unique_abbrev(two, DEFAULT_ABBREV),
78 message);
79 fprintf(f, "%s", sb.buf);
80 strbuf_release(&sb);