[PATCH] make INSTALL binary in Makefile configurable via make variable
[git.git] / local-pull.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <limits.h>
9 #include "cache.h"
10 #include "commit.h"
11 #include <errno.h>
12 #include <stdio.h>
13 #include "pull.h"
14
15 static int use_link = 0;
16 static int use_symlink = 0;
17 static int use_filecopy = 1;
18
19 static char *path;
20
21 int fetch(unsigned char *sha1)
22 {
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);
27
28         if (object_name_start < 0) {
29                 strcpy(filename, path); /* e.g. git.git */
30                 strcat(filename, "/objects/");
31                 object_name_start = strlen(filename);
32         }
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);
37         if (use_link) {
38                 if (!link(filename, dest_filename)) {
39                         pull_say("link %s\n", hex);
40                         return 0;
41                 }
42                 /* If we got ENOENT there is no point continuing. */
43                 if (errno == ENOENT) {
44                         fprintf(stderr, "does not exist %s\n", filename);
45                         return -1;
46                 }
47         }
48         if (use_symlink && !symlink(filename, dest_filename)) {
49                 pull_say("symlink %s\n", hex);
50                 return 0;
51         }
52         if (use_filecopy) {
53                 int ifd, ofd, status;
54                 struct stat st;
55                 void *map;
56                 ifd = open(filename, O_RDONLY);
57                 if (ifd < 0 || fstat(ifd, &st) < 0) {
58                         close(ifd);
59                         fprintf(stderr, "cannot open %s\n", filename);
60                         return -1;
61                 }
62                 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0);
63                 close(ifd);
64                 if (-1 == (int)(long)map) {
65                         fprintf(stderr, "cannot mmap %s\n", filename);
66                         return -1;
67                 }
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);
72                 close(ofd);
73                 if (status)
74                         fprintf(stderr, "cannot write %s (%ld bytes)\n",
75                                 dest_filename, st.st_size);
76                 else
77                         pull_say("copy %s\n", hex);
78                 return status;
79         }
80         fprintf(stderr, "failed to copy %s with given copy methods.\n", hex);
81         return -1;
82 }
83
84 static const char *local_pull_usage = 
85 "git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] commit-id path";
86
87 /* 
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.
92  */
93 int main(int argc, char **argv)
94 {
95         char *commit_id;
96         int arg = 1;
97
98         while (arg < argc && argv[arg][0] == '-') {
99                 if (argv[arg][1] == 't')
100                         get_tree = 1;
101                 else if (argv[arg][1] == 'c')
102                         get_history = 1;
103                 else if (argv[arg][1] == 'a') {
104                         get_all = 1;
105                         get_tree = 1;
106                         get_history = 1;
107                 }
108                 else if (argv[arg][1] == 'l')
109                         use_link = 1;
110                 else if (argv[arg][1] == 's')
111                         use_symlink = 1;
112                 else if (argv[arg][1] == 'n')
113                         use_filecopy = 0;
114                 else if (argv[arg][1] == 'v')
115                         get_verbosely = 1;
116                 else
117                         usage(local_pull_usage);
118                 arg++;
119         }
120         if (argc < arg + 2)
121                 usage(local_pull_usage);
122         commit_id = argv[arg];
123         path = argv[arg + 1];
124
125         if (pull(commit_id))
126                 return 1;
127
128         return 0;
129 }