shallow: add setup_temporary_shallow()

This function is like setup_alternate_shallow() except that it does not lock $GIT_DIR/shallow. It is supposed to be used when a program generates temporary shallow for use by another program, then throw the shallow file away. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Aug 16, 2013 at 16:52 UTC 08ea65ad13faaf24f57732cdc8af3d830d4b367f
2 files changed +24
commit.h
+1
@@ -201,6 +201,7 @@ extern void set_alternate_shallow_file(const char *path);
201 extern int write_shallow_commits(struct strbuf *out, int use_pack_protocol);
202 extern void setup_alternate_shallow(struct lock_file *shallow_lock,
203 const char **alternate_shallow_file);
204 +extern char *setup_temporary_shallow(void);
205
206 int is_descendant_of(struct commit *, struct commit_list *);
207 int in_merge_bases(struct commit *, struct commit *);
shallow.c
+23
@@ -175,6 +175,29 @@ int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
175 return data.count;
176 }
177
178 +char *setup_temporary_shallow(void)
179 +{
180 + struct strbuf sb = STRBUF_INIT;
181 + int fd;
182 +
183 + if (write_shallow_commits(&sb, 0)) {
184 + struct strbuf path = STRBUF_INIT;
185 + strbuf_addstr(&path, git_path("shallow_XXXXXX"));
186 + fd = xmkstemp(path.buf);
187 + if (write_in_full(fd, sb.buf, sb.len) != sb.len)
188 + die_errno("failed to write to %s",
189 + path.buf);
190 + close(fd);
191 + strbuf_release(&sb);
192 + return strbuf_detach(&path, NULL);
193 + }
194 + /*
195 + * is_repository_shallow() sees empty string as "no shallow
196 + * file".
197 + */
198 + return xstrdup("");
199 +}
200 +
201 void setup_alternate_shallow(struct lock_file *shallow_lock,
202 const char **alternate_shallow_file)
203 {