Merge branch 'jc/clone-branch-rebase'
[git] / entry.c
1 #include "cache.h"
2 #include "blob.h"
3 #include "dir.h"
4
5 static void create_directories(const char *path, int path_len,
6                                const struct checkout *state)
7 {
8         char *buf = xmalloc(path_len + 1);
9         int len = 0;
10
11         while (len < path_len) {
12                 do {
13                         buf[len] = path[len];
14                         len++;
15                 } while (len < path_len && path[len] != '/');
16                 if (len >= path_len)
17                         break;
18                 buf[len] = 0;
19
20                 /*
21                  * For 'checkout-index --prefix=<dir>', <dir> is
22                  * allowed to be a symlink to an existing directory,
23                  * and we set 'state->base_dir_len' below, such that
24                  * we test the path components of the prefix with the
25                  * stat() function instead of the lstat() function.
26                  */
27                 if (has_dirs_only_path(buf, len, state->base_dir_len))
28                         continue; /* ok, it is already a directory. */
29
30                 /*
31                  * If this mkdir() would fail, it could be that there
32                  * is already a symlink or something else exists
33                  * there, therefore we then try to unlink it and try
34                  * one more time to create the directory.
35                  */
36                 if (mkdir(buf, 0777)) {
37                         if (errno == EEXIST && state->force &&
38                             !unlink(buf) && !mkdir(buf, 0777))
39                                 continue;
40                         die("cannot create directory at %s", buf);
41                 }
42         }
43         free(buf);
44 }
45
46 static void remove_subtree(const char *path)
47 {
48         DIR *dir = opendir(path);
49         struct dirent *de;
50         char pathbuf[PATH_MAX];
51         char *name;
52
53         if (!dir)
54                 die("cannot opendir %s (%s)", path, strerror(errno));
55         strcpy(pathbuf, path);
56         name = pathbuf + strlen(path);
57         *name++ = '/';
58         while ((de = readdir(dir)) != NULL) {
59                 struct stat st;
60                 if (is_dot_or_dotdot(de->d_name))
61                         continue;
62                 strcpy(name, de->d_name);
63                 if (lstat(pathbuf, &st))
64                         die("cannot lstat %s (%s)", pathbuf, strerror(errno));
65                 if (S_ISDIR(st.st_mode))
66                         remove_subtree(pathbuf);
67                 else if (unlink(pathbuf))
68                         die("cannot unlink %s (%s)", pathbuf, strerror(errno));
69         }
70         closedir(dir);
71         if (rmdir(path))
72                 die("cannot rmdir %s (%s)", path, strerror(errno));
73 }
74
75 static int create_file(const char *path, unsigned int mode)
76 {
77         mode = (mode & 0100) ? 0777 : 0666;
78         return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
79 }
80
81 static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
82 {
83         enum object_type type;
84         void *new = read_sha1_file(ce->sha1, &type, size);
85
86         if (new) {
87                 if (type == OBJ_BLOB)
88                         return new;
89                 free(new);
90         }
91         return NULL;
92 }
93
94 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
95 {
96         unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
97         int fd, ret, fstat_done = 0;
98         char *new;
99         struct strbuf buf = STRBUF_INIT;
100         unsigned long size;
101         size_t wrote, newsize = 0;
102         struct stat st;
103
104         switch (ce_mode_s_ifmt) {
105         case S_IFREG:
106         case S_IFLNK:
107                 new = read_blob_entry(ce, &size);
108                 if (!new)
109                         return error("git checkout-index: unable to read sha1 file of %s (%s)",
110                                 path, sha1_to_hex(ce->sha1));
111
112                 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
113                         ret = symlink(new, path);
114                         free(new);
115                         if (ret)
116                                 return error("git checkout-index: unable to create symlink %s (%s)",
117                                              path, strerror(errno));
118                         break;
119                 }
120
121                 /*
122                  * Convert from git internal format to working tree format
123                  */
124                 if (ce_mode_s_ifmt == S_IFREG &&
125                     convert_to_working_tree(ce->name, new, size, &buf)) {
126                         free(new);
127                         new = strbuf_detach(&buf, &newsize);
128                         size = newsize;
129                 }
130
131                 if (to_tempfile) {
132                         if (ce_mode_s_ifmt == S_IFREG)
133                                 strcpy(path, ".merge_file_XXXXXX");
134                         else
135                                 strcpy(path, ".merge_link_XXXXXX");
136                         fd = mkstemp(path);
137                 } else if (ce_mode_s_ifmt == S_IFREG) {
138                         fd = create_file(path, ce->ce_mode);
139                 } else {
140                         fd = create_file(path, 0666);
141                 }
142                 if (fd < 0) {
143                         free(new);
144                         return error("git checkout-index: unable to create file %s (%s)",
145                                 path, strerror(errno));
146                 }
147
148                 wrote = write_in_full(fd, new, size);
149                 /* use fstat() only when path == ce->name */
150                 if (state->refresh_cache && !to_tempfile && !state->base_dir_len) {
151                         fstat(fd, &st);
152                         fstat_done = 1;
153                 }
154                 close(fd);
155                 free(new);
156                 if (wrote != size)
157                         return error("git checkout-index: unable to write file %s", path);
158                 break;
159         case S_IFGITLINK:
160                 if (to_tempfile)
161                         return error("git checkout-index: cannot create temporary subproject %s", path);
162                 if (mkdir(path, 0777) < 0)
163                         return error("git checkout-index: cannot create subproject directory %s", path);
164                 break;
165         default:
166                 return error("git checkout-index: unknown file mode for %s", path);
167         }
168
169         if (state->refresh_cache) {
170                 if (!fstat_done)
171                         lstat(ce->name, &st);
172                 fill_stat_cache_info(ce, &st);
173         }
174         return 0;
175 }
176
177 int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
178 {
179         static char path[PATH_MAX + 1];
180         struct stat st;
181         int len = state->base_dir_len;
182
183         if (topath)
184                 return write_entry(ce, topath, state, 1);
185
186         memcpy(path, state->base_dir, len);
187         strcpy(path + len, ce->name);
188         len += ce_namelen(ce);
189
190         if (!lstat(path, &st)) {
191                 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
192                 if (!changed)
193                         return 0;
194                 if (!state->force) {
195                         if (!state->quiet)
196                                 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
197                         return -1;
198                 }
199
200                 /*
201                  * We unlink the old file, to get the new one with the
202                  * right permissions (including umask, which is nasty
203                  * to emulate by hand - much easier to let the system
204                  * just do the right thing)
205                  */
206                 if (S_ISDIR(st.st_mode)) {
207                         /* If it is a gitlink, leave it alone! */
208                         if (S_ISGITLINK(ce->ce_mode))
209                                 return 0;
210                         if (!state->force)
211                                 return error("%s is a directory", path);
212                         remove_subtree(path);
213                 } else if (unlink(path))
214                         return error("unable to unlink old '%s' (%s)", path, strerror(errno));
215         } else if (state->not_new)
216                 return 0;
217         create_directories(path, len, state);
218         return write_entry(ce, path, state, 0);
219 }