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

Function add_file_cb

archive.c:562–624  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

560}
561
562static int add_file_cb(const struct option *opt, const char *arg, int unset)
563{
564 struct archiver_args *args = opt->value;
565 const char **basep = (const char **)opt->defval;
566 const char *base = *basep;
567 char *path;
568 struct string_list_item *item;
569 struct extra_file_info *info;
570
571 if (unset) {
572 string_list_clear_func(&args->extra_files,
573 extra_file_info_clear);
574 return 0;
575 }
576
577 if (!arg)
578 return -1;
579
580 info = xmalloc(sizeof(*info));
581 info->base = xstrdup_or_null(base);
582
583 if (!strcmp(opt->long_name, "add-file")) {
584 path = prefix_filename(args->prefix, arg);
585 if (stat(path, &info->stat))
586 die(_("File not found: %s"), path);
587 if (!S_ISREG(info->stat.st_mode))
588 die(_("Not a regular file: %s"), path);
589 info->content = NULL; /* read the file later */
590 } else if (!strcmp(opt->long_name, "add-virtual-file")) {
591 struct strbuf buf = STRBUF_INIT;
592 const char *p = arg;
593
594 if (*p != '"')
595 p = strchr(p, ':');
596 else if (unquote_c_style(&buf, p, &p) < 0)
597 die(_("unclosed quote: '%s'"), arg);
598
599 if (!p || *p != ':')
600 die(_("missing colon: '%s'"), arg);
601
602 if (p == arg)
603 die(_("empty file name: '%s'"), arg);
604
605 path = buf.len ?
606 strbuf_detach(&buf, NULL) : xstrndup(arg, p - arg);
607
608 if (args->prefix) {
609 char *save = path;
610 path = prefix_filename(args->prefix, path);
611 free(save);
612 }
613 memset(&info->stat, 0, sizeof(info->stat));
614 info->stat.st_mode = S_IFREG | 0644;
615 info->content = xstrdup(p + 1);
616 info->stat.st_size = strlen(info->content);
617 } else {
618 BUG("add_file_cb() called for %s", opt->long_name);
619 }

Callers

nothing calls this directly

Calls 11

string_list_clear_funcFunction · 0.85
xstrdup_or_nullFunction · 0.85
prefix_filenameFunction · 0.85
unquote_c_styleFunction · 0.85
strbuf_detachFunction · 0.85
xstrndupFunction · 0.85
xstrdupFunction · 0.85
string_list_append_nodupFunction · 0.85
xmallocFunction · 0.70
statClass · 0.70
dieFunction · 0.70

Tested by

no test coverage detected