Don't add parents to the commit list if we have already
[git/jnareb-git.git] / rpull.c
blobc27af2c2464de28732b8ad1fff3ed8a0804250d6
1 #include <fcntl.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include "cache.h"
6 #include "commit.h"
7 #include <errno.h>
8 #include <stdio.h>
9 #include "rsh.h"
11 static int tree = 0;
12 static int commits = 0;
13 static int all = 0;
15 static int fd_in;
16 static int fd_out;
18 static int fetch(unsigned char *sha1)
20 if (has_sha1_file(sha1))
21 return 0;
22 write(fd_out, sha1, 20);
23 return write_sha1_from_fd(sha1, fd_in);
26 static int process_tree(unsigned char *sha1)
28 struct tree *tree = lookup_tree(sha1);
29 struct tree_entry_list *entries;
31 if (parse_tree(tree))
32 return -1;
34 for (entries = tree->entries; entries; entries = entries->next) {
36 fprintf(stderr, "Tree %s ", sha1_to_hex(sha1));
37 fprintf(stderr, "needs %s\n",
38 sha1_to_hex(entries->item.tree->object.sha1));
40 if (fetch(entries->item.tree->object.sha1)) {
41 return error("Missing item %s",
42 sha1_to_hex(entries->item.tree->object.sha1));
44 if (entries->directory) {
45 if (process_tree(entries->item.tree->object.sha1))
46 return -1;
49 return 0;
52 static int process_commit(unsigned char *sha1)
54 struct commit *obj = lookup_commit(sha1);
56 if (fetch(sha1)) {
57 return error("Fetching %s", sha1_to_hex(sha1));
60 if (parse_commit(obj))
61 return -1;
63 if (tree) {
64 if (fetch(obj->tree->object.sha1))
65 return -1;
66 if (process_tree(obj->tree->object.sha1))
67 return -1;
68 if (!all)
69 tree = 0;
71 if (commits) {
72 struct commit_list *parents = obj->parents;
73 for (; parents; parents = parents->next) {
74 if (has_sha1_file(parents->item->object.sha1))
75 continue;
76 if (fetch(parents->item->object.sha1)) {
77 /* The server might not have it, and
78 * we don't mind.
80 error("Missing tree %s; continuing",
81 sha1_to_hex(parents->item->object.sha1));
82 continue;
84 if (process_commit(parents->item->object.sha1))
85 return -1;
88 return 0;
91 int main(int argc, char **argv)
93 char *commit_id;
94 char *url;
95 int arg = 1;
96 unsigned char sha1[20];
98 while (arg < argc && argv[arg][0] == '-') {
99 if (argv[arg][1] == 't') {
100 tree = 1;
101 } else if (argv[arg][1] == 'c') {
102 commits = 1;
103 } else if (argv[arg][1] == 'a') {
104 all = 1;
105 tree = 1;
106 commits = 1;
108 arg++;
110 if (argc < arg + 2) {
111 usage("rpull [-c] [-t] [-a] commit-id url");
112 return 1;
114 commit_id = argv[arg];
115 url = argv[arg + 1];
117 if (setup_connection(&fd_in, &fd_out, "rpush", url, arg, argv + 1))
118 return 1;
120 get_sha1_hex(commit_id, sha1);
122 if (fetch(sha1))
123 return 1;
124 if (process_commit(sha1))
125 return 1;
127 return 0;