2 * Copyright (C) 2005 Junio C Hamano
15 static int use_link = 0;
16 static int use_symlink = 0;
17 static int use_filecopy = 1;
21 int fetch(unsigned char *sha1)
23 static int object_name_start = -1;
24 static char filename[PATH_MAX];
25 char *hex = sha1_to_hex(sha1);
26 const char *dest_filename = sha1_file_name(sha1);
28 if (object_name_start < 0) {
29 strcpy(filename, path); /* e.g. git.git */
30 strcat(filename, "/objects/");
31 object_name_start = strlen(filename);
33 filename[object_name_start+0] = hex[0];
34 filename[object_name_start+1] = hex[1];
35 filename[object_name_start+2] = '/';
36 strcpy(filename + object_name_start + 3, hex + 2);
38 if (!link(filename, dest_filename)) {
39 pull_say("link %s\n", hex);
42 /* If we got ENOENT there is no point continuing. */
43 if (errno == ENOENT) {
44 fprintf(stderr, "does not exist %s\n", filename);
48 if (use_symlink && !symlink(filename, dest_filename)) {
49 pull_say("symlink %s\n", hex);
56 ifd = open(filename, O_RDONLY);
57 if (ifd < 0 || fstat(ifd, &st) < 0) {
59 fprintf(stderr, "cannot open %s\n", filename);
62 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0);
64 if (-1 == (int)(long)map) {
65 fprintf(stderr, "cannot mmap %s\n", filename);
68 ofd = open(dest_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
69 status = ((ofd < 0) ||
70 (write(ofd, map, st.st_size) != st.st_size));
71 munmap(map, st.st_size);
74 fprintf(stderr, "cannot write %s (%ld bytes)\n",
75 dest_filename, st.st_size);
77 pull_say("copy %s\n", hex);
80 fprintf(stderr, "failed to copy %s with given copy methods.\n", hex);
84 static const char *local_pull_usage =
85 "git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] commit-id path";
88 * By default we only use file copy.
89 * If -l is specified, a hard link is attempted.
90 * If -s is specified, then a symlink is attempted.
91 * If -n is _not_ specified, then a regular file-to-file copy is done.
93 int main(int argc, char **argv)
98 while (arg < argc && argv[arg][0] == '-') {
99 if (argv[arg][1] == 't')
101 else if (argv[arg][1] == 'c')
103 else if (argv[arg][1] == 'a') {
108 else if (argv[arg][1] == 'l')
110 else if (argv[arg][1] == 's')
112 else if (argv[arg][1] == 'n')
114 else if (argv[arg][1] == 'v')
117 usage(local_pull_usage);
121 usage(local_pull_usage);
122 commit_id = argv[arg];
123 path = argv[arg + 1];