[PATCH] Handle deltified object correctly in git-*-pull family.
[git/kirr.git] / local-pull.c
blobe5d834ff2f7d6949ca2c7dd2424c65f6431a839b
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "cache.h"
5 #include "commit.h"
6 #include "pull.h"
8 static int use_link = 0;
9 static int use_symlink = 0;
10 static int use_filecopy = 1;
12 static char *path;
14 int fetch(unsigned char *sha1)
16 static int object_name_start = -1;
17 static char filename[PATH_MAX];
18 char *hex = sha1_to_hex(sha1);
19 const char *dest_filename = sha1_file_name(sha1);
21 if (object_name_start < 0) {
22 strcpy(filename, path); /* e.g. git.git */
23 strcat(filename, "/objects/");
24 object_name_start = strlen(filename);
26 filename[object_name_start+0] = hex[0];
27 filename[object_name_start+1] = hex[1];
28 filename[object_name_start+2] = '/';
29 strcpy(filename + object_name_start + 3, hex + 2);
30 if (use_link) {
31 if (!link(filename, dest_filename)) {
32 pull_say("link %s\n", hex);
33 return 0;
35 /* If we got ENOENT there is no point continuing. */
36 if (errno == ENOENT) {
37 fprintf(stderr, "does not exist %s\n", filename);
38 return -1;
41 if (use_symlink && !symlink(filename, dest_filename)) {
42 pull_say("symlink %s\n", hex);
43 return 0;
45 if (use_filecopy) {
46 int ifd, ofd, status;
47 struct stat st;
48 void *map;
49 ifd = open(filename, O_RDONLY);
50 if (ifd < 0 || fstat(ifd, &st) < 0) {
51 close(ifd);
52 fprintf(stderr, "cannot open %s\n", filename);
53 return -1;
55 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0);
56 close(ifd);
57 if (-1 == (int)(long)map) {
58 fprintf(stderr, "cannot mmap %s\n", filename);
59 return -1;
61 ofd = open(dest_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
62 status = ((ofd < 0) ||
63 (write(ofd, map, st.st_size) != st.st_size));
64 munmap(map, st.st_size);
65 close(ofd);
66 if (status)
67 fprintf(stderr, "cannot write %s\n", dest_filename);
68 else
69 pull_say("copy %s\n", hex);
70 return status;
72 fprintf(stderr, "failed to copy %s with given copy methods.\n", hex);
73 return -1;
76 static const char *local_pull_usage =
77 "git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] [-d] commit-id path";
79 /*
80 * By default we only use file copy.
81 * If -l is specified, a hard link is attempted.
82 * If -s is specified, then a symlink is attempted.
83 * If -n is _not_ specified, then a regular file-to-file copy is done.
85 int main(int argc, char **argv)
87 char *commit_id;
88 int arg = 1;
90 while (arg < argc && argv[arg][0] == '-') {
91 if (argv[arg][1] == 't')
92 get_tree = 1;
93 else if (argv[arg][1] == 'c')
94 get_history = 1;
95 else if (argv[arg][1] == 'd')
96 get_delta = 0;
97 else if (argv[arg][1] == 'a') {
98 get_all = 1;
99 get_tree = 1;
100 get_history = 1;
102 else if (argv[arg][1] == 'l')
103 use_link = 1;
104 else if (argv[arg][1] == 's')
105 use_symlink = 1;
106 else if (argv[arg][1] == 'n')
107 use_filecopy = 0;
108 else if (argv[arg][1] == 'v')
109 get_verbosely = 1;
110 else
111 usage(local_pull_usage);
112 arg++;
114 if (argc < arg + 2)
115 usage(local_pull_usage);
116 commit_id = argv[arg];
117 path = argv[arg + 1];
119 if (pull(commit_id))
120 return 1;
122 return 0;