merge-index.con commit gitweb: Cleanup and uniquify error messages (e484a2d)
   1#include <sys/types.h>
   2#include <sys/wait.h>
   3#include <signal.h>
   4
   5#include "cache.h"
   6
   7static const char *pgm = NULL;
   8static const char *arguments[8];
   9static int one_shot, quiet;
  10static int err;
  11
  12static void run_program(void)
  13{
  14        int pid = fork(), status;
  15
  16        if (pid < 0)
  17                die("unable to fork");
  18        if (!pid) {
  19                execlp(pgm, arguments[0],
  20                            arguments[1],
  21                            arguments[2],
  22                            arguments[3],
  23                            arguments[4],
  24                            arguments[5],
  25                            arguments[6],
  26                            arguments[7],
  27                            NULL);
  28                die("unable to execute '%s'", pgm);
  29        }
  30        if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status)) {
  31                if (one_shot) {
  32                        err++;
  33                } else {
  34                        if (!quiet)
  35                                die("merge program failed");
  36                        exit(1);
  37                }
  38        }
  39}
  40
  41static int merge_entry(int pos, const char *path)
  42{
  43        int found;
  44        
  45        if (pos >= active_nr)
  46                die("git-merge-index: %s not in the cache", path);
  47        arguments[0] = pgm;
  48        arguments[1] = "";
  49        arguments[2] = "";
  50        arguments[3] = "";
  51        arguments[4] = path;
  52        arguments[5] = "";
  53        arguments[6] = "";
  54        arguments[7] = "";
  55        found = 0;
  56        do {
  57                static char hexbuf[4][60];
  58                static char ownbuf[4][60];
  59                struct cache_entry *ce = active_cache[pos];
  60                int stage = ce_stage(ce);
  61
  62                if (strcmp(ce->name, path))
  63                        break;
  64                found++;
  65                strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
  66                sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode) & (~S_IFMT));
  67                arguments[stage] = hexbuf[stage];
  68                arguments[stage + 4] = ownbuf[stage];
  69        } while (++pos < active_nr);
  70        if (!found)
  71                die("git-merge-index: %s not in the cache", path);
  72        run_program();
  73        return found;
  74}
  75
  76static void merge_file(const char *path)
  77{
  78        int pos = cache_name_pos(path, strlen(path));
  79
  80        /*
  81         * If it already exists in the cache as stage0, it's
  82         * already merged and there is nothing to do.
  83         */
  84        if (pos < 0)
  85                merge_entry(-pos-1, path);
  86}
  87
  88static void merge_all(void)
  89{
  90        int i;
  91        for (i = 0; i < active_nr; i++) {
  92                struct cache_entry *ce = active_cache[i];
  93                if (!ce_stage(ce))
  94                        continue;
  95                i += merge_entry(i, ce->name)-1;
  96        }
  97}
  98
  99int main(int argc, char **argv)
 100{
 101        int i, force_file = 0;
 102
 103        /* Without this we cannot rely on waitpid() to tell
 104         * what happened to our children.
 105         */
 106        signal(SIGCHLD, SIG_DFL);
 107
 108        if (argc < 3)
 109                usage("git-merge-index [-o] [-q] <merge-program> (-a | <filename>*)");
 110
 111        setup_git_directory();
 112        read_cache();
 113
 114        i = 1;
 115        if (!strcmp(argv[i], "-o")) {
 116                one_shot = 1;
 117                i++;
 118        }
 119        if (!strcmp(argv[i], "-q")) {
 120                quiet = 1;
 121                i++;
 122        }
 123        pgm = argv[i++];
 124        for (; i < argc; i++) {
 125                char *arg = argv[i];
 126                if (!force_file && *arg == '-') {
 127                        if (!strcmp(arg, "--")) {
 128                                force_file = 1;
 129                                continue;
 130                        }
 131                        if (!strcmp(arg, "-a")) {
 132                                merge_all();
 133                                continue;
 134                        }
 135                        die("git-merge-index: unknown option %s", arg);
 136                }
 137                merge_file(arg);
 138        }
 139        if (err && !quiet)
 140                die("merge program failed");
 141        return err;
 142}