7 #include "run-command.h"
10 static int add_submodule_odb(const char *path
)
12 struct strbuf objects_directory
= STRBUF_INIT
;
13 struct alternate_object_database
*alt_odb
;
16 strbuf_addf(&objects_directory
, "%s/.git/objects/", path
);
17 if (!is_directory(objects_directory
.buf
)) {
21 /* avoid adding it twice */
22 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
23 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
24 !strncmp(alt_odb
->base
, objects_directory
.buf
,
25 objects_directory
.len
))
28 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
29 alt_odb
->next
= alt_odb_list
;
30 strcpy(alt_odb
->base
, objects_directory
.buf
);
31 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
32 alt_odb
->name
[2] = '/';
33 alt_odb
->name
[40] = '\0';
34 alt_odb
->name
[41] = '\0';
35 alt_odb_list
= alt_odb
;
38 strbuf_release(&objects_directory
);
42 void show_submodule_summary(FILE *f
, const char *path
,
43 unsigned char one
[20], unsigned char two
[20],
44 unsigned dirty_submodule
,
45 const char *del
, const char *add
, const char *reset
)
48 struct commit
*commit
, *left
= left
, *right
= right
;
49 struct commit_list
*merge_bases
, *list
;
50 const char *message
= NULL
;
51 struct strbuf sb
= STRBUF_INIT
;
52 static const char *format
= " %m %s";
53 int fast_forward
= 0, fast_backward
= 0;
55 if (is_null_sha1(two
))
56 message
= "(submodule deleted)";
57 else if (add_submodule_odb(path
))
58 message
= "(not checked out)";
59 else if (is_null_sha1(one
))
60 message
= "(new submodule)";
61 else if (!(left
= lookup_commit_reference(one
)) ||
62 !(right
= lookup_commit_reference(two
)))
63 message
= "(commits not present)";
66 init_revisions(&rev
, NULL
);
67 setup_revisions(0, NULL
, &rev
, NULL
);
69 rev
.first_parent_only
= 1;
70 left
->object
.flags
|= SYMMETRIC_LEFT
;
71 add_pending_object(&rev
, &left
->object
, path
);
72 add_pending_object(&rev
, &right
->object
, path
);
73 merge_bases
= get_merge_bases(left
, right
, 1);
75 if (merge_bases
->item
== left
)
77 else if (merge_bases
->item
== right
)
80 for (list
= merge_bases
; list
; list
= list
->next
) {
81 list
->item
->object
.flags
|= UNINTERESTING
;
82 add_pending_object(&rev
, &list
->item
->object
,
83 sha1_to_hex(list
->item
->object
.sha1
));
85 if (prepare_revision_walk(&rev
))
86 message
= "(revision walker failed)";
89 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
90 fprintf(f
, "Submodule %s contains untracked content\n", path
);
91 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
92 fprintf(f
, "Submodule %s contains modified content\n", path
);
94 if (!hashcmp(one
, two
)) {
99 strbuf_addf(&sb
, "Submodule %s %s..", path
,
100 find_unique_abbrev(one
, DEFAULT_ABBREV
));
101 if (!fast_backward
&& !fast_forward
)
102 strbuf_addch(&sb
, '.');
103 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
105 strbuf_addf(&sb
, " %s\n", message
);
107 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
108 fwrite(sb
.buf
, sb
.len
, 1, f
);
111 while ((commit
= get_revision(&rev
))) {
112 struct pretty_print_context ctx
= {0};
113 ctx
.date_mode
= rev
.date_mode
;
114 strbuf_setlen(&sb
, 0);
115 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
117 strbuf_addstr(&sb
, del
);
120 strbuf_addstr(&sb
, add
);
121 format_commit_message(commit
, format
, &sb
, &ctx
);
123 strbuf_addstr(&sb
, reset
);
124 strbuf_addch(&sb
, '\n');
125 fprintf(f
, "%s", sb
.buf
);
127 clear_commit_marks(left
, ~0);
128 clear_commit_marks(right
, ~0);
133 unsigned is_submodule_modified(const char *path
)
137 struct child_process cp
;
138 const char *argv
[] = {
143 const char *env
[LOCAL_REPO_ENV_SIZE
+ 3];
144 struct strbuf buf
= STRBUF_INIT
;
145 unsigned dirty_submodule
= 0;
146 const char *line
, *next_line
;
148 for (i
= 0; i
< LOCAL_REPO_ENV_SIZE
; i
++)
149 env
[i
] = local_repo_env
[i
];
151 strbuf_addf(&buf
, "%s/.git/", path
);
152 if (!is_directory(buf
.buf
)) {
153 strbuf_release(&buf
);
154 /* The submodule is not checked out, so it is not modified */
160 strbuf_addf(&buf
, "GIT_WORK_TREE=%s", path
);
161 env
[i
++] = strbuf_detach(&buf
, NULL
);
162 strbuf_addf(&buf
, "GIT_DIR=%s/.git", path
);
163 env
[i
++] = strbuf_detach(&buf
, NULL
);
166 memset(&cp
, 0, sizeof(cp
));
172 if (start_command(&cp
))
173 die("Could not run git status --porcelain");
175 len
= strbuf_read(&buf
, cp
.out
, 1024);
178 if ((line
[0] == '?') && (line
[1] == '?')) {
179 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
180 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
183 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
184 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
187 next_line
= strchr(line
, '\n');
191 len
-= (next_line
- line
);
196 if (finish_command(&cp
))
197 die("git status --porcelain failed");
199 for (i
= LOCAL_REPO_ENV_SIZE
; env
[i
]; i
++)
200 free((char *)env
[i
]);
201 strbuf_release(&buf
);
202 return dirty_submodule
;