1eec8927dbfa3af934651b25ded738d192706286
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 "cache.h"
9#include "commit.h"
10#include <errno.h>
11#include <stdio.h>
12#include "pull.h"
13
14static int use_link = 0;
15static int use_symlink = 0;
16static int use_filecopy = 1;
17
18static char *path;
19
20int fetch(unsigned char *sha1)
21{
22 static int object_name_start = -1;
23 static char filename[PATH_MAX];
24 char *hex = sha1_to_hex(sha1);
25 const char *dest_filename = sha1_file_name(sha1);
26
27 if (object_name_start < 0) {
28 strcpy(filename, path); /* e.g. git.git */
29 strcat(filename, "/objects/");
30 object_name_start = strlen(filename);
31 }
32 filename[object_name_start+0] = hex[0];
33 filename[object_name_start+1] = hex[1];
34 filename[object_name_start+2] = '/';
35 strcpy(filename + object_name_start + 3, hex + 2);
36 if (use_link) {
37 if (!link(filename, dest_filename)) {
38 pull_say("link %s\n", hex);
39 return 0;
40 }
41 /* If we got ENOENT there is no point continuing. */
42 if (errno == ENOENT) {
43 fprintf(stderr, "does not exist %s\n", filename);
44 return -1;
45 }
46 }
47 if (use_symlink && !symlink(filename, dest_filename)) {
48 pull_say("symlink %s\n", hex);
49 return 0;
50 }
51 if (use_filecopy) {
52 int ifd, ofd, status;
53 struct stat st;
54 void *map;
55 ifd = open(filename, O_RDONLY);
56 if (ifd < 0 || fstat(ifd, &st) < 0) {
57 close(ifd);
58 fprintf(stderr, "cannot open %s\n", filename);
59 return -1;
60 }
61 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0);
62 close(ifd);
63 if (-1 == (int)(long)map) {
64 fprintf(stderr, "cannot mmap %s\n", filename);
65 return -1;
66 }
67 ofd = open(dest_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
68 status = ((ofd < 0) ||
69 (write(ofd, map, st.st_size) != st.st_size));
70 munmap(map, st.st_size);
71 close(ofd);
72 if (status)
73 fprintf(stderr, "cannot write %s (%ld bytes)\n",
74 dest_filename, st.st_size);
75 else
76 pull_say("copy %s\n", hex);
77 return status;
78 }
79 fprintf(stderr, "failed to copy %s with given copy methods.\n", hex);
80 return -1;
81}
82
83static const char *local_pull_usage =
84"git-local-pull [-c] [-t] [-a] [-l] [-s] [-n] [-v] commit-id path";
85
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.
91 */
92int main(int argc, char **argv)
93{
94 char *commit_id;
95 int arg = 1;
96
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;
106 }
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++;
118 }
119 if (argc < arg + 2)
120 usage(local_pull_usage);
121 commit_id = argv[arg];
122 path = argv[arg + 1];
123
124 if (pull(commit_id))
125 return 1;
126
127 return 0;
128}