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>
This commit is contained in:
parent
81a9aa60a1
commit
4857c761e3
73
entry.c
73
entry.c
@ -78,7 +78,7 @@ static int create_file(const char *path, unsigned int mode)
|
|||||||
return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
|
return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size)
|
static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
|
||||||
{
|
{
|
||||||
enum object_type type;
|
enum object_type type;
|
||||||
void *new = read_sha1_file(ce->sha1, &type, size);
|
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
|
|||||||
|
|
||||||
static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
|
static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
|
||||||
{
|
{
|
||||||
int fd;
|
unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
|
||||||
long wrote;
|
int fd, ret;
|
||||||
|
char *new;
|
||||||
switch (ce->ce_mode & S_IFMT) {
|
struct strbuf buf = STRBUF_INIT;
|
||||||
char *new;
|
unsigned long size;
|
||||||
struct strbuf buf;
|
size_t wrote, newsize = 0;
|
||||||
unsigned long size;
|
|
||||||
|
|
||||||
|
switch (ce_mode_s_ifmt) {
|
||||||
case S_IFREG:
|
case S_IFREG:
|
||||||
new = read_blob_entry(ce, path, &size);
|
case S_IFLNK:
|
||||||
|
new = read_blob_entry(ce, &size);
|
||||||
if (!new)
|
if (!new)
|
||||||
return error("git checkout-index: unable to read sha1 file of %s (%s)",
|
return error("git checkout-index: unable to read sha1 file of %s (%s)",
|
||||||
path, sha1_to_hex(ce->sha1));
|
path, sha1_to_hex(ce->sha1));
|
||||||
|
|
||||||
|
if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
|
||||||
|
ret = symlink(new, path);
|
||||||
|
free(new);
|
||||||
|
if (ret)
|
||||||
|
return error("git checkout-index: unable to create symlink %s (%s)",
|
||||||
|
path, strerror(errno));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Convert from git internal format to working tree format
|
* Convert from git internal format to working tree format
|
||||||
*/
|
*/
|
||||||
strbuf_init(&buf, 0);
|
if (ce_mode_s_ifmt == S_IFREG &&
|
||||||
if (convert_to_working_tree(ce->name, new, size, &buf)) {
|
convert_to_working_tree(ce->name, new, size, &buf)) {
|
||||||
size_t newsize = 0;
|
|
||||||
free(new);
|
free(new);
|
||||||
new = strbuf_detach(&buf, &newsize);
|
new = strbuf_detach(&buf, &newsize);
|
||||||
size = newsize;
|
size = newsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (to_tempfile) {
|
if (to_tempfile) {
|
||||||
strcpy(path, ".merge_file_XXXXXX");
|
if (ce_mode_s_ifmt == S_IFREG)
|
||||||
|
strcpy(path, ".merge_file_XXXXXX");
|
||||||
|
else
|
||||||
|
strcpy(path, ".merge_link_XXXXXX");
|
||||||
fd = mkstemp(path);
|
fd = mkstemp(path);
|
||||||
} else
|
} else if (ce_mode_s_ifmt == S_IFREG) {
|
||||||
fd = create_file(path, ce->ce_mode);
|
fd = create_file(path, ce->ce_mode);
|
||||||
|
} else {
|
||||||
|
fd = create_file(path, 0666);
|
||||||
|
}
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
free(new);
|
free(new);
|
||||||
return error("git checkout-index: unable to create file %s (%s)",
|
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
|
|||||||
if (wrote != size)
|
if (wrote != size)
|
||||||
return error("git checkout-index: unable to write file %s", path);
|
return error("git checkout-index: unable to write file %s", path);
|
||||||
break;
|
break;
|
||||||
case S_IFLNK:
|
|
||||||
new = read_blob_entry(ce, path, &size);
|
|
||||||
if (!new)
|
|
||||||
return error("git checkout-index: unable to read sha1 file of %s (%s)",
|
|
||||||
path, sha1_to_hex(ce->sha1));
|
|
||||||
if (to_tempfile || !has_symlinks) {
|
|
||||||
if (to_tempfile) {
|
|
||||||
strcpy(path, ".merge_link_XXXXXX");
|
|
||||||
fd = mkstemp(path);
|
|
||||||
} else
|
|
||||||
fd = create_file(path, 0666);
|
|
||||||
if (fd < 0) {
|
|
||||||
free(new);
|
|
||||||
return error("git checkout-index: unable to create "
|
|
||||||
"file %s (%s)", path, strerror(errno));
|
|
||||||
}
|
|
||||||
wrote = write_in_full(fd, new, size);
|
|
||||||
close(fd);
|
|
||||||
free(new);
|
|
||||||
if (wrote != size)
|
|
||||||
return error("git checkout-index: unable to write file %s",
|
|
||||||
path);
|
|
||||||
} else {
|
|
||||||
wrote = symlink(new, path);
|
|
||||||
free(new);
|
|
||||||
if (wrote)
|
|
||||||
return error("git checkout-index: unable to create "
|
|
||||||
"symlink %s (%s)", path, strerror(errno));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case S_IFGITLINK:
|
case S_IFGITLINK:
|
||||||
if (to_tempfile)
|
if (to_tempfile)
|
||||||
return error("git checkout-index: cannot create temporary subproject %s", path);
|
return error("git checkout-index: cannot create temporary subproject %s", path);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user