void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2) { strbuf_grow(sb, sb2->len); memcpy(sb->buf + sb->len, sb2->buf, sb2->len); strbuf_setlen(sb, sb->len + sb2->len); }
static int update_one(struct cache_tree *it, struct cache_entry **cache, int entries, const char *base, int baselen, int *skip_count, int flags) { struct strbuf buffer; int missing_ok = flags & WRITE_TREE_MISSING_OK; int dryrun = flags & WRITE_TREE_DRY_RUN; int repair = flags & WRITE_TREE_REPAIR; int to_invalidate = 0; int i; assert(!(dryrun && repair)); *skip_count = 0; if (0 <= it->entry_count && has_sha1_file(it->oid.hash)) return it->entry_count; /* * We first scan for subtrees and update them; we start by * marking existing subtrees -- the ones that are unmarked * should not be in the result. */ for (i = 0; i < it->subtree_nr; i++) it->down[i]->used = 0; /* * Find the subtrees and update them. */ i = 0; while (i < entries) { const struct cache_entry *ce = cache[i]; struct cache_tree_sub *sub; const char *path, *slash; int pathlen, sublen, subcnt, subskip; path = ce->name; pathlen = ce_namelen(ce); if (pathlen <= baselen || memcmp(base, path, baselen)) break; /* at the end of this level */ slash = strchr(path + baselen, '/'); if (!slash) { i++; continue; } /* * a/bbb/c (base = a/, slash = /c) * ==> * path+baselen = bbb/c, sublen = 3 */ sublen = slash - (path + baselen); sub = find_subtree(it, path + baselen, sublen, 1); if (!sub->cache_tree) sub->cache_tree = cache_tree(); subcnt = update_one(sub->cache_tree, cache + i, entries - i, path, baselen + sublen + 1, &subskip, flags); if (subcnt < 0) return subcnt; if (!subcnt) die("index cache-tree records empty sub-tree"); i += subcnt; sub->count = subcnt; /* to be used in the next loop */ *skip_count += subskip; sub->used = 1; } discard_unused_subtrees(it); /* * Then write out the tree object for this level. */ strbuf_init(&buffer, 8192); i = 0; while (i < entries) { const struct cache_entry *ce = cache[i]; struct cache_tree_sub *sub = NULL; const char *path, *slash; int pathlen, entlen; const unsigned char *sha1; unsigned mode; int expected_missing = 0; int contains_ita = 0; path = ce->name; pathlen = ce_namelen(ce); if (pathlen <= baselen || memcmp(base, path, baselen)) break; /* at the end of this level */ slash = strchr(path + baselen, '/'); if (slash) { entlen = slash - (path + baselen); sub = find_subtree(it, path + baselen, entlen, 0); if (!sub) die("cache-tree.c: '%.*s' in '%s' not found", entlen, path + baselen, path); i += sub->count; sha1 = sub->cache_tree->oid.hash; mode = S_IFDIR; contains_ita = sub->cache_tree->entry_count < 0; if (contains_ita) { to_invalidate = 1; expected_missing = 1; } } else { sha1 = ce->oid.hash; mode = ce->ce_mode; entlen = pathlen - baselen; i++; } if (is_null_sha1(sha1) || (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1))) { strbuf_release(&buffer); if (expected_missing) return -1; return error("invalid object %06o %s for '%.*s'", mode, sha1_to_hex(sha1), entlen+baselen, path); } /* * CE_REMOVE entries are removed before the index is * written to disk. Skip them to remain consistent * with the future on-disk index. */ if (ce->ce_flags & CE_REMOVE) { *skip_count = *skip_count + 1; continue; } /* * CE_INTENT_TO_ADD entries exist on on-disk index but * they are not part of generated trees. Invalidate up * to root to force cache-tree users to read elsewhere. */ if (!sub && ce_intent_to_add(ce)) { to_invalidate = 1; continue; } /* * "sub" can be an empty tree if all subentries are i-t-a. */ if (contains_ita && !hashcmp(sha1, EMPTY_TREE_SHA1_BIN)) continue; strbuf_grow(&buffer, entlen + 100); strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0'); strbuf_add(&buffer, sha1, 20); #if DEBUG_CACHE_TREE fprintf(stderr, "cache-tree update-one %o %.*s\n", mode, entlen, path + baselen); #endif } if (repair) { unsigned char sha1[20]; hash_sha1_file(buffer.buf, buffer.len, tree_type, sha1); if (has_sha1_file(sha1)) hashcpy(it->oid.hash, sha1); else to_invalidate = 1; } else if (dryrun) hash_sha1_file(buffer.buf, buffer.len, tree_type, it->oid.hash); else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->oid.hash)) { strbuf_release(&buffer); return -1; } strbuf_release(&buffer); it->entry_count = to_invalidate ? -1 : i - *skip_count; #if DEBUG_CACHE_TREE fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n", it->entry_count, it->subtree_nr, oid_to_hex(&it->oid)); #endif return i; }
void strbuf_add(struct strbuf *sb, const void *data, size_t len) { strbuf_grow(sb, len); memcpy(sb->buf + sb->len, data, len); strbuf_setlen(sb, sb->len + len); }
void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len) { strbuf_grow(sb, len); memcpy(sb->buf + sb->len, sb->buf + pos, len); strbuf_setlen(sb, sb->len + len); }
void strbuf_addchars(struct strbuf *sb, int c, size_t n) { strbuf_grow(sb, n); memset(sb->buf + sb->len, c, n); strbuf_setlen(sb, sb->len + n); }
static void pp_header(enum cmit_fmt fmt, int abbrev, enum date_mode dmode, const char *encoding, const struct commit *commit, const char **msg_p, struct strbuf *sb) { int parents_shown = 0; for (;;) { const char *line = *msg_p; int linelen = get_one_line(*msg_p); if (!linelen) return; *msg_p += linelen; if (linelen == 1) /* End of header */ return; if (fmt == CMIT_FMT_RAW) { strbuf_add(sb, line, linelen); continue; } if (!memcmp(line, "parent ", 7)) { if (linelen != 48) die("bad parent line in commit"); continue; } if (!parents_shown) { struct commit_list *parent; int num; for (parent = commit->parents, num = 0; parent; parent = parent->next, num++) ; /* with enough slop */ strbuf_grow(sb, num * 50 + 20); add_merge_info(fmt, sb, commit, abbrev); parents_shown = 1; } /* * MEDIUM == DEFAULT shows only author with dates. * FULL shows both authors but not dates. * FULLER shows both authors and dates. */ if (!memcmp(line, "author ", 7)) { strbuf_grow(sb, linelen + 80); pp_user_info("Author", fmt, sb, line + 7, dmode, encoding); } if (!memcmp(line, "committer ", 10) && (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) { strbuf_grow(sb, linelen + 80); pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding); } } }
/* * Give path as relative to prefix. * * The strbuf may or may not be used, so do not assume it contains the * returned path. */ const char *relative_path(const char *in, const char *prefix, struct strbuf *sb) { int in_len = in ? strlen(in) : 0; int prefix_len = prefix ? strlen(prefix) : 0; int in_off = 0; int prefix_off = 0; int i = 0, j = 0; if (!in_len) return "./"; else if (!prefix_len) return in; if (have_same_root(in, prefix)) { /* bypass dos_drive, for "c:" is identical to "C:" */ if (has_dos_drive_prefix(in)) { i = 2; j = 2; } } else { return in; } while (i < prefix_len && j < in_len && prefix[i] == in[j]) { if (is_dir_sep(prefix[i])) { while (is_dir_sep(prefix[i])) i++; while (is_dir_sep(in[j])) j++; prefix_off = i; in_off = j; } else { i++; j++; } } if ( /* "prefix" seems like prefix of "in" */ i >= prefix_len && /* * but "/foo" is not a prefix of "/foobar" * (i.e. prefix not end with '/') */ prefix_off < prefix_len) { if (j >= in_len) { /* in="/a/b", prefix="/a/b" */ in_off = in_len; } else if (is_dir_sep(in[j])) { /* in="/a/b/c", prefix="/a/b" */ while (is_dir_sep(in[j])) j++; in_off = j; } else { /* in="/a/bbb/c", prefix="/a/b" */ i = prefix_off; } } else if ( /* "in" is short than "prefix" */ j >= in_len && /* "in" not end with '/' */ in_off < in_len) { if (is_dir_sep(prefix[i])) { /* in="/a/b", prefix="/a/b/c/" */ while (is_dir_sep(prefix[i])) i++; in_off = in_len; } } in += in_off; in_len -= in_off; if (i >= prefix_len) { if (!in_len) return "./"; else return in; } strbuf_reset(sb); strbuf_grow(sb, in_len); while (i < prefix_len) { if (is_dir_sep(prefix[i])) { strbuf_addstr(sb, "../"); while (is_dir_sep(prefix[i])) i++; continue; } i++; } if (!is_dir_sep(prefix[prefix_len - 1])) strbuf_addstr(sb, "../"); strbuf_addstr(sb, in); return sb->buf; }
static int append_edit(int argc, const char **argv, const char *prefix) { int allow_empty = 0; const char *object_ref; struct notes_tree *t; unsigned char object[20], new_note[20]; const unsigned char *note; char logmsg[100]; const char * const *usage; struct note_data d = { 0, 0, NULL, STRBUF_INIT }; struct option options[] = { { OPTION_CALLBACK, 'm', "message", &d, N_("message"), N_("note contents as a string"), PARSE_OPT_NONEG, parse_msg_arg}, { OPTION_CALLBACK, 'F', "file", &d, N_("file"), N_("note contents in a file"), PARSE_OPT_NONEG, parse_file_arg}, { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"), N_("reuse and edit specified note object"), PARSE_OPT_NONEG, parse_reedit_arg}, { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"), N_("reuse specified note object"), PARSE_OPT_NONEG, parse_reuse_arg}, OPT_BOOL(0, "allow-empty", &allow_empty, N_("allow storing empty note")), OPT_END() }; int edit = !strcmp(argv[0], "edit"); usage = edit ? git_notes_edit_usage : git_notes_append_usage; argc = parse_options(argc, argv, prefix, options, usage, PARSE_OPT_KEEP_ARGV0); if (2 < argc) { error(_("too many parameters")); usage_with_options(usage, options); } if (d.given && edit) fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated " "for the 'edit' subcommand.\n" "Please use 'git notes add -f -m/-F/-c/-C' instead.\n")); object_ref = 1 < argc ? argv[1] : "HEAD"; if (get_sha1(object_ref, object)) die(_("Failed to resolve '%s' as a valid ref."), object_ref); t = init_notes_check(argv[0], NOTES_INIT_WRITABLE); note = get_note(t, object); prepare_note_data(object, &d, edit ? note : NULL); if (note && !edit) { /* Append buf to previous note contents */ unsigned long size; enum object_type type; char *prev_buf = read_sha1_file(note, &type, &size); strbuf_grow(&d.buf, size + 1); if (d.buf.len && prev_buf && size) strbuf_insert(&d.buf, 0, "\n", 1); if (prev_buf && size) strbuf_insert(&d.buf, 0, prev_buf, size); free(prev_buf); } if (d.buf.len || allow_empty) { write_note_data(&d, new_note); if (add_note(t, object, new_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); snprintf(logmsg, sizeof(logmsg), "Notes added by 'git notes %s'", argv[0]); } else { fprintf(stderr, _("Removing note for object %s\n"), sha1_to_hex(object)); remove_note(t, object); snprintf(logmsg, sizeof(logmsg), "Notes removed by 'git notes %s'", argv[0]); } commit_notes(t, logmsg); free_note_data(&d); free_notes(t); return 0; }
static int update_one(struct cache_tree *it, struct cache_entry **cache, int entries, const char *base, int baselen, int missing_ok, int dryrun) { struct strbuf buffer; int i; if (0 <= it->entry_count && has_sha1_file(it->sha1)) return it->entry_count; /* * We first scan for subtrees and update them; we start by * marking existing subtrees -- the ones that are unmarked * should not be in the result. */ for (i = 0; i < it->subtree_nr; i++) it->down[i]->used = 0; /* * Find the subtrees and update them. */ for (i = 0; i < entries; i++) { struct cache_entry *ce = cache[i]; struct cache_tree_sub *sub; const char *path, *slash; int pathlen, sublen, subcnt; path = ce->name; pathlen = ce_namelen(ce); if (pathlen <= baselen || memcmp(base, path, baselen)) break; /* at the end of this level */ slash = strchr(path + baselen, '/'); if (!slash) continue; /* * a/bbb/c (base = a/, slash = /c) * ==> * path+baselen = bbb/c, sublen = 3 */ sublen = slash - (path + baselen); sub = find_subtree(it, path + baselen, sublen, 1); if (!sub->cache_tree) sub->cache_tree = cache_tree(); subcnt = update_one(sub->cache_tree, cache + i, entries - i, path, baselen + sublen + 1, missing_ok, dryrun); if (subcnt < 0) return subcnt; i += subcnt - 1; sub->used = 1; } discard_unused_subtrees(it); /* * Then write out the tree object for this level. */ strbuf_init(&buffer, 8192); for (i = 0; i < entries; i++) { struct cache_entry *ce = cache[i]; struct cache_tree_sub *sub; const char *path, *slash; int pathlen, entlen; const unsigned char *sha1; unsigned mode; path = ce->name; pathlen = ce_namelen(ce); if (pathlen <= baselen || memcmp(base, path, baselen)) break; /* at the end of this level */ slash = strchr(path + baselen, '/'); if (slash) { entlen = slash - (path + baselen); sub = find_subtree(it, path + baselen, entlen, 0); if (!sub) die("cache-tree.c: '%.*s' in '%s' not found", entlen, path + baselen, path); i += sub->cache_tree->entry_count - 1; sha1 = sub->cache_tree->sha1; mode = S_IFDIR; } else { sha1 = ce->sha1; mode = ce->ce_mode; entlen = pathlen - baselen; } if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) { strbuf_release(&buffer); return error("invalid object %06o %s for '%.*s'", mode, sha1_to_hex(sha1), entlen+baselen, path); } if (ce->ce_flags & CE_REMOVE) continue; /* entry being removed */ strbuf_grow(&buffer, entlen + 100); strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0'); strbuf_add(&buffer, sha1, 20); #if DEBUG fprintf(stderr, "cache-tree update-one %o %.*s\n", mode, entlen, path + baselen); #endif } if (dryrun) hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1); else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) { strbuf_release(&buffer); return -1; } strbuf_release(&buffer); it->entry_count = i; #if DEBUG fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n", it->entry_count, it->subtree_nr, sha1_to_hex(it->sha1)); #endif return i; }
void pp_title_line(struct pretty_print_context *pp, const char **msg_p, struct strbuf *sb, const char *encoding, int need_8bit_cte) { static const int max_length = 78; /* per rfc2047 */ struct strbuf title; strbuf_init(&title, 80); *msg_p = format_subject(&title, *msg_p, pp->preserve_subject ? "\n" : " "); strbuf_grow(sb, title.len + 1024); if (pp->print_email_subject) { if (pp->rev) fmt_output_email_subject(sb, pp->rev); if (needs_rfc2047_encoding(title.buf, title.len, RFC2047_SUBJECT)) add_rfc2047(sb, title.buf, title.len, encoding, RFC2047_SUBJECT); else strbuf_add_wrapped_bytes(sb, title.buf, title.len, -last_line_length(sb), 1, max_length); } else { strbuf_addbuf(sb, &title); } strbuf_addch(sb, '\n'); if (need_8bit_cte == 0) { int i; for (i = 0; i < pp->in_body_headers.nr; i++) { if (has_non_ascii(pp->in_body_headers.items[i].string)) { need_8bit_cte = 1; break; } } } if (need_8bit_cte > 0) { const char *header_fmt = "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=%s\n" "Content-Transfer-Encoding: 8bit\n"; strbuf_addf(sb, header_fmt, encoding); } if (pp->after_subject) { strbuf_addstr(sb, pp->after_subject); } if (cmit_fmt_is_mail(pp->fmt)) { strbuf_addch(sb, '\n'); } if (pp->in_body_headers.nr) { int i; for (i = 0; i < pp->in_body_headers.nr; i++) { strbuf_addstr(sb, pp->in_body_headers.items[i].string); free(pp->in_body_headers.items[i].string); } string_list_clear(&pp->in_body_headers, 0); strbuf_addch(sb, '\n'); } strbuf_release(&title); }
void strbuf_init(struct strbuf *sb, size_t hint) { memset(sb, 0, sizeof(*sb)); if (hint) strbuf_grow(sb, hint); }
static void create_note(const unsigned char *object, struct msg_arg *msg, int append_only, const unsigned char *prev, unsigned char *result) { char *path = NULL; if (msg->use_editor || !msg->given) { int fd; /* write the template message before editing: */ path = git_pathdup("NOTES_EDITMSG"); fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); if (fd < 0) die_errno("could not create file '%s'", path); if (msg->given) write_or_die(fd, msg->buf.buf, msg->buf.len); else if (prev && !append_only) write_note_data(fd, prev); write_or_die(fd, note_template, strlen(note_template)); write_commented_object(fd, object); close(fd); strbuf_reset(&(msg->buf)); if (launch_editor(path, &(msg->buf), NULL)) { die("Please supply the note contents using either -m" \ " or -F option"); } stripspace(&(msg->buf), 1); } if (prev && append_only) { /* Append buf to previous note contents */ unsigned long size; enum object_type type; char *prev_buf = read_sha1_file(prev, &type, &size); strbuf_grow(&(msg->buf), size + 1); if (msg->buf.len && prev_buf && size) strbuf_insert(&(msg->buf), 0, "\n", 1); if (prev_buf && size) strbuf_insert(&(msg->buf), 0, prev_buf, size); free(prev_buf); } if (!msg->buf.len) { fprintf(stderr, "Removing note for object %s\n", sha1_to_hex(object)); hashclr(result); } else { if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) { error("unable to write note object"); if (path) error("The note contents has been left in %s", path); exit(128); } } if (path) { unlink_or_warn(path); free(path); } }