pull.con commit Merge with gitk. (89ab859)
   1#include "pull.h"
   2
   3#include "cache.h"
   4#include "commit.h"
   5#include "tree.h"
   6#include "tag.h"
   7#include "blob.h"
   8#include "refs.h"
   9
  10const char *write_ref = NULL;
  11
  12const unsigned char *current_ref = NULL;
  13
  14int get_tree = 0;
  15int get_history = 0;
  16int get_all = 0;
  17int get_verbosely = 0;
  18static unsigned char current_commit_sha1[20];
  19
  20static const char commitS[] = "commit";
  21static const char treeS[] = "tree";
  22static const char blobS[] = "blob";
  23
  24void pull_say(const char *fmt, const char *hex) {
  25        if (get_verbosely)
  26                fprintf(stderr, fmt, hex);
  27}
  28
  29static void report_missing(const char *what, const unsigned char *missing)
  30{
  31        char missing_hex[41];
  32
  33        strcpy(missing_hex, sha1_to_hex(missing));;
  34        fprintf(stderr,
  35                "Cannot obtain needed %s %s\nwhile processing commit %s.\n",
  36                what, missing_hex, sha1_to_hex(current_commit_sha1));
  37}
  38
  39static int make_sure_we_have_it(const char *what, unsigned char *sha1)
  40{
  41        int status = 0;
  42
  43        if (!has_sha1_file(sha1)) {
  44                status = fetch(sha1);
  45                if (status && what)
  46                        report_missing(what, sha1);
  47        }
  48        return status;
  49}
  50
  51static int process_unknown(unsigned char *sha1);
  52
  53static int process_tree(unsigned char *sha1)
  54{
  55        struct tree *tree = lookup_tree(sha1);
  56        struct tree_entry_list *entries;
  57
  58        if (parse_tree(tree))
  59                return -1;
  60
  61        for (entries = tree->entries; entries; entries = entries->next) {
  62                const char *what = entries->directory ? treeS : blobS;
  63                if (make_sure_we_have_it(what, entries->item.tree->object.sha1))
  64                        return -1;
  65                if (entries->directory) {
  66                        if (process_tree(entries->item.tree->object.sha1))
  67                                return -1;
  68                }
  69        }
  70        return 0;
  71}
  72
  73static int process_commit(unsigned char *sha1)
  74{
  75        struct commit *obj = lookup_commit(sha1);
  76
  77        if (make_sure_we_have_it(commitS, sha1))
  78                return -1;
  79
  80        if (parse_commit(obj))
  81                return -1;
  82
  83        if (get_tree) {
  84                if (make_sure_we_have_it(treeS, obj->tree->object.sha1))
  85                        return -1;
  86                if (process_tree(obj->tree->object.sha1))
  87                        return -1;
  88                if (!get_all)
  89                        get_tree = 0;
  90        }
  91        if (get_history) {
  92                struct commit_list *parents = obj->parents;
  93                for (; parents; parents = parents->next) {
  94                        if (has_sha1_file(parents->item->object.sha1))
  95                                continue;
  96                        if (make_sure_we_have_it(NULL,
  97                                                 parents->item->object.sha1)) {
  98                                /* The server might not have it, and
  99                                 * we don't mind. 
 100                                 */
 101                                continue;
 102                        }
 103                        if (process_commit(parents->item->object.sha1))
 104                                return -1;
 105                        memcpy(current_commit_sha1, sha1, 20);
 106                }
 107        }
 108        return 0;
 109}
 110
 111static int process_tag(unsigned char *sha1)
 112{
 113        struct tag *obj = lookup_tag(sha1);
 114
 115        if (parse_tag(obj))
 116                return -1;
 117        return process_unknown(obj->tagged->sha1);
 118}
 119
 120static int process_unknown(unsigned char *sha1)
 121{
 122        struct object *obj;
 123        if (make_sure_we_have_it("object", sha1))
 124                return -1;
 125        obj = parse_object(sha1);
 126        if (!obj)
 127                return error("Unable to parse object %s", sha1_to_hex(sha1));
 128        if (obj->type == commit_type)
 129                return process_commit(sha1);
 130        if (obj->type == tree_type)
 131                return process_tree(sha1);
 132        if (obj->type == blob_type)
 133                return 0;
 134        if (obj->type == tag_type)
 135                return process_tag(sha1);
 136        return error("Unable to determine requirement of type %s for %s",
 137                     obj->type, sha1_to_hex(sha1));
 138}
 139
 140static int interpret_target(char *target, unsigned char *sha1)
 141{
 142        if (!get_sha1_hex(target, sha1))
 143                return 0;
 144        if (!check_ref_format(target)) {
 145                if (!fetch_ref(target, sha1)) {
 146                        return 0;
 147                }
 148        }
 149        return -1;
 150}
 151
 152
 153int pull(char *target)
 154{
 155        unsigned char sha1[20];
 156        int fd = -1;
 157
 158        if (write_ref && current_ref) {
 159                fd = lock_ref_sha1(write_ref, current_ref);
 160                if (fd < 0)
 161                        return -1;
 162        }
 163
 164        if (interpret_target(target, sha1))
 165                return error("Could not interpret %s as something to pull",
 166                             target);
 167        if (process_unknown(sha1))
 168                return -1;
 169        
 170        if (write_ref) {
 171                if (current_ref) {
 172                        write_ref_sha1(write_ref, fd, sha1);
 173                } else {
 174                        write_ref_sha1_unlocked(write_ref, sha1);
 175                }
 176        }
 177        return 0;
 178}