MCPcopy Index your code
hub / github.com/git/git / parse_diff

Function parse_diff

add-patch.c:538–775  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

536}
537
538static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
539{
540 struct strvec args = STRVEC_INIT;
541 struct strbuf *plain = &s->plain, *colored = NULL;
542 struct child_process cp = CHILD_PROCESS_INIT;
543 char *p, *pend, *colored_p = NULL, *colored_pend = NULL, marker = '\0';
544 size_t file_diff_alloc = 0, i, color_arg_index;
545 struct file_diff *file_diff = NULL;
546 struct hunk *hunk = NULL;
547 int res;
548
549 strvec_pushv(&args, s->mode->diff_cmd);
550 if (s->cfg.context != -1)
551 strvec_pushf(&args, "--unified=%i", s->cfg.context);
552 if (s->cfg.interhunkcontext != -1)
553 strvec_pushf(&args, "--inter-hunk-context=%i", s->cfg.interhunkcontext);
554 if (s->cfg.interactive_diff_algorithm)
555 strvec_pushf(&args, "--diff-algorithm=%s", s->cfg.interactive_diff_algorithm);
556 if (s->revision) {
557 struct object_id oid;
558 strvec_push(&args,
559 /* could be on an unborn branch */
560 !strcmp("HEAD", s->revision) &&
561 repo_get_oid(s->r, "HEAD", &oid) ?
562 empty_tree_oid_hex(s->r->hash_algo) : s->revision);
563 }
564 color_arg_index = args.nr;
565 /* Use `--no-color` explicitly, just in case `diff.color = always`. */
566 strvec_pushl(&args, "--no-color", "--ignore-submodules=dirty", "-p",
567 "--", NULL);
568 for (i = 0; i < ps->nr; i++)
569 strvec_push(&args, ps->items[i].original);
570
571 setup_child_process(s, &cp, NULL);
572 strvec_pushv(&cp.args, args.v);
573 res = capture_command(&cp, plain, 0);
574 if (res) {
575 strvec_clear(&args);
576 return error(_("could not parse diff"));
577 }
578 if (!plain->len) {
579 strvec_clear(&args);
580 return 0;
581 }
582 strbuf_complete_line(plain);
583
584 if (want_color_fd(1, s->cfg.use_color_diff)) {
585 struct child_process colored_cp = CHILD_PROCESS_INIT;
586 const char *diff_filter = s->cfg.interactive_diff_filter;
587
588 setup_child_process(s, &colored_cp, NULL);
589 xsnprintf((char *)args.v[color_arg_index], 8, "--color");
590 strvec_pushv(&colored_cp.args, args.v);
591 colored = &s->colored;
592 res = capture_command(&colored_cp, colored, 0);
593 strvec_clear(&args);
594 if (res)
595 return error(_("could not parse colored diff"));

Callers 1

run_add_p_commonFunction · 0.85

Calls 15

strvec_pushvFunction · 0.85
strvec_pushfFunction · 0.85
strvec_pushFunction · 0.85
repo_get_oidFunction · 0.85
empty_tree_oid_hexFunction · 0.85
strvec_pushlFunction · 0.85
setup_child_processFunction · 0.85
capture_commandFunction · 0.85
strvec_clearFunction · 0.85
errorFunction · 0.85
strbuf_complete_lineFunction · 0.85
want_color_fdFunction · 0.85

Tested by

no test coverage detected