2009-10-19 20:38:32 +08:00
|
|
|
#include "cache.h"
|
|
|
|
#include "submodule.h"
|
|
|
|
#include "dir.h"
|
|
|
|
#include "diff.h"
|
|
|
|
#include "commit.h"
|
|
|
|
#include "revision.h"
|
2010-01-17 01:42:24 +08:00
|
|
|
#include "run-command.h"
|
2010-03-05 05:20:33 +08:00
|
|
|
#include "diffcore.h"
|
2009-10-19 20:38:32 +08:00
|
|
|
|
2010-01-12 14:31:58 +08:00
|
|
|
static int add_submodule_odb(const char *path)
|
2009-10-19 20:38:32 +08:00
|
|
|
{
|
|
|
|
struct strbuf objects_directory = STRBUF_INIT;
|
|
|
|
struct alternate_object_database *alt_odb;
|
2010-02-01 00:43:49 +08:00
|
|
|
int ret = 0;
|
2009-10-19 20:38:32 +08:00
|
|
|
|
|
|
|
strbuf_addf(&objects_directory, "%s/.git/objects/", path);
|
2010-02-01 00:43:49 +08:00
|
|
|
if (!is_directory(objects_directory.buf)) {
|
|
|
|
ret = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
2009-10-19 20:38:32 +08:00
|
|
|
/* avoid adding it twice */
|
|
|
|
for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
|
|
|
|
if (alt_odb->name - alt_odb->base == objects_directory.len &&
|
|
|
|
!strncmp(alt_odb->base, objects_directory.buf,
|
|
|
|
objects_directory.len))
|
2010-02-01 00:43:49 +08:00
|
|
|
goto done;
|
2009-10-19 20:38:32 +08:00
|
|
|
|
|
|
|
alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
|
|
|
|
alt_odb->next = alt_odb_list;
|
|
|
|
strcpy(alt_odb->base, objects_directory.buf);
|
|
|
|
alt_odb->name = alt_odb->base + objects_directory.len;
|
|
|
|
alt_odb->name[2] = '/';
|
|
|
|
alt_odb->name[40] = '\0';
|
|
|
|
alt_odb->name[41] = '\0';
|
|
|
|
alt_odb_list = alt_odb;
|
|
|
|
prepare_alt_odb();
|
2010-02-01 00:43:49 +08:00
|
|
|
done:
|
|
|
|
strbuf_release(&objects_directory);
|
|
|
|
return ret;
|
2009-10-19 20:38:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void show_submodule_summary(FILE *f, const char *path,
|
|
|
|
unsigned char one[20], unsigned char two[20],
|
2010-01-24 22:09:00 +08:00
|
|
|
unsigned dirty_submodule,
|
2009-10-19 20:38:32 +08:00
|
|
|
const char *del, const char *add, const char *reset)
|
|
|
|
{
|
|
|
|
struct rev_info rev;
|
2009-11-20 19:33:53 +08:00
|
|
|
struct commit *commit, *left = left, *right = right;
|
2009-10-19 20:38:32 +08:00
|
|
|
struct commit_list *merge_bases, *list;
|
|
|
|
const char *message = NULL;
|
|
|
|
struct strbuf sb = STRBUF_INIT;
|
|
|
|
static const char *format = " %m %s";
|
|
|
|
int fast_forward = 0, fast_backward = 0;
|
|
|
|
|
|
|
|
if (is_null_sha1(two))
|
|
|
|
message = "(submodule deleted)";
|
|
|
|
else if (add_submodule_odb(path))
|
|
|
|
message = "(not checked out)";
|
|
|
|
else if (is_null_sha1(one))
|
|
|
|
message = "(new submodule)";
|
|
|
|
else if (!(left = lookup_commit_reference(one)) ||
|
|
|
|
!(right = lookup_commit_reference(two)))
|
|
|
|
message = "(commits not present)";
|
|
|
|
|
|
|
|
if (!message) {
|
|
|
|
init_revisions(&rev, NULL);
|
|
|
|
setup_revisions(0, NULL, &rev, NULL);
|
|
|
|
rev.left_right = 1;
|
|
|
|
rev.first_parent_only = 1;
|
|
|
|
left->object.flags |= SYMMETRIC_LEFT;
|
|
|
|
add_pending_object(&rev, &left->object, path);
|
|
|
|
add_pending_object(&rev, &right->object, path);
|
|
|
|
merge_bases = get_merge_bases(left, right, 1);
|
|
|
|
if (merge_bases) {
|
|
|
|
if (merge_bases->item == left)
|
|
|
|
fast_forward = 1;
|
|
|
|
else if (merge_bases->item == right)
|
|
|
|
fast_backward = 1;
|
|
|
|
}
|
|
|
|
for (list = merge_bases; list; list = list->next) {
|
|
|
|
list->item->object.flags |= UNINTERESTING;
|
|
|
|
add_pending_object(&rev, &list->item->object,
|
|
|
|
sha1_to_hex(list->item->object.sha1));
|
|
|
|
}
|
|
|
|
if (prepare_revision_walk(&rev))
|
|
|
|
message = "(revision walker failed)";
|
|
|
|
}
|
|
|
|
|
2010-03-05 05:20:33 +08:00
|
|
|
if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
|
|
|
|
fprintf(f, "Submodule %s contains untracked content\n", path);
|
|
|
|
if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
|
|
|
|
fprintf(f, "Submodule %s contains modified content\n", path);
|
|
|
|
|
|
|
|
if (!hashcmp(one, two)) {
|
|
|
|
strbuf_release(&sb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-19 20:38:32 +08:00
|
|
|
strbuf_addf(&sb, "Submodule %s %s..", path,
|
|
|
|
find_unique_abbrev(one, DEFAULT_ABBREV));
|
|
|
|
if (!fast_backward && !fast_forward)
|
|
|
|
strbuf_addch(&sb, '.');
|
|
|
|
strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
|
|
|
|
if (message)
|
|
|
|
strbuf_addf(&sb, " %s\n", message);
|
|
|
|
else
|
|
|
|
strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
|
|
|
|
fwrite(sb.buf, sb.len, 1, f);
|
|
|
|
|
|
|
|
if (!message) {
|
|
|
|
while ((commit = get_revision(&rev))) {
|
2009-10-31 11:18:31 +08:00
|
|
|
struct pretty_print_context ctx = {0};
|
|
|
|
ctx.date_mode = rev.date_mode;
|
2009-10-19 20:38:32 +08:00
|
|
|
strbuf_setlen(&sb, 0);
|
|
|
|
if (commit->object.flags & SYMMETRIC_LEFT) {
|
|
|
|
if (del)
|
|
|
|
strbuf_addstr(&sb, del);
|
|
|
|
}
|
|
|
|
else if (add)
|
|
|
|
strbuf_addstr(&sb, add);
|
2009-10-31 11:18:31 +08:00
|
|
|
format_commit_message(commit, format, &sb, &ctx);
|
2009-10-19 20:38:32 +08:00
|
|
|
if (reset)
|
|
|
|
strbuf_addstr(&sb, reset);
|
|
|
|
strbuf_addch(&sb, '\n');
|
|
|
|
fprintf(f, "%s", sb.buf);
|
|
|
|
}
|
|
|
|
clear_commit_marks(left, ~0);
|
|
|
|
clear_commit_marks(right, ~0);
|
|
|
|
}
|
|
|
|
strbuf_release(&sb);
|
|
|
|
}
|
2010-01-17 01:42:24 +08:00
|
|
|
|
2010-03-14 06:00:27 +08:00
|
|
|
unsigned is_submodule_modified(const char *path, int ignore_untracked)
|
2010-01-17 01:42:24 +08:00
|
|
|
{
|
2010-03-05 05:20:33 +08:00
|
|
|
int i;
|
|
|
|
ssize_t len;
|
2010-01-17 01:42:24 +08:00
|
|
|
struct child_process cp;
|
|
|
|
const char *argv[] = {
|
|
|
|
"status",
|
|
|
|
"--porcelain",
|
|
|
|
NULL,
|
2010-03-14 06:00:27 +08:00
|
|
|
NULL,
|
2010-01-17 01:42:24 +08:00
|
|
|
};
|
2010-02-25 07:34:18 +08:00
|
|
|
const char *env[LOCAL_REPO_ENV_SIZE + 3];
|
2010-01-17 01:42:24 +08:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
2010-03-05 05:20:33 +08:00
|
|
|
unsigned dirty_submodule = 0;
|
|
|
|
const char *line, *next_line;
|
2010-01-17 01:42:24 +08:00
|
|
|
|
2010-02-25 07:34:18 +08:00
|
|
|
for (i = 0; i < LOCAL_REPO_ENV_SIZE; i++)
|
|
|
|
env[i] = local_repo_env[i];
|
|
|
|
|
2010-01-17 01:42:24 +08:00
|
|
|
strbuf_addf(&buf, "%s/.git/", path);
|
|
|
|
if (!is_directory(buf.buf)) {
|
|
|
|
strbuf_release(&buf);
|
|
|
|
/* The submodule is not checked out, so it is not modified */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
|
|
|
|
strbuf_addf(&buf, "GIT_WORK_TREE=%s", path);
|
2010-02-25 07:34:18 +08:00
|
|
|
env[i++] = strbuf_detach(&buf, NULL);
|
2010-01-17 01:42:24 +08:00
|
|
|
strbuf_addf(&buf, "GIT_DIR=%s/.git", path);
|
2010-02-25 07:34:18 +08:00
|
|
|
env[i++] = strbuf_detach(&buf, NULL);
|
|
|
|
env[i] = NULL;
|
2010-01-17 01:42:24 +08:00
|
|
|
|
2010-03-14 06:00:27 +08:00
|
|
|
if (ignore_untracked)
|
|
|
|
argv[2] = "-uno";
|
|
|
|
|
2010-01-17 01:42:24 +08:00
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
cp.argv = argv;
|
2010-02-25 07:34:18 +08:00
|
|
|
cp.env = env;
|
2010-01-17 01:42:24 +08:00
|
|
|
cp.git_cmd = 1;
|
|
|
|
cp.no_stdin = 1;
|
|
|
|
cp.out = -1;
|
|
|
|
if (start_command(&cp))
|
|
|
|
die("Could not run git status --porcelain");
|
|
|
|
|
|
|
|
len = strbuf_read(&buf, cp.out, 1024);
|
2010-03-05 05:20:33 +08:00
|
|
|
line = buf.buf;
|
|
|
|
while (len > 2) {
|
|
|
|
if ((line[0] == '?') && (line[1] == '?')) {
|
|
|
|
dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
|
|
|
|
if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
|
2010-03-14 06:00:27 +08:00
|
|
|
if (ignore_untracked ||
|
|
|
|
(dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
|
2010-03-05 05:20:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
next_line = strchr(line, '\n');
|
|
|
|
if (!next_line)
|
|
|
|
break;
|
|
|
|
next_line++;
|
|
|
|
len -= (next_line - line);
|
|
|
|
line = next_line;
|
|
|
|
}
|
2010-01-17 01:42:24 +08:00
|
|
|
close(cp.out);
|
|
|
|
|
|
|
|
if (finish_command(&cp))
|
|
|
|
die("git status --porcelain failed");
|
|
|
|
|
2010-02-25 07:34:18 +08:00
|
|
|
for (i = LOCAL_REPO_ENV_SIZE; env[i]; i++)
|
|
|
|
free((char *)env[i]);
|
2010-01-17 01:42:24 +08:00
|
|
|
strbuf_release(&buf);
|
2010-03-05 05:20:33 +08:00
|
|
|
return dirty_submodule;
|
2010-01-17 01:42:24 +08:00
|
|
|
}
|