pull.con commit [PATCH] diff.c: -B argument passing fix. (49d9e85)
   1#include "pull.h"
   2
   3#include "cache.h"
   4#include "commit.h"
   5#include "tree.h"
   6
   7int get_tree = 0;
   8int get_history = 0;
   9int get_delta = 1;
  10int get_all = 0;
  11int get_verbosely = 0;
  12static unsigned char current_commit_sha1[20];
  13
  14static const char commitS[] = "commit";
  15static const char treeS[] = "tree";
  16static const char blobS[] = "blob";
  17
  18void pull_say(const char *fmt, const char *hex) {
  19        if (get_verbosely)
  20                fprintf(stderr, fmt, hex);
  21}
  22
  23static void report_missing(const char *what, const unsigned char *missing)
  24{
  25        char missing_hex[41];
  26
  27        strcpy(missing_hex, sha1_to_hex(missing));;
  28        fprintf(stderr,
  29                "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
  30                what, missing_hex, sha1_to_hex(current_commit_sha1));
  31}
  32
  33static int make_sure_we_have_it(const char *what, unsigned char *sha1)
  34{
  35        int status;
  36        if (has_sha1_file(sha1))
  37                return 0;
  38        status = fetch(sha1);
  39        if (status && what)
  40                report_missing(what, sha1);
  41        if (get_delta) {
  42                char delta_sha1[20];
  43                status = sha1_delta_base(sha1, delta_sha1);
  44                if (0 < status)
  45                        status = make_sure_we_have_it(what, delta_sha1);
  46        }
  47        return status;
  48}
  49
  50static int process_tree(unsigned char *sha1)
  51{
  52        struct tree *tree = lookup_tree(sha1);
  53        struct tree_entry_list *entries;
  54
  55        if (parse_tree(tree))
  56                return -1;
  57
  58        for (entries = tree->entries; entries; entries = entries->next) {
  59                const char *what = entries->directory ? treeS : blobS;
  60                if (make_sure_we_have_it(what, entries->item.tree->object.sha1))
  61                        return -1;
  62                if (entries->directory) {
  63                        if (process_tree(entries->item.tree->object.sha1))
  64                                return -1;
  65                }
  66        }
  67        return 0;
  68}
  69
  70static int process_commit(unsigned char *sha1)
  71{
  72        struct commit *obj = lookup_commit(sha1);
  73
  74        if (make_sure_we_have_it(commitS, sha1))
  75                return -1;
  76
  77        if (parse_commit(obj))
  78                return -1;
  79
  80        if (get_tree) {
  81                if (make_sure_we_have_it(treeS, obj->tree->object.sha1))
  82                        return -1;
  83                if (process_tree(obj->tree->object.sha1))
  84                        return -1;
  85                if (!get_all)
  86                        get_tree = 0;
  87        }
  88        if (get_history) {
  89                struct commit_list *parents = obj->parents;
  90                for (; parents; parents = parents->next) {
  91                        if (has_sha1_file(parents->item->object.sha1))
  92                                continue;
  93                        if (make_sure_we_have_it(NULL,
  94                                                 parents->item->object.sha1)) {
  95                                /* The server might not have it, and
  96                                 * we don't mind. 
  97                                 */
  98                                continue;
  99                        }
 100                        if (process_commit(parents->item->object.sha1))
 101                                return -1;
 102                        memcpy(current_commit_sha1, sha1, 20);
 103                }
 104        }
 105        return 0;
 106}
 107
 108int pull(char *target)
 109{
 110        int retval;
 111        unsigned char sha1[20];
 112        retval = get_sha1_hex(target, sha1);
 113        if (retval)
 114                return retval;
 115        retval = make_sure_we_have_it(commitS, sha1);
 116        if (retval)
 117                return retval;
 118        memcpy(current_commit_sha1, sha1, 20);
 119        return process_commit(sha1);
 120}