@matheusbsbs / matheus-bagliano / commits / 4857c761e3
Fork 0

write_entry(): cleanup of some duplicated code

The switch-cases for S_IFREG and S_IFLNK was so similar that it will be better to do some cleanup and use the common parts of it. And the entry.c file should now be clean for 'gcc -Wextra' warnings. Signed-off-by: Kjetil Barvik <barvik@broadpark.no> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kjetil Barvik committed Feb 9, 2009 at 21:54 UTC 4857c761e35b07c12ff2ef1140e93b071b8ac4e7
1 file changed +30 -45
entry.c
+30 -45
@@ -78,7 +78,7 @@ static int create_file(const char *path, unsigned int mode)
78 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
79 }
80
81 -static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size)
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);
@@ -93,36 +93,51 @@ static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned
93
94 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
95 {
96 - int fd;
97 - long wrote;
98 -
99 - switch (ce->ce_mode & S_IFMT) {
100 - char *new;
101 - struct strbuf buf;
102 - unsigned long size;
103 -
96 + unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
97 + int fd, ret;
98 + char *new;
99 + struct strbuf buf = STRBUF_INIT;
100 + unsigned long size;
101 + size_t wrote, newsize = 0;
102 +
103 + switch (ce_mode_s_ifmt) {
104 case S_IFREG:
105 - new = read_blob_entry(ce, path, &size);
105 + case S_IFLNK:
106 + new = read_blob_entry(ce, &size);
107 if (!new)
108 return error("git checkout-index: unable to read sha1 file of %s (%s)",
109 path, sha1_to_hex(ce->sha1));
110
111 + if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
112 + ret = symlink(new, path);
113 + free(new);
114 + if (ret)
115 + return error("git checkout-index: unable to create symlink %s (%s)",
116 + path, strerror(errno));
117 + break;
118 + }
119 +
120 /*
121 * Convert from git internal format to working tree format
122 */
113 - strbuf_init(&buf, 0);
114 - if (convert_to_working_tree(ce->name, new, size, &buf)) {
115 - size_t newsize = 0;
123 + if (ce_mode_s_ifmt == S_IFREG &&
124 + convert_to_working_tree(ce->name, new, size, &buf)) {
125 free(new);
126 new = strbuf_detach(&buf, &newsize);
127 size = newsize;
128 }
129
130 if (to_tempfile) {
122 - strcpy(path, ".merge_file_XXXXXX");
131 + if (ce_mode_s_ifmt == S_IFREG)
132 + strcpy(path, ".merge_file_XXXXXX");
133 + else
134 + strcpy(path, ".merge_link_XXXXXX");
135 fd = mkstemp(path);
124 - } else
136 + } else if (ce_mode_s_ifmt == S_IFREG) {
137 fd = create_file(path, ce->ce_mode);
138 + } else {
139 + fd = create_file(path, 0666);
140 + }
141 if (fd < 0) {
142 free(new);
143 return error("git checkout-index: unable to create file %s (%s)",
@@ -135,36 +150,6 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
150 if (wrote != size)
151 return error("git checkout-index: unable to write file %s", path);
152 break;
138 - case S_IFLNK:
139 - new = read_blob_entry(ce, path, &size);
140 - if (!new)
141 - return error("git checkout-index: unable to read sha1 file of %s (%s)",
142 - path, sha1_to_hex(ce->sha1));
143 - if (to_tempfile || !has_symlinks) {
144 - if (to_tempfile) {
145 - strcpy(path, ".merge_link_XXXXXX");
146 - fd = mkstemp(path);
147 - } else
148 - fd = create_file(path, 0666);
149 - if (fd < 0) {
150 - free(new);
151 - return error("git checkout-index: unable to create "
152 - "file %s (%s)", path, strerror(errno));
153 - }
154 - wrote = write_in_full(fd, new, size);
155 - close(fd);
156 - free(new);
157 - if (wrote != size)
158 - return error("git checkout-index: unable to write file %s",
159 - path);
160 - } else {
161 - wrote = symlink(new, path);
162 - free(new);
163 - if (wrote)
164 - return error("git checkout-index: unable to create "
165 - "symlink %s (%s)", path, strerror(errno));
166 - }
167 - break;
153 case S_IFGITLINK:
154 if (to_tempfile)
155 return error("git checkout-index: cannot create temporary subproject %s", path);