int git_config_parse_parameter(const char *text) { struct config_item *ct; struct strbuf tmp = STRBUF_INIT; struct strbuf **pair; strbuf_addstr(&tmp, text); pair = strbuf_split(&tmp, '='); if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') strbuf_setlen(pair[0], pair[0]->len - 1); strbuf_trim(pair[0]); if (!pair[0]->len) { strbuf_list_free(pair); return -1; } ct = xcalloc(1, sizeof(struct config_item)); ct->name = strbuf_detach(pair[0], NULL); if (pair[1]) { strbuf_trim(pair[1]); ct->value = strbuf_detach(pair[1], NULL); } strbuf_list_free(pair); lowercase(ct->name); *config_parameters_tail = ct; config_parameters_tail = &ct->next; return 0; }
static int notes_copy_from_stdin(int force, const char *rewrite_cmd) { struct strbuf buf = STRBUF_INIT; struct notes_rewrite_cfg *c = NULL; struct notes_tree *t = NULL; int ret = 0; const char *msg = "Notes added by 'git notes copy'"; if (rewrite_cmd) { c = init_copy_notes_for_rewrite(rewrite_cmd); if (!c) return 0; } else { init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE); t = &default_notes_tree; } while (strbuf_getline_lf(&buf, stdin) != EOF) { struct object_id from_obj, to_obj; struct strbuf **split; int err; split = strbuf_split(&buf, ' '); if (!split[0] || !split[1]) die(_("malformed input line: '%s'."), buf.buf); strbuf_rtrim(split[0]); strbuf_rtrim(split[1]); if (get_oid(split[0]->buf, &from_obj)) die(_("failed to resolve '%s' as a valid ref."), split[0]->buf); if (get_oid(split[1]->buf, &to_obj)) die(_("failed to resolve '%s' as a valid ref."), split[1]->buf); if (rewrite_cmd) err = copy_note_for_rewrite(c, &from_obj, &to_obj); else err = copy_note(t, &from_obj, &to_obj, force, combine_notes_overwrite); if (err) { error(_("failed to copy notes from '%s' to '%s'"), split[0]->buf, split[1]->buf); ret = 1; } strbuf_list_free(split); } if (!rewrite_cmd) { commit_notes(t, msg); free_notes(t); } else { finish_copy_notes_for_rewrite(c, msg); } strbuf_release(&buf); return ret; }
int notes_copy_from_stdin(int force, const char *rewrite_cmd) { struct strbuf buf = STRBUF_INIT; struct notes_rewrite_cfg *c = NULL; struct notes_tree *t = NULL; int ret = 0; if (rewrite_cmd) { c = init_copy_notes_for_rewrite(rewrite_cmd); if (!c) return 0; } else { init_notes(NULL, NULL, NULL, 0); t = &default_notes_tree; } while (strbuf_getline(&buf, stdin, '\n') != EOF) { unsigned char from_obj[20], to_obj[20]; struct strbuf **split; int err; split = strbuf_split(&buf, ' '); if (!split[0] || !split[1]) die("Malformed input line: '%s'.", buf.buf); strbuf_rtrim(split[0]); strbuf_rtrim(split[1]); if (get_sha1(split[0]->buf, from_obj)) die("Failed to resolve '%s' as a valid ref.", split[0]->buf); if (get_sha1(split[1]->buf, to_obj)) die("Failed to resolve '%s' as a valid ref.", split[1]->buf); if (rewrite_cmd) err = copy_note_for_rewrite(c, from_obj, to_obj); else err = copy_note(t, from_obj, to_obj, force, combine_notes_overwrite); if (err) { error("Failed to copy notes from '%s' to '%s'", split[0]->buf, split[1]->buf); ret = 1; } strbuf_list_free(split); } if (!rewrite_cmd) { commit_notes(t, "Notes added by 'git notes copy'"); free_notes(t); } else { finish_copy_notes_for_rewrite(c); } return ret; }
static BOOL build_branch_menu(struct git_data *data, const struct menu_item *item, void *platform) { void *submenu; int status; char *wd = wd_from_path(data->name, NULL); struct strbuf output; struct strbuf **lines, **it; strbuf_init(&output, 0); status = exec_program(wd, &output, NULL, WAITMODE, "git", "branch", NULL); free(wd); if (status) return FALSE; submenu = start_submenu(data, item, platform); lines = strbuf_split(&output, '\n'); for (it = lines; *it; it++) { struct menu_item item = { MENU_ITEM_CLEANUP, 0, NULL, NULL, NULL, menu_branch }; strbuf_rtrim(*it); item.string = strdup((*it)->buf + 2); item.helptext = strdup((*it)->buf + 2); item.flags = '*' == (*it)->buf[0] ? MI_CHECKED | MI_DISABLED : 0; if (build_item(data, &item, submenu)) append_active_menu(&item); else /* * if the platform failed to create an item * there is no point to try other items */ break; } end_submenu(platform, submenu); /* technically, there is nothing to track for the menu engine */ return FALSE; }
void string_list_add_refs_from_colon_sep(struct string_list *list, const char *globs) { struct strbuf globbuf = STRBUF_INIT; struct strbuf **split; int i; strbuf_addstr(&globbuf, globs); split = strbuf_split(&globbuf, ':'); for (i = 0; split[i]; i++) { if (!split[i]->len) continue; if (split[i]->buf[split[i]->len-1] == ':') strbuf_setlen(split[i], split[i]->len-1); string_list_add_refs_by_glob(list, split[i]->buf); } strbuf_list_free(split); strbuf_release(&globbuf); }
static int string_list_add_note_lines(struct string_list *sort_uniq_list, const unsigned char *sha1) { char *data; unsigned long len; enum object_type t; struct strbuf buf = STRBUF_INIT; struct strbuf **lines = NULL; int i, list_index; if (is_null_sha1(sha1)) return 0; /* read_sha1_file NUL-terminates */ data = read_sha1_file(sha1, &t, &len); if (t != OBJ_BLOB || !data || !len) { free(data); return t != OBJ_BLOB || !data; } strbuf_attach(&buf, data, len, len + 1); lines = strbuf_split(&buf, '\n'); for (i = 0; lines[i]; i++) { if (lines[i]->buf[lines[i]->len - 1] == '\n') strbuf_setlen(lines[i], lines[i]->len - 1); if (!lines[i]->len) continue; /* skip empty lines */ list_index = string_list_find_insert_index(sort_uniq_list, lines[i]->buf, 0); if (list_index < 0) continue; /* skip duplicate lines */ string_list_insert_at_index(sort_uniq_list, list_index, lines[i]->buf); } strbuf_list_free(lines); strbuf_release(&buf); return 0; }
int varnam_learn_from_file(varnam *handle, const char *filepath, vlearn_status *status, void (*callback)(varnam *handle, const char *word, int status_code, void *object), void *object) { int rc; FILE *infile; char line_buffer[10000]; strbuf *word; varray *word_parts; int confidence; int parts; infile = fopen(filepath, "r"); if (!infile) { set_last_error (handle, "Couldn't open file '%s' for reading.\n", filepath); return VARNAM_ERROR; } if (status != NULL) { status->total_words = 0; status->failed = 0; } rc = vwt_optimize_for_huge_transaction(handle); if (rc) { fclose (infile); return rc; } /* Learning from file will be mostly new words. Optimizing for that */ v_->_config_mostly_learning_new_words = 1; varnam_log (handle, "Starting to learn from %s", filepath); rc = vwt_start_changes (handle); if (rc) { vwt_turn_off_optimization_for_huge_transaction(handle); fclose (infile); return rc; } while (fgets(line_buffer, sizeof(line_buffer), infile)) { reset_pool (handle); word = get_pooled_string (handle); strbuf_add (word, trimwhitespace (line_buffer)); word_parts = strbuf_split (word, handle, ' '); parts = varray_length (word_parts); if (parts > 0 && parts <= 2) { confidence = 1; if (parts == 2) { word = varray_get (word_parts, 1); confidence = atoi (strbuf_to_s (word)); } word = varray_get (word_parts, 0); rc = varnam_learn_internal (handle, strbuf_to_s (word), confidence); if (rc) { if (status != NULL) status->failed++; } } else { rc = VARNAM_ERROR; if (status != NULL) status->failed++; } if (status != NULL) status->total_words++; if (callback != NULL) callback (handle, strbuf_to_s (word), rc, object); } varnam_log (handle, "Writing changes to disk"); rc = vwt_end_changes (handle); if (rc) { varnam_log (handle, "Writing changes to disk failed"); } varnam_log (handle, "Ensuring file integrity"); rc = vwt_turn_off_optimization_for_huge_transaction(handle); if (rc) { varnam_log (handle, "Failed to check file integrity"); } varnam_log (handle, "Compacting file"); rc = vwt_compact_file (handle); if (rc) return rc; fclose (infile); return rc; }
static void handle_body(void) { struct strbuf prev = STRBUF_INIT; /* Skip up to the first boundary */ if (*content_top) { if (!find_boundary()) goto handle_body_out; } do { /* process any boundary lines */ if (*content_top && is_multipart_boundary(&line)) { /* flush any leftover */ if (prev.len) { handle_filter(&prev); strbuf_reset(&prev); } if (!handle_boundary()) goto handle_body_out; } /* Unwrap transfer encoding */ decode_transfer_encoding(&line); switch (transfer_encoding) { case TE_BASE64: case TE_QP: { struct strbuf **lines, **it, *sb; /* Prepend any previous partial lines */ strbuf_insert(&line, 0, prev.buf, prev.len); strbuf_reset(&prev); /* binary data most likely doesn't have newlines */ if (message_type != TYPE_TEXT) { handle_filter(&line); break; } /* * This is a decoded line that may contain * multiple new lines. Pass only one chunk * at a time to handle_filter() */ lines = strbuf_split(&line, '\n'); for (it = lines; (sb = *it); it++) { if (*(it + 1) == NULL) /* The last line */ if (sb->buf[sb->len - 1] != '\n') { /* Partial line, save it for later. */ strbuf_addbuf(&prev, sb); break; } handle_filter(sb); } /* * The partial chunk is saved in "prev" and will be * appended by the next iteration of read_line_with_nul(). */ strbuf_list_free(lines); break; } default: handle_filter(&line); } } while (!strbuf_getwholeline(&line, fin, '\n')); handle_body_out: strbuf_release(&prev); }