[patch] git: cleanup in ls-tree.c
[git/repo.git] / local-pull.c
blob3a342ab18390d7ce0df1f970a4961b31548a9417
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"
15 static int use_link = 0;
16 static int use_symlink = 0;
17 static int use_filecopy = 1;
19 static char *path;
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);
37 if (use_link) {
38 if (!link(filename, dest_filename)) {
39 pull_say("link %s\n", hex);
40 return 0;
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;
48 if (use_symlink && !symlink(filename, dest_filename)) {
49 pull_say("symlink %s\n", hex);
50 return 0;
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;
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;
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\n", dest_filename);
75 else
76 pull_say("copy %s\n", hex);
77 return status;
79 fprintf(stderr, "failed to copy %s with given copy methods.\n", hex);
80 return -1;
83 static const char *local_pull_usage =
84 "git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] commit-id path";
86 /*
87 * By default we only use file copy.
88 * If -l is specified, a hard link is attempted.
89 * If -s is specified, then a symlink is attempted.
90 * If -n is _not_ specified, then a regular file-to-file copy is done.
92 int main(int argc, char **argv)
94 char *commit_id;
95 int arg = 1;
97 while (arg < argc && argv[arg][0] == '-') {
98 if (argv[arg][1] == 't')
99 get_tree = 1;
100 else if (argv[arg][1] == 'c')
101 get_history = 1;
102 else if (argv[arg][1] == 'a') {
103 get_all = 1;
104 get_tree = 1;
105 get_history = 1;
107 else if (argv[arg][1] == 'l')
108 use_link = 1;
109 else if (argv[arg][1] == 's')
110 use_symlink = 1;
111 else if (argv[arg][1] == 'n')
112 use_filecopy = 0;
113 else if (argv[arg][1] == 'v')
114 get_verbosely = 1;
115 else
116 usage(local_pull_usage);
117 arg++;
119 if (argc < arg + 2)
120 usage(local_pull_usage);
121 commit_id = argv[arg];
122 path = argv[arg + 1];
124 if (pull(commit_id))
125 return 1;
127 return 0;