Skip to content

Commit 336108c

Browse files
pyokagangitster
authored andcommitted
builtin-am: support and auto-detect StGit series files
Since c574e68 (git-am foreign patch support: StGIT support, 2009-05-27), git-am.sh is able to read a single StGit series file and, for each StGit patch listed in the file, convert the StGit patch into a RFC2822 mail patch suitable for parsing with git-mailinfo, and queue them in the state directory for applying. Since 15ced75 (git-am foreign patch support: autodetect some patch formats, 2009-05-27), git-am.sh is able to auto-detect StGit series files by checking to see if the file starts with the string: # This series applies on GIT commit Re-implement the above in builtin/am.c. Signed-off-by: Paul Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5ae41c7 commit 336108c

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

builtin/am.c

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ static int str_isspace(const char *str)
8080
enum patch_format {
8181
PATCH_FORMAT_UNKNOWN = 0,
8282
PATCH_FORMAT_MBOX,
83-
PATCH_FORMAT_STGIT
83+
PATCH_FORMAT_STGIT,
84+
PATCH_FORMAT_STGIT_SERIES
8485
};
8586

8687
enum keep_type {
@@ -650,6 +651,11 @@ static int detect_patch_format(const char **paths)
650651
goto done;
651652
}
652653

654+
if (starts_with(l1.buf, "# This series applies on GIT commit")) {
655+
ret = PATCH_FORMAT_STGIT_SERIES;
656+
goto done;
657+
}
658+
653659
strbuf_reset(&l2);
654660
strbuf_getline_crlf(&l2, fp);
655661
strbuf_reset(&l3);
@@ -800,6 +806,53 @@ static int stgit_patch_to_mail(FILE *out, FILE *in, int keep_cr)
800806
return 0;
801807
}
802808

809+
/**
810+
* This function only supports a single StGit series file in `paths`.
811+
*
812+
* Given an StGit series file, converts the StGit patches in the series into
813+
* RFC2822 messages suitable for parsing with git-mailinfo, and queues them in
814+
* the state directory.
815+
*
816+
* Returns 0 on success, -1 on failure.
817+
*/
818+
static int split_mail_stgit_series(struct am_state *state, const char **paths,
819+
int keep_cr)
820+
{
821+
const char *series_dir;
822+
char *series_dir_buf;
823+
FILE *fp;
824+
struct argv_array patches = ARGV_ARRAY_INIT;
825+
struct strbuf sb = STRBUF_INIT;
826+
int ret;
827+
828+
if (!paths[0] || paths[1])
829+
return error(_("Only one StGIT patch series can be applied at once"));
830+
831+
series_dir_buf = xstrdup(*paths);
832+
series_dir = dirname(series_dir_buf);
833+
834+
fp = fopen(*paths, "r");
835+
if (!fp)
836+
return error(_("could not open '%s' for reading: %s"), *paths,
837+
strerror(errno));
838+
839+
while (!strbuf_getline(&sb, fp, '\n')) {
840+
if (*sb.buf == '#')
841+
continue; /* skip comment lines */
842+
843+
argv_array_push(&patches, mkpath("%s/%s", series_dir, sb.buf));
844+
}
845+
846+
fclose(fp);
847+
strbuf_release(&sb);
848+
free(series_dir_buf);
849+
850+
ret = split_mail_conv(stgit_patch_to_mail, state, patches.argv, keep_cr);
851+
852+
argv_array_clear(&patches);
853+
return ret;
854+
}
855+
803856
/**
804857
* Splits a list of files/directories into individual email patches. Each path
805858
* in `paths` must be a file/directory that is formatted according to
@@ -830,6 +883,8 @@ static int split_mail(struct am_state *state, enum patch_format patch_format,
830883
return split_mail_mbox(state, paths, keep_cr);
831884
case PATCH_FORMAT_STGIT:
832885
return split_mail_conv(stgit_patch_to_mail, state, paths, keep_cr);
886+
case PATCH_FORMAT_STGIT_SERIES:
887+
return split_mail_stgit_series(state, paths, keep_cr);
833888
default:
834889
die("BUG: invalid patch_format");
835890
}
@@ -1880,6 +1935,8 @@ static int parse_opt_patchformat(const struct option *opt, const char *arg, int
18801935
*opt_value = PATCH_FORMAT_MBOX;
18811936
else if (!strcmp(arg, "stgit"))
18821937
*opt_value = PATCH_FORMAT_STGIT;
1938+
else if (!strcmp(arg, "stgit-series"))
1939+
*opt_value = PATCH_FORMAT_STGIT_SERIES;
18831940
else
18841941
return error(_("Invalid value for --patch-format: %s"), arg);
18851942
return 0;

0 commit comments

Comments
 (0)