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

Function detect_patch_format

builtin/am.c:627–694  ·  view source on GitHub ↗

* Attempts to detect the patch_format of the patches contained in `paths`, * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if * detection fails. */

Source from the content-addressed store, hash-verified

625 * detection fails.
626 */
627static int detect_patch_format(const char **paths)
628{
629 enum patch_format ret = PATCH_FORMAT_UNKNOWN;
630 struct strbuf l1 = STRBUF_INIT;
631 struct strbuf l2 = STRBUF_INIT;
632 struct strbuf l3 = STRBUF_INIT;
633 FILE *fp;
634
635 /*
636 * We default to mbox format if input is from stdin and for directories
637 */
638 if (!*paths || !strcmp(*paths, "-") || is_directory(*paths))
639 return PATCH_FORMAT_MBOX;
640
641 /*
642 * Otherwise, check the first few lines of the first patch, starting
643 * from the first non-blank line, to try to detect its format.
644 */
645
646 fp = xfopen(*paths, "r");
647
648 while (!strbuf_getline(&l1, fp)) {
649 if (l1.len)
650 break;
651 }
652
653 if (starts_with(l1.buf, "From ") || starts_with(l1.buf, "From: ")) {
654 ret = PATCH_FORMAT_MBOX;
655 goto done;
656 }
657
658 if (starts_with(l1.buf, "# This series applies on GIT commit")) {
659 ret = PATCH_FORMAT_STGIT_SERIES;
660 goto done;
661 }
662
663 if (!strcmp(l1.buf, "# HG changeset patch")) {
664 ret = PATCH_FORMAT_HG;
665 goto done;
666 }
667
668 strbuf_getline(&l2, fp);
669 strbuf_getline(&l3, fp);
670
671 /*
672 * If the second line is empty and the third is a From, Author or Date
673 * entry, this is likely an StGit patch.
674 */
675 if (l1.len && !l2.len &&
676 (starts_with(l3.buf, "From:") ||
677 starts_with(l3.buf, "Author:") ||
678 starts_with(l3.buf, "Date:"))) {
679 ret = PATCH_FORMAT_STGIT;
680 goto done;
681 }
682
683 if (l1.len && is_mail(fp)) {
684 ret = PATCH_FORMAT_MBOX;

Callers 1

am_setupFunction · 0.85

Calls 6

is_directoryFunction · 0.85
xfopenFunction · 0.85
strbuf_getlineFunction · 0.85
starts_withFunction · 0.85
is_mailFunction · 0.85
strbuf_releaseFunction · 0.85

Tested by

no test coverage detected