]> git.ipfire.org Git - thirdparty/git.git/blame - local-pull.c
[PATCH] Updates for cvs-migration.txt
[thirdparty/git.git] / local-pull.c
CommitLineData
b02a26cb
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
dfcb4057
JH
4#include "cache.h"
5#include "commit.h"
dfcb4057
JH
6#include "pull.h"
7
8static int use_link = 0;
9static int use_symlink = 0;
b02a26cb 10static int use_filecopy = 1;
dfcb4057 11
f5ab6cca 12static char *path; /* "Remote" git repository */
dfcb4057 13
dfcb4057
JH
14int fetch(unsigned char *sha1)
15{
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);
dfcb4057
JH
20
21 if (object_name_start < 0) {
22 strcpy(filename, path); /* e.g. git.git */
23 strcat(filename, "/objects/");
24 object_name_start = strlen(filename);
25 }
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);
fd0ffd3a
JH
30 if (use_link) {
31 if (!link(filename, dest_filename)) {
e78d9772 32 pull_say("link %s\n", hex);
fd0ffd3a
JH
33 return 0;
34 }
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;
39 }
dfcb4057
JH
40 }
41 if (use_symlink && !symlink(filename, dest_filename)) {
e78d9772 42 pull_say("symlink %s\n", hex);
dfcb4057
JH
43 return 0;
44 }
b02a26cb
JH
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);
fd0ffd3a 52 fprintf(stderr, "cannot open %s\n", filename);
b02a26cb
JH
53 return -1;
54 }
55 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0);
dfcb4057 56 close(ifd);
e35f9824 57 if (map == MAP_FAILED) {
fd0ffd3a 58 fprintf(stderr, "cannot mmap %s\n", filename);
b02a26cb
JH
59 return -1;
60 }
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)
b4635be7 67 fprintf(stderr, "cannot write %s\n", dest_filename);
b02a26cb 68 else
e78d9772 69 pull_say("copy %s\n", hex);
b02a26cb 70 return status;
dfcb4057 71 }
fd0ffd3a 72 fprintf(stderr, "failed to copy %s with given copy methods.\n", hex);
b02a26cb 73 return -1;
dfcb4057
JH
74}
75
cd541a68
DB
76int fetch_ref(char *ref, unsigned char *sha1)
77{
f5ab6cca
JH
78 static int ref_name_start = -1;
79 static char filename[PATH_MAX];
80 static char hex[41];
81 int ifd;
82
83 if (ref_name_start < 0) {
84 sprintf(filename, "%s/refs/", path);
85 ref_name_start = strlen(filename);
86 }
87 strcpy(filename + ref_name_start, ref);
88 ifd = open(filename, O_RDONLY);
89 if (ifd < 0) {
90 close(ifd);
91 fprintf(stderr, "cannot open %s\n", filename);
92 return -1;
93 }
94 if (read(ifd, hex, 40) != 40 || get_sha1_hex(hex, sha1)) {
95 close(ifd);
96 fprintf(stderr, "cannot read from %s\n", filename);
97 return -1;
98 }
99 close(ifd);
100 pull_say("ref %s\n", sha1_to_hex(sha1));
101 return 0;
cd541a68
DB
102}
103
4d1f1190 104static const char local_pull_usage[] =
f5ab6cca 105"git-local-pull [-c] [-t] [-a] [-d] [-v] [-w filename] [--recover] [-l] [-s] [-n] commit-id path";
dfcb4057 106
b02a26cb
JH
107/*
108 * By default we only use file copy.
109 * If -l is specified, a hard link is attempted.
110 * If -s is specified, then a symlink is attempted.
111 * If -n is _not_ specified, then a regular file-to-file copy is done.
112 */
dfcb4057
JH
113int main(int argc, char **argv)
114{
115 char *commit_id;
116 int arg = 1;
117
118 while (arg < argc && argv[arg][0] == '-') {
119 if (argv[arg][1] == 't')
120 get_tree = 1;
121 else if (argv[arg][1] == 'c')
122 get_history = 1;
123 else if (argv[arg][1] == 'a') {
124 get_all = 1;
125 get_tree = 1;
126 get_history = 1;
127 }
128 else if (argv[arg][1] == 'l')
129 use_link = 1;
130 else if (argv[arg][1] == 's')
131 use_symlink = 1;
b02a26cb
JH
132 else if (argv[arg][1] == 'n')
133 use_filecopy = 0;
dfcb4057 134 else if (argv[arg][1] == 'v')
e78d9772 135 get_verbosely = 1;
f5ab6cca
JH
136 else if (argv[arg][1] == 'w')
137 write_ref = argv[++arg];
dfcb4057
JH
138 else
139 usage(local_pull_usage);
140 arg++;
141 }
142 if (argc < arg + 2)
143 usage(local_pull_usage);
144 commit_id = argv[arg];
145 path = argv[arg + 1];
146
147 if (pull(commit_id))
148 return 1;
149
150 return 0;
151}