| 652 | } |
| 653 | |
| 654 | static char *find_name_common(struct strbuf *root, |
| 655 | const char *line, |
| 656 | const char *def, |
| 657 | int p_value, |
| 658 | const char *end, |
| 659 | int terminate) |
| 660 | { |
| 661 | int len; |
| 662 | const char *start = NULL; |
| 663 | |
| 664 | if (p_value == 0) |
| 665 | start = line; |
| 666 | while (line != end) { |
| 667 | char c = *line; |
| 668 | |
| 669 | if (!end && isspace(c)) { |
| 670 | if (c == '\n') |
| 671 | break; |
| 672 | if (name_terminate(c, terminate)) |
| 673 | break; |
| 674 | } |
| 675 | line++; |
| 676 | if (c == '/' && !--p_value) |
| 677 | start = line; |
| 678 | } |
| 679 | if (!start) |
| 680 | return squash_slash(xstrdup_or_null(def)); |
| 681 | len = line - start; |
| 682 | if (!len) |
| 683 | return squash_slash(xstrdup_or_null(def)); |
| 684 | |
| 685 | /* |
| 686 | * Generally we prefer the shorter name, especially |
| 687 | * if the other one is just a variation of that with |
| 688 | * something else tacked on to the end (ie "file.orig" |
| 689 | * or "file~"). |
| 690 | */ |
| 691 | if (def) { |
| 692 | int deflen = strlen(def); |
| 693 | if (deflen < len && !strncmp(start, def, deflen)) |
| 694 | return squash_slash(xstrdup(def)); |
| 695 | } |
| 696 | |
| 697 | if (root->len) { |
| 698 | char *ret = xstrfmt("%s%.*s", root->buf, len, start); |
| 699 | return squash_slash(ret); |
| 700 | } |
| 701 | |
| 702 | return squash_slash(xmemdupz(start, len)); |
| 703 | } |
| 704 | |
| 705 | static char *find_name(struct strbuf *root, |
| 706 | const char *line, |
no test coverage detected