44#include "quote.h"
55#include "sigchain.h"
66#include "pkt-line.h"
7+ #include "sub-process.h"
78
89/*
910 * convert.c - convert a file when checking it out and checking it in.
@@ -496,12 +497,6 @@ static int apply_single_file_filter(const char *path, const char *src, size_t le
496497#define CAP_CLEAN (1u<<0)
497498#define CAP_SMUDGE (1u<<1)
498499
499- struct subprocess_entry {
500- struct hashmap_entry ent ; /* must be the first member! */
501- const char * cmd ;
502- struct child_process process ;
503- };
504-
505500struct cmd2process {
506501 struct subprocess_entry subprocess ; /* must be the first member! */
507502 unsigned int supported_capabilities ;
@@ -510,65 +505,6 @@ struct cmd2process {
510505static int subprocess_map_initialized ;
511506static struct hashmap subprocess_map ;
512507
513- static int cmd2process_cmp (const struct subprocess_entry * e1 ,
514- const struct subprocess_entry * e2 ,
515- const void * unused )
516- {
517- return strcmp (e1 -> cmd , e2 -> cmd );
518- }
519-
520- static struct subprocess_entry * subprocess_find_entry (struct hashmap * hashmap , const char * cmd )
521- {
522- struct subprocess_entry key ;
523-
524- hashmap_entry_init (& key , strhash (cmd ));
525- key .cmd = cmd ;
526- return hashmap_get (hashmap , & key , NULL );
527- }
528-
529- static void subprocess_read_status (int fd , struct strbuf * status )
530- {
531- struct strbuf * * pair ;
532- char * line ;
533- for (;;) {
534- line = packet_read_line (fd , NULL );
535- if (!line )
536- break ;
537- pair = strbuf_split_str (line , '=' , 2 );
538- if (pair [0 ] && pair [0 ]-> len && pair [1 ]) {
539- /* the last "status=<foo>" line wins */
540- if (!strcmp (pair [0 ]-> buf , "status=" )) {
541- strbuf_reset (status );
542- strbuf_addbuf (status , pair [1 ]);
543- }
544- }
545- strbuf_list_free (pair );
546- }
547- }
548-
549- static void subprocess_stop (struct hashmap * hashmap , struct subprocess_entry * entry )
550- {
551- if (!entry )
552- return ;
553-
554- entry -> process .clean_on_exit = 0 ;
555- kill (entry -> process .pid , SIGTERM );
556- finish_command (& entry -> process );
557-
558- hashmap_remove (hashmap , entry , NULL );
559- }
560-
561- static void subprocess_exit_handler (struct child_process * process )
562- {
563- sigchain_push (SIGPIPE , SIG_IGN );
564- /* Closing the pipe signals the subprocess to initiate a shutdown. */
565- close (process -> in );
566- close (process -> out );
567- sigchain_pop (SIGPIPE );
568- /* Finish command will wait until the shutdown is complete. */
569- finish_command (process );
570- }
571-
572508static int start_multi_file_filter_fn (struct subprocess_entry * subprocess )
573509{
574510 int err ;
@@ -629,44 +565,6 @@ static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)
629565 return err ;
630566}
631567
632- typedef int (* subprocess_start_fn )(struct subprocess_entry * entry );
633- int subprocess_start (struct hashmap * hashmap , struct subprocess_entry * entry , const char * cmd ,
634- subprocess_start_fn startfn )
635- {
636- int err ;
637- struct child_process * process ;
638- const char * argv [] = { cmd , NULL };
639-
640- entry -> cmd = cmd ;
641- process = & entry -> process ;
642-
643- child_process_init (process );
644- process -> argv = argv ;
645- process -> use_shell = 1 ;
646- process -> in = -1 ;
647- process -> out = -1 ;
648- process -> clean_on_exit = 1 ;
649- process -> clean_on_exit_handler = subprocess_exit_handler ;
650-
651- err = start_command (process );
652- if (err ) {
653- error ("cannot fork to run subprocess '%s'" , cmd );
654- return err ;
655- }
656-
657- hashmap_entry_init (entry , strhash (cmd ));
658-
659- err = startfn (entry );
660- if (err ) {
661- error ("initialization for subprocess '%s' failed" , cmd );
662- subprocess_stop (hashmap , entry );
663- return err ;
664- }
665-
666- hashmap_add (hashmap , entry );
667- return 0 ;
668- }
669-
670568static int apply_multi_file_filter (const char * path , const char * src , size_t len ,
671569 int fd , struct strbuf * dst , const char * cmd ,
672570 const unsigned int wanted_capability )
0 commit comments