Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 277 lines (248 sloc) 7.007 kb
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
1 #include "cache.h"
8e44025 Use blob_, commit_, tag_, and tree_type throughout.
Peter Eriksen authored
2 #include "blob.h"
8ca12c0 @aspotashev add is_dot_or_dotdot inline function
aspotashev authored
3 #include "dir.h"
dd8e912 @gitster streaming_write_entry(): use streaming API in write_entry()
gitster authored
4 #include "streaming.h"
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
5
81a9aa6 create_directories(): remove some memcpy() and strchr() calls
Kjetil Barvik authored
6 static void create_directories(const char *path, int path_len,
7 const struct checkout *state)
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
8 {
81a9aa6 create_directories(): remove some memcpy() and strchr() calls
Kjetil Barvik authored
9 char *buf = xmalloc(path_len + 1);
10 int len = 0;
11
12 while (len < path_len) {
13 do {
14 buf[len] = path[len];
15 len++;
16 } while (len < path_len && path[len] != '/');
17 if (len >= path_len)
18 break;
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
19 buf[len] = 0;
fa2e71c @gitster Do not expect unlink(2) to fail on a directory.
gitster authored
20
bad4a54 lstat_cache(): introduce has_dirs_only_path() function
Kjetil Barvik authored
21 /*
22 * For 'checkout-index --prefix=<dir>', <dir> is
23 * allowed to be a symlink to an existing directory,
24 * and we set 'state->base_dir_len' below, such that
25 * we test the path components of the prefix with the
26 * stat() function instead of the lstat() function.
27 */
5719989 lstat_cache(): swap func(length, string) into func(string, length)
Kjetil Barvik authored
28 if (has_dirs_only_path(buf, len, state->base_dir_len))
fa2e71c @gitster Do not expect unlink(2) to fail on a directory.
gitster authored
29 continue; /* ok, it is already a directory. */
30
31 /*
bad4a54 lstat_cache(): introduce has_dirs_only_path() function
Kjetil Barvik authored
32 * If this mkdir() would fail, it could be that there
33 * is already a symlink or something else exists
34 * there, therefore we then try to unlink it and try
35 * one more time to create the directory.
fa2e71c @gitster Do not expect unlink(2) to fail on a directory.
gitster authored
36 */
f312de0 [PATCH] Let umask do its work upon filesystem object creation.
Junio C Hamano authored
37 if (mkdir(buf, 0777)) {
fa2e71c @gitster Do not expect unlink(2) to fail on a directory.
gitster authored
38 if (errno == EEXIST && state->force &&
691f1a2 @raalkml replace direct calls to unlink(2) with unlink_or_warn
raalkml authored
39 !unlink_or_warn(buf) && !mkdir(buf, 0777))
fa2e71c @gitster Do not expect unlink(2) to fail on a directory.
gitster authored
40 continue;
0721c31 @trast Use die_errno() instead of die() when checking syscalls
trast authored
41 die_errno("cannot create directory at '%s'", buf);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
42 }
43 }
44 free(buf);
45 }
46
47 static void remove_subtree(const char *path)
48 {
49 DIR *dir = opendir(path);
50 struct dirent *de;
51 char pathbuf[PATH_MAX];
52 char *name;
a6080a0 @gitster War on whitespace
gitster authored
53
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
54 if (!dir)
d824cbb @trast Convert existing die(..., strerror(errno)) to die_errno()
trast authored
55 die_errno("cannot opendir '%s'", path);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
56 strcpy(pathbuf, path);
57 name = pathbuf + strlen(path);
58 *name++ = '/';
59 while ((de = readdir(dir)) != NULL) {
60 struct stat st;
8ca12c0 @aspotashev add is_dot_or_dotdot inline function
aspotashev authored
61 if (is_dot_or_dotdot(de->d_name))
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
62 continue;
63 strcpy(name, de->d_name);
64 if (lstat(pathbuf, &st))
d824cbb @trast Convert existing die(..., strerror(errno)) to die_errno()
trast authored
65 die_errno("cannot lstat '%s'", pathbuf);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
66 if (S_ISDIR(st.st_mode))
67 remove_subtree(pathbuf);
68 else if (unlink(pathbuf))
d824cbb @trast Convert existing die(..., strerror(errno)) to die_errno()
trast authored
69 die_errno("cannot unlink '%s'", pathbuf);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
70 }
71 closedir(dir);
72 if (rmdir(path))
d824cbb @trast Convert existing die(..., strerror(errno)) to die_errno()
trast authored
73 die_errno("cannot rmdir '%s'", path);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
74 }
75
d48a72f Fix replacing of a directory with a file/symlink in git-checkout-cache
Linus Torvalds authored
76 static int create_file(const char *path, unsigned int mode)
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
77 {
78 mode = (mode & 0100) ? 0777 : 0666;
781411e @raalkml trivial: O_EXCL makes O_TRUNC redundant
raalkml authored
79 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
80 }
81
4857c76 write_entry(): cleanup of some duplicated code
Kjetil Barvik authored
82 static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
f0807e6 @torvalds Teach "git-read-tree -u" to check out submodules as a directory
torvalds authored
83 {
84 enum object_type type;
85 void *new = read_sha1_file(ce->sha1, &type, size);
86
87 if (new) {
88 if (type == OBJ_BLOB)
89 return new;
90 free(new);
91 }
92 return NULL;
93 }
94
fd5db55 @gitster write_entry(): separate two helper functions out
gitster authored
95 static int open_output_fd(char *path, struct cache_entry *ce, int to_tempfile)
96 {
97 int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
98 if (to_tempfile) {
99 strcpy(path, symlink
100 ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
101 return mkstemp(path);
102 } else {
103 return create_file(path, !symlink ? ce->ce_mode : 0666);
104 }
105 }
106
107 static int fstat_output(int fd, const struct checkout *state, struct stat *st)
108 {
109 /* use fstat() only when path == ce->name */
110 if (fstat_is_reliable() &&
111 state->refresh_cache && !state->base_dir_len) {
112 fstat(fd, st);
113 return 1;
114 }
115 return 0;
116 }
117
dd8e912 @gitster streaming_write_entry(): use streaming API in write_entry()
gitster authored
118 static int streaming_write_entry(struct cache_entry *ce, char *path,
b669109 @gitster Add streaming filter API
gitster authored
119 struct stream_filter *filter,
dd8e912 @gitster streaming_write_entry(): use streaming API in write_entry()
gitster authored
120 const struct checkout *state, int to_tempfile,
121 int *fstat_done, struct stat *statbuf)
122 {
123 int result = -1;
47a02ff @gitster streaming: make streaming-write-entry to be more reusable
gitster authored
124 int fd;
dd8e912 @gitster streaming_write_entry(): use streaming API in write_entry()
gitster authored
125
126 fd = open_output_fd(path, ce, to_tempfile);
47a02ff @gitster streaming: make streaming-write-entry to be more reusable
gitster authored
127 if (0 <= fd) {
128 result = stream_blob_to_fd(fd, ce->sha1, filter, 1);
129 *fstat_done = fstat_output(fd, state, statbuf);
dd8e912 @gitster streaming_write_entry(): use streaming API in write_entry()
gitster authored
130 result = close(fd);
47a02ff @gitster streaming: make streaming-write-entry to be more reusable
gitster authored
131 }
dd8e912 @gitster streaming_write_entry(): use streaming API in write_entry()
gitster authored
132 if (result && 0 <= fd)
133 unlink(path);
134 return result;
135 }
136
efbc583 entry.c: Use const qualifier for 'struct checkout' parameters
Luiz Fernando N. Capitulino authored
137 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
138 {
4857c76 write_entry(): cleanup of some duplicated code
Kjetil Barvik authored
139 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
e4c7292 write_entry(): use fstat() instead of lstat() when file is open
Kjetil Barvik authored
140 int fd, ret, fstat_done = 0;
4857c76 write_entry(): cleanup of some duplicated code
Kjetil Barvik authored
141 char *new;
142 struct strbuf buf = STRBUF_INIT;
143 unsigned long size;
144 size_t wrote, newsize = 0;
e4c7292 write_entry(): use fstat() instead of lstat() when file is open
Kjetil Barvik authored
145 struct stat st;
4857c76 write_entry(): cleanup of some duplicated code
Kjetil Barvik authored
146
b669109 @gitster Add streaming filter API
gitster authored
147 if (ce_mode_s_ifmt == S_IFREG) {
148 struct stream_filter *filter = get_stream_filter(path, ce->sha1);
149 if (filter &&
150 !streaming_write_entry(ce, path, filter,
151 state, to_tempfile,
152 &fstat_done, &st))
153 goto finish;
154 }
dd8e912 @gitster streaming_write_entry(): use streaming API in write_entry()
gitster authored
155
4857c76 write_entry(): cleanup of some duplicated code
Kjetil Barvik authored
156 switch (ce_mode_s_ifmt) {
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
157 case S_IFREG:
4857c76 write_entry(): cleanup of some duplicated code
Kjetil Barvik authored
158 case S_IFLNK:
159 new = read_blob_entry(ce, &size);
f0807e6 @torvalds Teach "git-read-tree -u" to check out submodules as a directory
torvalds authored
160 if (!new)
d43e907 @pclouds entry.c: remove "checkout-index" from error messages
pclouds authored
161 return error("unable to read sha1 file of %s (%s)",
f0807e6 @torvalds Teach "git-read-tree -u" to check out submodules as a directory
torvalds authored
162 path, sha1_to_hex(ce->sha1));
1a9d7e9 @gitster attr.c: read .gitattributes from index as well.
gitster authored
163
4857c76 write_entry(): cleanup of some duplicated code
Kjetil Barvik authored
164 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
165 ret = symlink(new, path);
166 free(new);
167 if (ret)
d43e907 @pclouds entry.c: remove "checkout-index" from error messages
pclouds authored
168 return error("unable to create symlink %s (%s)",
4857c76 write_entry(): cleanup of some duplicated code
Kjetil Barvik authored
169 path, strerror(errno));
170 break;
171 }
172
1a9d7e9 @gitster attr.c: read .gitattributes from index as well.
gitster authored
173 /*
174 * Convert from git internal format to working tree format
175 */
4857c76 write_entry(): cleanup of some duplicated code
Kjetil Barvik authored
176 if (ce_mode_s_ifmt == S_IFREG &&
177 convert_to_working_tree(ce->name, new, size, &buf)) {
1a9d7e9 @gitster attr.c: read .gitattributes from index as well.
gitster authored
178 free(new);
c32f749 Correct some sizeof(size_t) != sizeof(unsigned long) typing errors
René Scharfe authored
179 new = strbuf_detach(&buf, &newsize);
180 size = newsize;
1a9d7e9 @gitster attr.c: read .gitattributes from index as well.
gitster authored
181 }
182
fd5db55 @gitster write_entry(): separate two helper functions out
gitster authored
183 fd = open_output_fd(path, ce, to_tempfile);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
184 if (fd < 0) {
185 free(new);
d43e907 @pclouds entry.c: remove "checkout-index" from error messages
pclouds authored
186 return error("unable to create file %s (%s)",
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
187 path, strerror(errno));
188 }
6c510be @torvalds Lazy man's auto-CRLF
torvalds authored
189
93822c2 @awhitcroft short i/o: fix calls to write to use xwrite or write_in_full
awhitcroft authored
190 wrote = write_in_full(fd, new, size);
fd5db55 @gitster write_entry(): separate two helper functions out
gitster authored
191 if (!to_tempfile)
192 fstat_done = fstat_output(fd, state, &st);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
193 close(fd);
194 free(new);
195 if (wrote != size)
d43e907 @pclouds entry.c: remove "checkout-index" from error messages
pclouds authored
196 return error("unable to write file %s", path);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
197 break;
302b928 @tali rename dirlink to gitlink.
tali authored
198 case S_IFGITLINK:
f0807e6 @torvalds Teach "git-read-tree -u" to check out submodules as a directory
torvalds authored
199 if (to_tempfile)
d43e907 @pclouds entry.c: remove "checkout-index" from error messages
pclouds authored
200 return error("cannot create temporary subproject %s", path);
f0807e6 @torvalds Teach "git-read-tree -u" to check out submodules as a directory
torvalds authored
201 if (mkdir(path, 0777) < 0)
d43e907 @pclouds entry.c: remove "checkout-index" from error messages
pclouds authored
202 return error("cannot create subproject directory %s", path);
f0807e6 @torvalds Teach "git-read-tree -u" to check out submodules as a directory
torvalds authored
203 break;
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
204 default:
d43e907 @pclouds entry.c: remove "checkout-index" from error messages
pclouds authored
205 return error("unknown file mode for %s in index", path);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
206 }
207
dd8e912 @gitster streaming_write_entry(): use streaming API in write_entry()
gitster authored
208 finish:
6ee67f2 Fix entry.c dependency and compile problem
Linus Torvalds authored
209 if (state->refresh_cache) {
e4c7292 write_entry(): use fstat() instead of lstat() when file is open
Kjetil Barvik authored
210 if (!fstat_done)
211 lstat(ce->name, &st);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
212 fill_stat_cache_info(ce, &st);
213 }
214 return 0;
215 }
216
b6986d8 @torvalds git-checkout: be careful about untracked symlinks
torvalds authored
217 /*
218 * This is like 'lstat()', except it refuses to follow symlinks
da02ca5 @gitster check_path(): allow symlinked directories to checkout-index --prefix
gitster authored
219 * in the path, after skipping "skiplen".
b6986d8 @torvalds git-checkout: be careful about untracked symlinks
torvalds authored
220 */
61b97df @gitster entry.c: mark file-local function static
gitster authored
221 static int check_path(const char *path, int len, struct stat *st, int skiplen)
b6986d8 @torvalds git-checkout: be careful about untracked symlinks
torvalds authored
222 {
da02ca5 @gitster check_path(): allow symlinked directories to checkout-index --prefix
gitster authored
223 const char *slash = path + len;
224
225 while (path < slash && *slash != '/')
226 slash--;
227 if (!has_dirs_only_path(path, slash - path, skiplen)) {
b6986d8 @torvalds git-checkout: be careful about untracked symlinks
torvalds authored
228 errno = ENOENT;
229 return -1;
230 }
231 return lstat(path, st);
232 }
233
efbc583 entry.c: Use const qualifier for 'struct checkout' parameters
Luiz Fernando N. Capitulino authored
234 int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
235 {
095c424 @jonas Use PATH_MAX instead of MAXPATHLEN
jonas authored
236 static char path[PATH_MAX + 1];
de84f99 @spearce Add --temp and --stage=all options to checkout-index.
spearce authored
237 struct stat st;
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
238 int len = state->base_dir_len;
239
de84f99 @spearce Add --temp and --stage=all options to checkout-index.
spearce authored
240 if (topath)
241 return write_entry(ce, topath, state, 1);
242
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
243 memcpy(path, state->base_dir, len);
244 strcpy(path + len, ce->name);
81a9aa6 create_directories(): remove some memcpy() and strchr() calls
Kjetil Barvik authored
245 len += ce_namelen(ce);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
246
da02ca5 @gitster check_path(): allow symlinked directories to checkout-index --prefix
gitster authored
247 if (!check_path(path, len, &st, state->base_dir_len)) {
56cac48 @pclouds ie_match_stat(): do not ignore skip-worktree bit with CE_MATCH_IGNORE…
pclouds authored
248 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
249 if (!changed)
250 return 0;
251 if (!state->force) {
252 if (!state->quiet)
d43e907 @pclouds entry.c: remove "checkout-index" from error messages
pclouds authored
253 fprintf(stderr, "%s already exists, no checkout\n", path);
4b12dae Return error when not checking out an entry due to dirtiness.
Junio C Hamano authored
254 return -1;
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
255 }
256
257 /*
258 * We unlink the old file, to get the new one with the
259 * right permissions (including umask, which is nasty
260 * to emulate by hand - much easier to let the system
261 * just do the right thing)
262 */
d48a72f Fix replacing of a directory with a file/symlink in git-checkout-cache
Linus Torvalds authored
263 if (S_ISDIR(st.st_mode)) {
f0807e6 @torvalds Teach "git-read-tree -u" to check out submodules as a directory
torvalds authored
264 /* If it is a gitlink, leave it alone! */
7a51ed6 @torvalds Make on-disk index representation separate from in-core one
torvalds authored
265 if (S_ISGITLINK(ce->ce_mode))
f0807e6 @torvalds Teach "git-read-tree -u" to check out submodules as a directory
torvalds authored
266 return 0;
d48a72f Fix replacing of a directory with a file/symlink in git-checkout-cache
Linus Torvalds authored
267 if (!state->force)
268 return error("%s is a directory", path);
269 remove_subtree(path);
971f229 @torvalds Fix possible Solaris problem in 'checkout_entry()'
torvalds authored
270 } else if (unlink(path))
271 return error("unable to unlink old '%s' (%s)", path, strerror(errno));
de84f99 @spearce Add --temp and --stage=all options to checkout-index.
spearce authored
272 } else if (state->not_new)
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
273 return 0;
81a9aa6 create_directories(): remove some memcpy() and strchr() calls
Kjetil Barvik authored
274 create_directories(path, len, state);
de84f99 @spearce Add --temp and --stage=all options to checkout-index.
spearce authored
275 return write_entry(ce, path, state, 0);
12dccc1 Make fiel checkout function available to the git library
Linus Torvalds authored
276 }
Something went wrong with that request. Please try again.