diff options
author | Russell Belfer <rb@github.com> | 2012-08-01 04:02:54 +0400 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2012-08-23 03:07:19 +0400 |
commit | ca1b6e54095a7e28d468a832f143025feae6cd4f (patch) | |
tree | 0ff9b9fbf71cd4f0489985b64f57590687361cd2 /src | |
parent | 662880ca60e4d1662bb10648522242ac54797720 (diff) |
Add template dir and set gid to repo init
This extends git_repository_init_ext further with support for
initializing the repository from an external template directory
and with support for the "create shared" type flags that make a
set GID repository directory.
This also adds tests for much of the new functionality to the
existing `repo/init.c` test suite.
Also, this adds a bunch of new utility functions including a
very general purpose `git_futils_mkdir` (with the ability to
make paths and to chmod the paths post-creation) and a file
tree copying function `git_futils_cp_r`. Also, this includes
some new path functions that were useful to keep the code
simple.
Diffstat (limited to 'src')
-rw-r--r-- | src/attr_file.c | 15 | ||||
-rw-r--r-- | src/config.c | 25 | ||||
-rw-r--r-- | src/fileops.c | 338 | ||||
-rw-r--r-- | src/fileops.h | 83 | ||||
-rw-r--r-- | src/path.c | 46 | ||||
-rw-r--r-- | src/path.h | 14 | ||||
-rw-r--r-- | src/posix.h | 7 | ||||
-rw-r--r-- | src/repository.c | 222 | ||||
-rw-r--r-- | src/repository.h | 1 | ||||
-rw-r--r-- | src/unix/posix.h | 1 | ||||
-rw-r--r-- | src/win32/posix.h | 8 |
11 files changed, 594 insertions, 166 deletions
diff --git a/src/attr_file.c b/src/attr_file.c index 20b3cf631..b2f312e3e 100644 --- a/src/attr_file.c +++ b/src/attr_file.c @@ -250,18 +250,15 @@ git_attr_assignment *git_attr_rule__lookup_assignment( int git_attr_path__init( git_attr_path *info, const char *path, const char *base) { + ssize_t root; + /* build full path as best we can */ git_buf_init(&info->full, 0); - if (base != NULL && git_path_root(path) < 0) { - if (git_buf_joinpath(&info->full, base, path) < 0) - return -1; - info->path = info->full.ptr + strlen(base); - } else { - if (git_buf_sets(&info->full, path) < 0) - return -1; - info->path = info->full.ptr; - } + if (git_path_join_unrooted(&info->full, path, base, &root) < 0) + return -1; + + info->path = info->full.ptr + root; /* remove trailing slashes */ while (info->full.size > 0) { diff --git a/src/config.c b/src/config.c index 44cfe760c..3ca49714c 100644 --- a/src/config.c +++ b/src/config.c @@ -515,3 +515,28 @@ int git_config_open_global(git_config **out) return error; } +int git_config_open_outside_repo(git_config **out) +{ + int error; + git_config *cfg = NULL; + git_buf buf = GIT_BUF_INIT; + + error = git_config_new(&cfg); + + if (!error && !git_config_find_global_r(&buf)) + error = git_config_add_file_ondisk(cfg, buf.ptr, 2); + + if (!error && !git_config_find_system_r(&buf)) + error = git_config_add_file_ondisk(cfg, buf.ptr, 1); + + git_buf_free(&buf); + + if (error && cfg) { + git_config_free(cfg); + cfg = NULL; + } + + *out = cfg; + + return error; +} diff --git a/src/fileops.c b/src/fileops.c index 70c5c387c..5aa6632e0 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -10,19 +10,8 @@ int git_futils_mkpath2file(const char *file_path, const mode_t mode) { - int result = 0; - git_buf target_folder = GIT_BUF_INIT; - - if (git_path_dirname_r(&target_folder, file_path) < 0) - return -1; - - /* Does the containing folder exist? */ - if (git_path_isdir(target_folder.ptr) == false) - /* Let's create the tree structure */ - result = git_futils_mkdir_r(target_folder.ptr, NULL, mode); - - git_buf_free(&target_folder); - return result; + return git_futils_mkdir( + file_path, NULL, mode, GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST); } int git_futils_mktmp(git_buf *path_out, const char *filename) @@ -239,76 +228,90 @@ void git_futils_mmap_free(git_map *out) p_munmap(out); } -int git_futils_mkdir_q(const char *path, const mode_t mode) -{ - if (p_mkdir(path, mode) < 0 && errno != EEXIST) { - giterr_set(GITERR_OS, "Failed to create directory at '%s'", path); - return -1; - } - - return 0; -} - -int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode) +int git_futils_mkdir( + const char *path, + const char *base, + mode_t mode, + uint32_t flags) { git_buf make_path = GIT_BUF_INIT; - size_t start = 0; - char *pp, *sp; - bool failed = false; - - if (base != NULL) { - /* - * when a base is being provided, it is supposed to already exist. - * Therefore, no attempt is being made to recursively create this leading path - * segment. It's just skipped. */ - start = strlen(base); - if (git_buf_joinpath(&make_path, base, path) < 0) - return -1; - } else { - int root_path_offset; + ssize_t root = 0; + char lastch, *tail; - if (git_buf_puts(&make_path, path) < 0) - return -1; + /* build path and find "root" where we should start calling mkdir */ + if (git_path_join_unrooted(&make_path, path, base, &root) < 0) + return -1; - root_path_offset = git_path_root(make_path.ptr); - if (root_path_offset > 0) { - /* - * On Windows, will skip the drive name (eg. C: or D:) - * or the leading part of a network path (eg. //computer_name ) */ - start = root_path_offset; - } + if (make_path.size == 0) { + giterr_set(GITERR_OS, "Attempt to create empty path"); + goto fail; } - pp = make_path.ptr + start; - - while (!failed && (sp = strchr(pp, '/')) != NULL) { - if (sp != pp && git_path_isdir(make_path.ptr) == false) { - *sp = 0; - - /* Do not choke while trying to recreate an existing directory */ - if (p_mkdir(make_path.ptr, mode) < 0 && errno != EEXIST) - failed = true; + /* remove trailing slashes on path */ + while (make_path.ptr[make_path.size - 1] == '/') { + make_path.size--; + make_path.ptr[make_path.size] = '\0'; + } - *sp = '/'; + /* if we are not supposed to made the last element, truncate it */ + if ((flags & GIT_MKDIR_SKIP_LAST) != 0) + git_buf_rtruncate_at_char(&make_path, '/'); + + /* if we are not supposed to make the whole path, reset root */ + if ((flags & GIT_MKDIR_PATH) == 0) + root = git_buf_rfind(&make_path, '/'); + + /* clip root to make_path length */ + if (root >= (ssize_t)make_path.size) + root = (ssize_t)make_path.size - 1; + + tail = & make_path.ptr[root]; + + while (*tail) { + /* advance tail to include next path component */ + while (*tail == '/') + tail++; + while (*tail && *tail != '/') + tail++; + + /* truncate path at next component */ + lastch = *tail; + *tail = '\0'; + + /* make directory */ + if (p_mkdir(make_path.ptr, mode) < 0 && + (errno != EEXIST || (flags & GIT_MKDIR_EXCL) != 0)) + { + giterr_set(GITERR_OS, "Failed to make directory '%s'", + make_path.ptr); + goto fail; } - pp = sp + 1; - } + /* chmod if requested */ + if ((flags & GIT_MKDIR_CHMOD_PATH) != 0 || + ((flags & GIT_MKDIR_CHMOD) != 0 && lastch == '\0')) + { + if (p_chmod(make_path.ptr, mode) < 0) { + giterr_set(GITERR_OS, "Failed to set permissions on '%s'", + make_path.ptr); + goto fail; + } + } - if (*pp != '\0' && !failed) { - if (p_mkdir(make_path.ptr, mode) < 0 && errno != EEXIST) - failed = true; + *tail = lastch; } git_buf_free(&make_path); + return 0; - if (failed) { - giterr_set(GITERR_OS, - "Failed to create directory structure at '%s'", path); - return -1; - } +fail: + git_buf_free(&make_path); + return -1; +} - return 0; +int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode) +{ + return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH); } static int _rmdir_recurs_foreach(void *opaque, git_buf *path) @@ -505,3 +508,202 @@ int git_futils_fake_symlink(const char *old, const char *new) } return retcode; } + +static int git_futils_cp_fd(int ifd, int ofd, bool close_fd) +{ + int error = 0; + char buffer[4096]; + ssize_t len = 0; + + while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0) + /* p_write() does not have the same semantics as write(). It loops + * internally and will return 0 when it has completed writing. + */ + error = p_write(ofd, buffer, len); + + if (len < 0) { + giterr_set(GITERR_OS, "Read error while copying file"); + error = (int)len; + } + + if (close_fd) { + p_close(ifd); + p_close(ofd); + } + + return error; +} + +int git_futils_cp_withpath( + const char *from, const char *to, mode_t filemode, mode_t dirmode) +{ + int ifd, ofd; + + if (git_futils_mkpath2file(to, dirmode) < 0) + return -1; + + if ((ifd = git_futils_open_ro(from)) < 0) + return ifd; + + if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) { + if (errno == ENOENT || errno == ENOTDIR) + ofd = GIT_ENOTFOUND; + giterr_set(GITERR_OS, "Failed to open '%s' for writing", to); + p_close(ifd); + return ofd; + } + + return git_futils_cp_fd(ifd, ofd, true); +} + +static int git_futils_cplink( + const char *from, size_t from_filesize, const char *to) +{ + int error = 0; + ssize_t read_len; + char *link_data = git__malloc(from_filesize + 1); + GITERR_CHECK_ALLOC(link_data); + + read_len = p_readlink(from, link_data, from_filesize); + if (read_len != (ssize_t)from_filesize) { + giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from); + error = -1; + } + else { + link_data[read_len] = '\0'; + + if (p_symlink(link_data, to) < 0) { + giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'", + link_data, to); + error = -1; + } + } + + git__free(link_data); + return error; +} + +typedef struct { + const char *to_root; + git_buf to; + ssize_t from_prefix; + uint32_t flags; + uint32_t mkdir_flags; + mode_t dirmode; +} cp_r_info; + +static int _cp_r_callback(void *ref, git_buf *from) +{ + cp_r_info *info = ref; + struct stat from_st, to_st; + bool exists = false; + + if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 && + from->ptr[git_path_basename_offset(from)] == '.') + return 0; + + if (git_buf_joinpath( + &info->to, info->to_root, from->ptr + info->from_prefix) < 0) + return -1; + + if (p_lstat(info->to.ptr, &to_st) < 0) { + if (errno != ENOENT) { + giterr_set(GITERR_OS, + "Could not access %s while copying files", info->to.ptr); + return -1; + } + } else + exists = true; + + if (git_path_lstat(from->ptr, &from_st) < 0) + return -1; + + if (S_ISDIR(from_st.st_mode)) { + int error = 0; + mode_t oldmode = info->dirmode; + + /* if we are not chmod'ing, then overwrite dirmode */ + if ((info->flags & GIT_CPDIR_CHMOD) == 0) + info->dirmode = from_st.st_mode; + + /* make directory now if CREATE_EMPTY_DIRS is requested and needed */ + if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0) + error = git_futils_mkdir( + info->to.ptr, NULL, info->dirmode, info->mkdir_flags); + + /* recurse onto target directory */ + if (!exists || S_ISDIR(to_st.st_mode)) + error = git_path_direach(from, _cp_r_callback, info); + + if (oldmode != 0) + info->dirmode = oldmode; + + return error; + } + + if (exists) { + if ((info->flags & GIT_CPDIR_OVERWRITE) == 0) + return 0; + + if (p_unlink(info->to.ptr) < 0) { + giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'", + info->to.ptr); + return -1; + } + } + + /* Done if this isn't a regular file or a symlink */ + if (!S_ISREG(from_st.st_mode) && + (!S_ISLNK(from_st.st_mode) || + (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0)) + return 0; + + /* Make container directory on demand if needed */ + if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 && + git_futils_mkdir( + info->to.ptr, NULL, info->dirmode, info->mkdir_flags) < 0) + return -1; + + /* make symlink or regular file */ + if (S_ISLNK(from_st.st_mode)) + return git_futils_cplink(from->ptr, from_st.st_size, info->to.ptr); + else + return git_futils_cp_withpath( + from->ptr, info->to.ptr, from_st.st_mode, info->dirmode); +} + +int git_futils_cp_r( + const char *from, + const char *to, + uint32_t flags, + mode_t dirmode) +{ + int error; + git_buf path = GIT_BUF_INIT; + cp_r_info info; + + if (git_buf_sets(&path, from) < 0) + return -1; + + info.to_root = to; + info.flags = flags; + info.dirmode = dirmode; + info.from_prefix = path.size; + git_buf_init(&info.to, 0); + + /* precalculate mkdir flags */ + if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) { + info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST; + if ((flags & GIT_CPDIR_CHMOD) != 0) + info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH; + } else { + info.mkdir_flags = + ((flags & GIT_CPDIR_CHMOD) != 0) ? GIT_MKDIR_CHMOD : 0; + } + + error = _cp_r_callback(&info, &path); + + git_buf_free(&path); + + return error; +} diff --git a/src/fileops.h b/src/fileops.h index edfcb7dd0..6f3450373 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -48,17 +48,47 @@ extern int git_futils_creat_locked(const char *path, const mode_t mode); extern int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode); /** - * Create a directory if it does not exist + * Create a path recursively + * + * If a base parameter is being passed, it's expected to be valued with a + * path pointing to an already existing directory. */ -extern int git_futils_mkdir_q(const char *path, const mode_t mode); +extern int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode); /** - * Create a path recursively + * Flags to pass to `git_futils_mkdir`. * - * If a base parameter is being passed, it's expected to be valued with a path pointing to an already - * exisiting directory. + * * GIT_MKDIR_EXCL is "exclusive" - i.e. generate an error if dir exists. + * * GIT_MKDIR_PATH says to make all components in the path. + * * GIT_MKDIR_CHMOD says to chmod the final directory entry after creation + * * GIT_MKDIR_CHMOD_PATH says to chmod each directory component in the path + * * GIT_MKDIR_SKIP_LAST says to leave off the last element of the path + * + * Note that the chmod options will be executed even if the directory already + * exists, unless GIT_MKDIR_EXCL is given. */ -extern int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode); +typedef enum { + GIT_MKDIR_EXCL = 1, + GIT_MKDIR_PATH = 2, + GIT_MKDIR_CHMOD = 4, + GIT_MKDIR_CHMOD_PATH = 8, + GIT_MKDIR_SKIP_LAST = 16 +} git_futils_mkdir_flags; + +/** + * Create a directory or entire path. + * + * This makes a directory (and the entire path leading up to it if requested), + * and optionally chmods the directory immediately after (or each part of the + * path if requested). + * + * @param path The path to create. + * @param base Root for relative path. These directories will never be made. + * @param mode The mode to use for created directories. + * @param flags Combination of the mkdir flags above. + * @return 0 on success, else error code + */ +extern int git_futils_mkdir(const char *path, const char *base, mode_t mode, uint32_t flags); /** * Create all the folders required to contain @@ -100,6 +130,47 @@ extern int git_futils_mktmp(git_buf *path_out, const char *filename); extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode); /** + * Copy a file, creating the destination path if needed. + * + * The filemode will be used for the file and the dirmode will be used for + * any intervening directories if necessary. + */ +extern int git_futils_cp_withpath( + const char *from, + const char *to, + mode_t filemode, + mode_t dirmode); + +/** + * Flags that can be passed to `git_futils_cp_r`. + */ +typedef enum { + GIT_CPDIR_CREATE_EMPTY_DIRS = 1, + GIT_CPDIR_COPY_SYMLINKS = 2, + GIT_CPDIR_COPY_DOTFILES = 4, + GIT_CPDIR_OVERWRITE = 8, + GIT_CPDIR_CHMOD = 16 +} git_futils_cpdir_flags; + +/** + * Copy a directory tree. + * + * This copies directories and files from one root to another. You can + * pass a combinationof GIT_CPDIR flags as defined above. + * + * If you pass the CHMOD flag, then the dirmode will be applied to all + * directories that are created during the copy, overiding the natural + * permissions. If you do not pass the CHMOD flag, then the dirmode + * will actually be copied from the source files and the `dirmode` arg + * will be ignored. + */ +extern int git_futils_cp_r( + const char *from, + const char *to, + uint32_t flags, + mode_t dirmode); + +/** * Open a file readonly and set error if needed. */ extern int git_futils_open_ro(const char *path); diff --git a/src/path.c b/src/path.c index 22391c52b..15188850d 100644 --- a/src/path.c +++ b/src/path.c @@ -147,6 +147,20 @@ char *git_path_basename(const char *path) return basename; } +size_t git_path_basename_offset(git_buf *buffer) +{ + ssize_t slash; + + if (!buffer || buffer->size <= 0) + return 0; + + slash = git_buf_rfind_next(buffer, '/'); + + if (slash >= 0 && buffer->ptr[slash] == '/') + return (size_t)(slash + 1); + + return 0; +} const char *git_path_topdir(const char *path) { @@ -193,6 +207,31 @@ int git_path_root(const char *path) return -1; /* Not a real error - signals that path is not rooted */ } +int git_path_join_unrooted( + git_buf *path_out, const char *path, const char *base, ssize_t *root_at) +{ + int error, root; + + assert(path && path_out); + + root = git_path_root(path); + + if (base != NULL && root < 0) { + error = git_buf_joinpath(path_out, base, path); + + if (root_at) + *root_at = (ssize_t)strlen(base); + } + else { + error = git_buf_sets(path_out, path); + + if (root_at) + *root_at = (root < 0) ? 0 : (ssize_t)root; + } + + return error; +} + int git_path_prettify(git_buf *path_out, const char *path, const char *base) { char buf[GIT_PATH_MAX]; @@ -502,12 +541,7 @@ bool git_path_contains_file(git_buf *base, const char *file) int git_path_find_dir(git_buf *dir, const char *path, const char *base) { - int error; - - if (base != NULL && git_path_root(path) < 0) - error = git_buf_joinpath(dir, base, path); - else - error = git_buf_sets(dir, path); + int error = git_path_join_unrooted(dir, path, base, NULL); if (!error) { char buf[GIT_PATH_MAX]; diff --git a/src/path.h b/src/path.h index 14618b2fc..b6292277f 100644 --- a/src/path.h +++ b/src/path.h @@ -58,6 +58,11 @@ extern int git_path_dirname_r(git_buf *buffer, const char *path); extern char *git_path_basename(const char *path); extern int git_path_basename_r(git_buf *buffer, const char *path); +/* Return the offset of the start of the basename. Unlike the other + * basename functions, this returns 0 if the path is empty. + */ +extern size_t git_path_basename_offset(git_buf *buffer); + extern const char *git_path_topdir(const char *path); /** @@ -186,6 +191,15 @@ extern bool git_path_contains_dir(git_buf *parent, const char *subdir); extern bool git_path_contains_file(git_buf *dir, const char *file); /** + * Prepend base to unrooted path or just copy path over. + * + * This will optionally return the index into the path where the "root" + * is, either the end of the base directory prefix or the path root. + */ +extern int git_path_join_unrooted( + git_buf *path_out, const char *path, const char *base, ssize_t *root_at); + +/** * Clean up path, prepending base if it is not already rooted. */ extern int git_path_prettify(git_buf *path_out, const char *path, const char *base); diff --git a/src/posix.h b/src/posix.h index d35fe08a5..71bb82283 100644 --- a/src/posix.h +++ b/src/posix.h @@ -11,8 +11,15 @@ #include <fcntl.h> #include <time.h> +#ifndef S_IFGITLINK #define S_IFGITLINK 0160000 #define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK) +#endif + +/* if S_ISGID is not defined, then don't try to set it */ +#ifndef S_ISGID +#define S_ISGID 0 +#endif #if !defined(O_BINARY) #define O_BINARY 0 diff --git a/src/repository.c b/src/repository.c index 994b13bd5..ebd60360a 100644 --- a/src/repository.c +++ b/src/repository.c @@ -24,6 +24,8 @@ #define GIT_REPO_VERSION 0 +#define GIT_TEMPLATE_DIR "/usr/share/git-core/templates" + static void drop_odb(git_repository *repo) { if (repo->_odb != NULL) { @@ -681,6 +683,7 @@ static bool is_chmod_supported(const char *file_path) return false; _is_supported = (st1.st_mode != st2.st_mode); + return _is_supported; } @@ -702,20 +705,45 @@ cleanup: return _is_insensitive; } +static bool are_symlinks_supported(const char *wd_path) +{ + git_buf path = GIT_BUF_INIT; + int fd; + struct stat st; + static int _symlinks_supported = -1; + + if (_symlinks_supported > -1) + return _symlinks_supported; + + if ((fd = git_futils_mktmp(&path, wd_path)) < 0 || + p_close(fd) < 0 || + p_unlink(path.ptr) < 0 || + p_symlink("testing", path.ptr) < 0 || + p_lstat(path.ptr, &st) < 0) + _symlinks_supported = false; + else + _symlinks_supported = (S_ISLNK(st.st_mode) != 0); + + (void)p_unlink(path.ptr); + git_buf_free(&path); + + return _symlinks_supported; +} + static int repo_init_config( - const char *git_dir, git_repository_init_options *opts) + const char *repo_dir, + const char *work_dir, + git_repository_init_options *opts) { + int error = 0; git_buf cfg_path = GIT_BUF_INIT; git_config *config = NULL; -#define SET_REPO_CONFIG(type, name, val) {\ - if (git_config_set_##type(config, name, val) < 0) { \ - git_buf_free(&cfg_path); \ - git_config_free(config); \ - return -1; } \ -} +#define SET_REPO_CONFIG(TYPE, NAME, VAL) do {\ + if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \ + goto cleanup; } while (0) - if (git_buf_joinpath(&cfg_path, git_dir, GIT_CONFIG_FILENAME_INREPO) < 0) + if (git_buf_joinpath(&cfg_path, repo_dir, GIT_CONFIG_FILENAME_INREPO) < 0) return -1; if (git_config_open_ondisk(&config, git_buf_cstr(&cfg_path)) < 0) { @@ -724,12 +752,8 @@ static int repo_init_config( } if ((opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0 && - check_repositoryformatversion(config) < 0) - { - git_buf_free(&cfg_path); - git_config_free(config); - return -1; - } + (error = check_repositoryformatversion(config)) < 0) + goto cleanup; SET_REPO_CONFIG( bool, "core.bare", (opts->flags & GIT_REPOSITORY_INIT_BARE) != 0); @@ -738,25 +762,42 @@ static int repo_init_config( SET_REPO_CONFIG( bool, "core.filemode", is_chmod_supported(git_buf_cstr(&cfg_path))); - if (!(opts->flags & GIT_REPOSITORY_INIT_BARE)) + if (!(opts->flags & GIT_REPOSITORY_INIT_BARE)) { SET_REPO_CONFIG(bool, "core.logallrefupdates", true); + if (!are_symlinks_supported(work_dir)) + SET_REPO_CONFIG(bool, "core.symlinks", false); + + if (!(opts->flags & GIT_REPOSITORY_INIT__NATURAL_WD)) { + SET_REPO_CONFIG(string, "core.worktree", work_dir); + } + else if ((opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0) { + if ((error = git_config_delete(config, "core.worktree")) < 0) + goto cleanup; + } + } else { + if (!are_symlinks_supported(repo_dir)) + SET_REPO_CONFIG(bool, "core.symlinks", false); + } + if (!(opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) && - is_filesystem_case_insensitive(git_dir)) + is_filesystem_case_insensitive(repo_dir)) SET_REPO_CONFIG(bool, "core.ignorecase", true); - if (opts->flags & GIT_REPOSITORY_INIT_SHARED_GROUP) { + if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP) { SET_REPO_CONFIG(int32, "core.sharedrepository", 1); SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true); - } else if (opts->flags & GIT_REPOSITORY_INIT_SHARED_ALL) { + } + else if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL) { SET_REPO_CONFIG(int32, "core.sharedrepository", 2); SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true); } +cleanup: git_buf_free(&cfg_path); git_config_free(config); - return 0; + return error; } static int repo_write_template( @@ -848,6 +889,17 @@ cleanup: return error; } +static mode_t pick_dir_mode(git_repository_init_options *opts) +{ + if (opts->mode == GIT_REPOSITORY_INIT_SHARED_UMASK) + return 0755; + if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP) + return (0775 | S_ISGID); + if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL) + return (0777 | S_ISGID); + return opts->mode; +} + #include "repo_template.h" static int repo_init_structure( @@ -855,8 +907,11 @@ static int repo_init_structure( const char *work_dir, git_repository_init_options *opts) { + int error = 0; repo_template_item *tpl; - mode_t gid = 0; + bool external_tpl = + ((opts->flags & GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE) != 0); + mode_t dmode = pick_dir_mode(opts); /* Hide the ".git" directory */ if ((opts->flags & GIT_REPOSITORY_INIT_BARE) != 0) { @@ -874,32 +929,60 @@ static int repo_init_structure( return -1; } - if ((opts->flags & GIT_REPOSITORY_INIT_SHARED_GROUP) != 0 || - (opts->flags & GIT_REPOSITORY_INIT_SHARED_ALL) != 0) - gid = S_ISGID; + /* Copy external template if requested */ + if (external_tpl) { + git_config *cfg; + const char *tdir; - /* TODO: honor GIT_REPOSITORY_INIT_USE_EXTERNAL_TEMPLATE if set */ + if (opts->template_path) + tdir = opts->template_path; + else if ((error = git_config_open_outside_repo(&cfg)) < 0) + return error; + else { + error = git_config_get_string(&tdir, cfg, "init.templatedir"); - /* Copy internal template as needed */ + git_config_free(cfg); - for (tpl = repo_template; tpl->path; ++tpl) { - if (!tpl->content) { - if (git_futils_mkdir_r(tpl->path, repo_dir, tpl->mode | gid) < 0) - return -1; + if (error && error != GIT_ENOTFOUND) + return error; + + giterr_clear(); + tdir = GIT_TEMPLATE_DIR; } - else { + + error = git_futils_cp_r(tdir, repo_dir, + GIT_CPDIR_COPY_SYMLINKS | GIT_CPDIR_CHMOD, dmode); + + if (error < 0) { + if (strcmp(tdir, GIT_TEMPLATE_DIR) != 0) + return error; + + /* if template was default, ignore error and use internal */ + giterr_clear(); + external_tpl = false; + } + } + + /* Copy internal template + * - always ensure existence of dirs + * - only create files if no external template was specified + */ + for (tpl = repo_template; !error && tpl->path; ++tpl) { + if (!tpl->content) + error = git_futils_mkdir( + tpl->path, repo_dir, dmode, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD); + else if (!external_tpl) { const char *content = tpl->content; if (opts->description && strcmp(tpl->path, GIT_DESC_FILE) == 0) content = opts->description; - if (repo_write_template( - repo_dir, false, tpl->path, tpl->mode, false, content) < 0) - return -1; + error = repo_write_template( + repo_dir, false, tpl->path, tpl->mode, false, content); } } - return 0; + return error; } static int repo_init_directories( @@ -910,6 +993,7 @@ static int repo_init_directories( { int error = 0; bool add_dotgit, has_dotgit, natural_wd; + mode_t dirmode; /* set up repo path */ @@ -930,15 +1014,9 @@ static int repo_init_directories( if ((opts->flags & GIT_REPOSITORY_INIT_BARE) == 0) { if (opts->workdir_path) { - if (git_path_root(opts->workdir_path) < 0) { - if (git_path_dirname_r(wd_path, repo_path->ptr) < 0 || - git_buf_putc(wd_path, '/') < 0 || - git_buf_puts(wd_path, opts->workdir_path) < 0) - return -1; - } else { - if (git_buf_sets(wd_path, opts->workdir_path) < 0) - return -1; - } + if (git_path_join_unrooted( + wd_path, opts->workdir_path, repo_path->ptr, NULL) < 0) + return -1; } else if (has_dotgit) { if (git_path_dirname_r(wd_path, repo_path->ptr) < 0) return -1; @@ -962,43 +1040,33 @@ static int repo_init_directories( if (natural_wd) opts->flags |= GIT_REPOSITORY_INIT__NATURAL_WD; - /* pick mode */ - - if ((opts->flags & GIT_REPOSITORY_INIT_SHARED_CUSTOM) != 0) - /* leave mode as is */; - else if ((opts->flags & GIT_REPOSITORY_INIT_SHARED_GROUP) != 0) - opts->mode = 0775 | S_ISGID; - else if ((opts->flags & GIT_REPOSITORY_INIT_SHARED_ALL) != 0) - opts->mode = 0777 | S_ISGID; - else if ((opts->flags & GIT_REPOSITORY_INIT_BARE) != 0) - opts->mode = 0755; - else - opts->mode = 0755; - /* create directories as needed / requested */ - if ((opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0) { - error = git_futils_mkdir_r(repo_path->ptr, NULL, opts->mode); + dirmode = pick_dir_mode(opts); - if (!error && !natural_wd && wd_path->size > 0) - error = git_futils_mkdir_r(wd_path->ptr, NULL, opts->mode); + if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 && has_dotgit) { + git_buf p = GIT_BUF_INIT; + if ((error = git_path_dirname_r(&p, repo_path->ptr)) >= 0) + error = git_futils_mkdir(p.ptr, NULL, dirmode, 0); + git_buf_free(&p); } - else if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0) { - if (has_dotgit) { - git_buf p = GIT_BUF_INIT; - if ((error = git_path_dirname_r(&p, repo_path->ptr)) >= 0) - error = git_futils_mkdir_q(p.ptr, opts->mode); - git_buf_free(&p); - } - if (!error) - error = git_futils_mkdir_q(repo_path->ptr, opts->mode); - - if (!error && !natural_wd && wd_path->size > 0) - error = git_futils_mkdir_q(wd_path->ptr, opts->mode); + if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 || + (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0 || + has_dotgit) + { + uint32_t mkflag = GIT_MKDIR_CHMOD; + if ((opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0) + mkflag |= GIT_MKDIR_PATH; + error = git_futils_mkdir(repo_path->ptr, NULL, dirmode, mkflag); } - else if (has_dotgit) - error = git_futils_mkdir_q(repo_path->ptr, opts->mode); + + if (wd_path->size > 0 && + !natural_wd && + ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 || + (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0)) + error = git_futils_mkdir(wd_path->ptr, NULL, dirmode & ~S_ISGID, + (opts->flags & GIT_REPOSITORY_INIT_MKPATH) ? GIT_MKDIR_PATH : 0); /* prettify both directories now that they are created */ @@ -1063,14 +1131,16 @@ int git_repository_init_ext( opts->flags |= GIT_REPOSITORY_INIT__IS_REINIT; - error = repo_init_config(git_buf_cstr(&repo_path), opts); + error = repo_init_config( + git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts); /* TODO: reinitialize the templates */ } else { if (!(error = repo_init_structure( git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts)) && - !(error = repo_init_config(git_buf_cstr(&repo_path), opts))) + !(error = repo_init_config( + git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts))) error = repo_init_create_head( git_buf_cstr(&repo_path), opts->initial_head); } diff --git a/src/repository.h b/src/repository.h index dd42c63e1..4695edf3a 100644 --- a/src/repository.h +++ b/src/repository.h @@ -75,7 +75,6 @@ enum { GIT_REPOSITORY_INIT__IS_REINIT = (1u << 18), }; - /** Base git object for inheritance */ struct git_object { git_cached_obj cached; diff --git a/src/unix/posix.h b/src/unix/posix.h index 7a3a388ec..45d2b7238 100644 --- a/src/unix/posix.h +++ b/src/unix/posix.h @@ -18,6 +18,7 @@ #define p_lstat(p,b) lstat(p,b) #define p_readlink(a, b, c) readlink(a, b, c) +#define p_symlink(o,n) symlink(o, n) #define p_link(o,n) link(o, n) #define p_symlink(o,n) symlink(o,n) #define p_unlink(p) unlink(p) diff --git a/src/win32/posix.h b/src/win32/posix.h index 14caae418..def3a766a 100644 --- a/src/win32/posix.h +++ b/src/win32/posix.h @@ -19,6 +19,14 @@ GIT_INLINE(int) p_link(const char *old, const char *new) return -1; } +GIT_INLINE(int) p_symlink(const char *old, const char *new) +{ + GIT_UNUSED(old); + GIT_UNUSED(new); + errno = ENOSYS; + return -1; +} + GIT_INLINE(int) p_mkdir(const char *path, mode_t mode) { wchar_t* buf = gitwin_to_utf16(path); |