diff options
author | Russell Belfer <rb@github.com> | 2013-05-20 13:41:39 -0700 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2013-05-20 13:41:39 -0700 |
commit | 4742148d54334629495eeaf0382e6c9da8786f17 (patch) | |
tree | 9fe01ef4d5b558c028432d4648f4b03c6fb5946c /src | |
parent | 9be5be47fb1d9bc08e25b30c05dbf48739710062 (diff) | |
download | libgit2-4742148d54334629495eeaf0382e6c9da8786f17.tar.gz |
Add more diff rename detection tests
This adds a bunch more rename detection tests including checks
vs the working directory, the new exact match options, some more
whitespace variants, etc.
This also adds a git_futils_writebuffer helper function and uses
it in checkout. This is mainly added because I wanted an easy
way to write out a git_buf to disk inside my test code.
Diffstat (limited to 'src')
-rw-r--r-- | src/checkout.c | 29 | ||||
-rw-r--r-- | src/fileops.c | 26 | ||||
-rw-r--r-- | src/fileops.h | 3 |
3 files changed, 40 insertions, 18 deletions
diff --git a/src/checkout.c b/src/checkout.c index 5820f626a..c28fcdee0 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -676,33 +676,26 @@ static int buffer_to_file( int file_open_flags, mode_t file_mode) { - int fd, error; + int error; if ((error = git_futils_mkpath2file(path, dir_mode)) < 0) return error; - if ((fd = p_open(path, file_open_flags, file_mode)) < 0) { - giterr_set(GITERR_OS, "Could not open '%s' for writing", path); - return fd; - } - - if ((error = p_write(fd, git_buf_cstr(buffer), git_buf_len(buffer))) < 0) { - giterr_set(GITERR_OS, "Could not write to '%s'", path); - (void)p_close(fd); - } else { - if ((error = p_close(fd)) < 0) - giterr_set(GITERR_OS, "Error while closing '%s'", path); + if ((error = git_futils_writebuffer( + buffer, path, file_open_flags, file_mode)) < 0) + return error; - if ((error = p_stat(path, st)) < 0) - giterr_set(GITERR_OS, "Error while statting '%s'", path); + if (st != NULL && (error = p_stat(path, st)) < 0) { + giterr_set(GITERR_OS, "Error while statting '%s'", path); + return error; } - if (!error && - (file_mode & 0100) != 0 && - (error = p_chmod(path, file_mode)) < 0) + if ((file_mode & 0100) != 0 && (error = p_chmod(path, file_mode)) < 0) { giterr_set(GITERR_OS, "Failed to set permissions on '%s'", path); + return error; + } - return error; + return 0; } static int blob_content_to_file( diff --git a/src/fileops.c b/src/fileops.c index 98ab8efe3..a3e43214f 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -202,6 +202,32 @@ int git_futils_readbuffer(git_buf *buf, const char *path) return git_futils_readbuffer_updated(buf, path, NULL, NULL, NULL); } +int git_futils_writebuffer( + const git_buf *buf, const char *path, int flags, mode_t mode) +{ + int fd, error = 0; + + if (flags <= 0) + flags = O_CREAT | O_TRUNC | O_WRONLY; + if (!mode) + mode = GIT_FILEMODE_BLOB; + + if ((fd = p_open(path, flags, mode)) < 0) { + giterr_set(GITERR_OS, "Could not open '%s' for writing", path); + return fd; + } + + if ((error = p_write(fd, git_buf_cstr(buf), git_buf_len(buf))) < 0) { + giterr_set(GITERR_OS, "Could not write to '%s'", path); + (void)p_close(fd); + } + + if ((error = p_close(fd)) < 0) + giterr_set(GITERR_OS, "Error while closing '%s'", path); + + return error; +} + int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode) { if (git_futils_mkpath2file(to, dirmode) < 0) diff --git a/src/fileops.h b/src/fileops.h index 3e214aab1..f4e059c83 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -22,6 +22,9 @@ extern int git_futils_readbuffer_updated( git_buf *obj, const char *path, time_t *mtime, size_t *size, int *updated); extern int git_futils_readbuffer_fd(git_buf *obj, git_file fd, size_t len); +extern int git_futils_writebuffer( + const git_buf *buf, const char *path, int open_flags, mode_t mode); + /** * File utils * |