3aa391be0a236ea1c432dadcd14824c2fe2b44ea
   1#include <sys/types.h>
   2#include <sys/wait.h>
   3
   4#include "cache.h"
   5
   6static const char *pgm = NULL;
   7static const char *arguments[5];
   8
   9static void run_program(void)
  10{
  11        int pid = fork(), status;
  12
  13        if (pid < 0)
  14                die("unable to fork");
  15        if (!pid) {
  16                execlp(pgm, arguments[0],
  17                            arguments[1],
  18                            arguments[2],
  19                            arguments[3],
  20                            arguments[4],
  21                            NULL);
  22                die("unable to execute '%s'", pgm);
  23        }
  24        if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status))
  25                die("merge program failed");
  26}
  27
  28static char *create_temp_file(int stage, unsigned char *sha1)
  29{
  30        static char template[4][50];
  31        char *path = template[stage];
  32        void *buf;
  33        char type[100];
  34        unsigned long size;
  35        int fd;
  36
  37        buf = read_sha1_file(sha1, type, &size);
  38        if (!buf || strcmp(type, "blob"))
  39                die("unable to read blob object %s", sha1_to_hex(sha1));
  40
  41        strcpy(path, ".merge_file_XXXXXX");
  42        fd = mkstemp(path);
  43        if (fd < 0)
  44                die("unable to create temp-file");
  45        if (write(fd, buf, size) != size)
  46                die("unable to write temp-file");
  47        close(fd);
  48        return path;
  49}
  50
  51static int merge_entry(int pos, const char *path)
  52{
  53        int found;
  54        
  55        if (pos >= active_nr)
  56                die("merge-cache: %s not in the cache", path);
  57        arguments[0] = pgm;
  58        arguments[1] = "";
  59        arguments[2] = "";
  60        arguments[3] = "";
  61        arguments[4] = path;
  62        found = 0;
  63        do {
  64                struct cache_entry *ce = active_cache[pos];
  65                int stage = ce_stage(ce);
  66
  67                if (strcmp(ce->name, path))
  68                        break;
  69                found++;
  70                arguments[stage] = create_temp_file(stage, ce->sha1);
  71        } while (++pos < active_nr);
  72        if (!found)
  73                die("merge-cache: %s not in the cache", path);
  74        run_program();
  75        return found;
  76}
  77
  78static void merge_file(const char *path)
  79{
  80        int pos = cache_name_pos(path, strlen(path));
  81
  82        /*
  83         * If it already exists in the cache as stage0, it's
  84         * already merged and there is nothing to do.
  85         */
  86        if (pos < 0)
  87                merge_entry(-pos-1, path);
  88}
  89
  90static void merge_all(void)
  91{
  92        int i;
  93        for (i = 0; i < active_nr; i++) {
  94                struct cache_entry *ce = active_cache[i];
  95                if (!ce_stage(ce))
  96                        continue;
  97                i += merge_entry(i, ce->name)-1;
  98        }
  99}
 100
 101int main(int argc, char **argv)
 102{
 103        int i, force_file = 0;
 104
 105        if (argc < 3)
 106                usage("merge-cache <merge-program> (-a | <filename>*)");
 107
 108        read_cache();
 109
 110        pgm = argv[1];
 111        for (i = 2; i < argc; i++) {
 112                char *arg = argv[i];
 113                if (!force_file && *arg == '-') {
 114                        if (!strcmp(arg, "--")) {
 115                                force_file = 1;
 116                                continue;
 117                        }
 118                        if (!strcmp(arg, "-a")) {
 119                                merge_all();
 120                                continue;
 121                        }
 122                        die("merge-cache: unknown option %s", arg);
 123                }
 124                merge_file(arg);
 125        }
 126        return 0;
 127}