MCPcopy Create free account
hub / github.com/git/git / odb_source_files_write_alternate

Function odb_source_files_write_alternate

odb/source-files.c:211–261  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

209}
210
211static int odb_source_files_write_alternate(struct odb_source *source,
212 const char *alternate)
213{
214 struct lock_file lock = LOCK_INIT;
215 char *path = xstrfmt("%s/%s", source->path, "info/alternates");
216 FILE *in, *out;
217 int found = 0;
218 int ret;
219
220 hold_lock_file_for_update(&lock, path, LOCK_DIE_ON_ERROR);
221 out = fdopen_lock_file(&lock, "w");
222 if (!out) {
223 ret = error_errno(_("unable to fdopen alternates lockfile"));
224 goto out;
225 }
226
227 in = fopen(path, "r");
228 if (in) {
229 struct strbuf line = STRBUF_INIT;
230
231 while (strbuf_getline(&line, in) != EOF) {
232 if (!strcmp(alternate, line.buf)) {
233 found = 1;
234 break;
235 }
236 fprintf_or_die(out, "%s\n", line.buf);
237 }
238
239 strbuf_release(&line);
240 fclose(in);
241 } else if (errno != ENOENT) {
242 ret = error_errno(_("unable to read alternates file"));
243 goto out;
244 }
245
246 if (found) {
247 rollback_lock_file(&lock);
248 } else {
249 fprintf_or_die(out, "%s\n", alternate);
250 if (commit_lock_file(&lock)) {
251 ret = error_errno(_("unable to move new alternates file into place"));
252 goto out;
253 }
254 }
255
256 ret = 0;
257
258out:
259 free(path);
260 return ret;
261}
262
263struct odb_source_files *odb_source_files_new(struct object_database *odb,
264 const char *path,

Callers

nothing calls this directly

Calls 9

xstrfmtFunction · 0.85
fdopen_lock_fileFunction · 0.85
error_errnoFunction · 0.85
strbuf_getlineFunction · 0.85
fprintf_or_dieFunction · 0.85
strbuf_releaseFunction · 0.85
rollback_lock_fileFunction · 0.85
commit_lock_fileFunction · 0.85

Tested by

no test coverage detected