summaryrefslogtreecommitdiff
path: root/src/repository.c
Commit message (Collapse)AuthorAgeFilesLines
* Fix warning C4133 incompatible types in MSVCSven Strickroth2018-11-251-2/+2
| | | | | | Introduced in commit b433a22a979ae78c28c8b16f8c3487e2787cb73e. Signed-off-by: Sven Strickroth <email@cs-ware.de>
* repository: load_config for non-repo configsEdward Thomson2018-10-201-6/+8
| | | | | | | Teach `load_config` how to load all the configurations except (optionally) the repository configuration. This allows the new repository codepath to load the global/xdg/system configuration paths so that they can be inspected during repository initialization.
* win32: emulate Git for Windows in symlink supportEdward Thomson2018-10-201-10/+42
| | | | | | | | | | | | | | | Emulate the Git for Windows `core.symlinks` support. Since symbolic links are generally enabled for Administrator (and _may_ be enabled due to enabling Developer mode) but symbolic links are still sufficiently uncommon on Windows that Git users are expected to explicitly opt-in to symbolic links by enabling `core.symlinks=true` in a global (or xdg or system) configuration. When `core.symlinks=true` is set globally _and_ symbolic links support is detected then new repositories created will not have a `core.symlinks` set. If `core.symlinks` is _not_ set then no detection will be performed, and `core.symlinks=false` will be set in the repository configuration.
* Convert usage of `git_buf_free` to new `git_buf_dispose`Patrick Steinhardt2018-06-101-56/+56
|
* repository: being a worktree means we're not really bareEtienne Samson2018-05-071-3/+7
| | | | | | | We were previously conflating any error into GIT_ENOTFOUND, which might or might not be correct. This fixes the code so a config error is bubbled up, as well as preserving the semantics in the face of worktree-repositories
* refcount: make refcounting conform to aliasing rulesPatrick Steinhardt2017-11-181-1/+1
| | | | | | | | | | | | | | | | | | | | | Strict aliasing rules dictate that for most data types, you are not allowed to cast them to another data type and then access the casted pointers. While this works just fine for most compilers, technically we end up in undefined behaviour when we hurt that rule. Our current refcounting code makes heavy use of casting and thus violates that rule. While we didn't have any problems with that code, Travis started spitting out a lot of warnings due to a change in their toolchain. In the refcounting case, the code is also easy to fix: as all refcounting-statements are actually macros, we can just access the `rc` field directly instead of casting. There are two outliers in our code where that doesn't work. Both the `git_diff` and `git_patch` structures have specializations for generated and parsed diffs/patches, which directly inherit from them. Because of that, the refcounting code is only part of the base structure and not of the children themselves. We can help that by instead passing their base into `GIT_REFCOUNT_INC`, though.
* config: pass repository when opening config filesPatrick Steinhardt2017-10-091-6/+6
| | | | | | | | | | | | | | | | | Our current configuration logic is completely oblivious of any repository, but only cares for actual file paths. Unfortunately, we are forced to break this assumption by the introduction of conditional includes, which are evaluated in the context of a repository. Right now, only one conditional exists with "gitdir:" -- it will only include the configuration if the current repository's git directory matches the value passed to "gitdir:". To support these conditionals, we have to break our API and make the repository available when opening a configuration file. This commit extends the `open` call of configuration backends to include another repository and adjusts existing code to have it available. This includes the user-visible functions `git_config_add_file_ondisk` and `git_config_add_backend`.
* repository: constify several repo parameters for gettersPatrick Steinhardt2017-10-091-6/+6
| | | | | | | Several functions to retrieve variables from a repository only return immutable values, which allows us to actually constify the passed-in repository parameter. Do so to help a later patch, which will only have access to a constant repository.
* Make sure to always include "common.h" firstPatrick Steinhardt2017-07-031-1/+3
| | | | | | | | | | | | | | | | | | | | | | Next to including several files, our "common.h" header also declares various macros which are then used throughout the project. As such, we have to make sure to always include this file first in all implementation files. Otherwise, we might encounter problems or even silent behavioural differences due to macros or defines not being defined as they should be. So in fact, our header and implementation files should make sure to always include "common.h" first. This commit does so by establishing a common include pattern. Header files inside of "src" will now always include "common.h" as its first other file, separated by a newline from all the other includes to make it stand out as special. There are two cases for the implementation files. If they do have a matching header file, they will always include this one first, leading to "common.h" being transitively included as first file. If they do not have a matching header file, they instead include "common.h" as first file themselves. This fixes the outlined problems and will become our standard practice for header and source files inside of the "src/" from now on.
* repository: remove trailing whitespaceAriel Davis2017-06-191-2/+2
|
* repository: do not initialize templates if dir is an empty stringAriel Davis2017-06-191-1/+7
|
* repository: don't fail to create config option in inmemory repoEdward Thomson2017-06-121-6/+3
| | | | | When in an in-memory repository - without a configuration file - do not fail to create a configuration object.
* repository_item_path: return ENOTFOUND when appropriateEdward Thomson2017-06-121-1/+1
| | | | | | | Disambiguate error values: return `GIT_ENOTFOUND` when the item cannot exist in the repository (perhaps because the repository is inmemory or otherwise not backed by a filesystem), return `-1` when there is a hard failure.
* repository_item_path: error messages lowercasedEdward Thomson2017-06-121-2/+2
|
* settings: rename `GIT_OPT_ENABLE_SYNCHRONOUS_OBJECT_CREATION`Patrick Steinhardt2017-06-081-0/+2
| | | | | | | | | | | Initially, the setting has been solely used to enable the use of `fsync()` when creating objects. Since then, the use has been extended to also cover references and index files. As the option is not yet part of any release, we can still correct this by renaming the option to something more sensible, indicating not only correlation to objects. This commit renames the option to `GIT_OPT_ENABLE_FSYNC_GITDIR`. We also move the variable from the object to repository source code.
* Merge pull request #4243 from pks-t/pks/submodule-workdirEdward Thomson2017-06-041-38/+59
|\ | | | | Submodule working directory
| * repository: make check if repo is a worktree more strictPatrick Steinhardt2017-05-191-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | To determine if a repository is a worktree or not, we currently check for the existence of a "gitdir" file inside of the repository's gitdir. While this is sufficient for non-broken repositories, we have at least one case of a subtly broken repository where there exists a gitdir file inside of a gitmodule. This will cause us to misidentify the submodule as a worktree. While this is not really a fault of ours, we can do better here by observing that a repository can only ever be a worktree iff its common directory and dotgit directory are different. This allows us to make our check whether a repo is a worktree or not more strict by doing a simple string comparison of these two directories. This will also allow us to do the right thing in the above case of a broken repository, as for submodules these directories will be the same. At the same time, this allows us to skip the `stat` check for the "gitdir" file for most repositories.
| * repository: factor out worktree checkPatrick Steinhardt2017-05-191-5/+19
| | | | | | | | | | | | | | The check whether a repository is a worktree or not is currently done inside of `git_repository_open_ext`. As we want to extend this function later on, pull it out into its own function `repo_is_worktree` to ease working on it.
| * repository: improve parameter names for `find_repo`Patrick Steinhardt2017-05-191-35/+35
| | | | | | | | | | | | | | | | | | | | | | The out-parameters of `find_repo` containing found paths of a repository are a tad confusing, as they are not as obvious as they could be. Rename them like following to ease reading the code: - `repo_path` -> `gitdir_path` - `parent_path` -> `workdir_path` - `link_path` -> `gitlink_path` - `common_path` -> `commondir_path`
| * repository: clear out-parameter instead of freeing itPatrick Steinhardt2017-05-191-1/+1
| | | | | | | | | | | | | | | | The `path` out-parameter of `find_repo` is being sanitized initially such that we do not try to append to existing content. The sanitization is done via `git_buf_free`, though, which forces us to needlessly reallocate the buffer later in the function. Fix this by using `git_buf_clear` instead.
* | Merge pull request #4174 from libgit2/ethomson/set_head_to_tagCarlos Martín Nieto2017-05-201-37/+40
|\ \ | |/ |/| git_repository_set_head: use tag name in reflog
| * git_repository_set_head: use remote name in reflogethomson/set_head_to_tagEdward Thomson2017-04-031-2/+4
| | | | | | | | | | | | When `git_repository_set_head` is provided a remote reference, update the reflog with the tag name, like we do with a branch. This helps consumers match the semantics of `git checkout remote`.
| * git_repository_set_head: use tag name in reflogEdward Thomson2017-03-211-37/+38
| | | | | | | | | | | | When `git_repository_set_head` is provided a tag reference, update the reflog with the tag name, like we do with a branch. This helps consumers match the semantics of `git checkout tag`.
* | worktrees: cleanup some memory leaksEdward Thomson2017-05-011-1/+2
| | | | | | | | | | Be sure to clean up looked up references. Free buffers instead of merely clearing them. Use `git__free` instead of `free`.
* | repository: add function to iterate over all HEADsPatrick Steinhardt2017-04-051-0/+32
| | | | | | | | | | | | | | | | While we already provide functions to get the current repository's HEAD, it is quite involved to iterate over HEADs of both the repository and all linked work trees. This commit implements a function `git_repository_foreach_head`, which accepts a callback which is then called for all HEAD files.
* | repository: get worktree HEAD via `git_reference__read_head`Patrick Steinhardt2017-04-051-50/+23
| | | | | | | | | | | | | | The functions `git_repository_head_for_worktree` and `git_repository_detached_head_for_worktree` both implement their own logic to read the HEAD reference file. Use the new function `git_reference__read_head` instead to unify the code paths.
* | repository: extract function to get path to a file in a work treePatrick Steinhardt2017-04-051-11/+8
| | | | | | | | | | | | | | The function `read_worktree_head` has the logic embedded to construct the path to `HEAD` in the work tree's git directory, which is quite useful for other callers. Extract the logic into its own function to make it reusable by others.
* | repository: set error message if trying to set HEAD to a checked out onePatrick Steinhardt2017-04-051-0/+2
| | | | | | | | | | | | | | If trying to set the HEAD of a repository to another reference, we have to check whether this reference is already checked out in another linked work tree. If it is, we will refuse setting the HEAD and return an error, but do not set a meaningful error message. Add one.
* | Honor `core.fsyncObjectFiles`ethomson/fsyncEdward Thomson2017-03-021-9/+13
|/
* strmap: remove GIT__USE_STRMAP macroPatrick Steinhardt2017-02-171-1/+0
|
* worktree: compute workdir for worktrees opened via their gitdirPatrick Steinhardt2017-02-131-4/+32
| | | | | | | | | | | | | | | | | | | | | When opening a worktree via the gitdir of its parent repository we fail to correctly set up the worktree's working directory. The problem here is two-fold: we first fail to see that the gitdir actually is a gitdir of a working tree and then subsequently fail to determine the working tree location from the gitdir. The first problem of not noticing a gitdir belongs to a worktree can be solved by checking for the existence of a `gitdir` file in the gitdir. This file points back to the gitlink file located in the working tree's working directory. As this file only exists for worktrees, it should be sufficient indication of the gitdir belonging to a worktree. The second problem, that is determining the location of the worktree's working directory, can then be solved by reading the `gitdir` file in the working directory's gitdir. When we now resolve relative paths and strip the final `.git` component, we have the actual worktree's working directory location.
* repository: rename `path_repository` and `path_gitlink`Patrick Steinhardt2017-02-131-24/+24
| | | | | | | | | | | | | The `path_repository` variable is actually confusing to think about, as it is not always clear what the repository actually is. It may either be the path to the folder containing worktree and .git directory, the path to .git itself, a worktree or something entirely different. Actually, the intent of the variable is to hold the path to the gitdir, which is either the .git directory or the bare repository. Rename the variable to `gitdir` to avoid confusion. While at it, also rename `path_gitlink` to `gitlink` to improve consistency.
* repository: restrict checking out checked out branchesPatrick Steinhardt2017-02-131-0/+6
| | | | | | If a branch is already checked out in a working tree we are not allowed to check out that branch in another repository. Introduce this restriction when setting a repository's HEAD.
* worktree: implement functions reading HEADPatrick Steinhardt2017-02-131-0/+85
| | | | | | Implement `git_repository_head_for_worktree` and `git_repository_head_detached_for_worktree` for directly accessing a worktree's HEAD without opening it as a `git_repository` first.
* worktree: implement `git_repository_open_from_worktree`Patrick Steinhardt2017-02-131-0/+31
| | | | | Add function `git_repository_open_from_worktree`, which allows to open a `git_worktree` as repository.
* repository: expose `repo_init_create_head`Patrick Steinhardt2017-02-131-2/+2
| | | | | Expose the function `repo_init_create_head` as `git_repository_create_head`.
* config: open configuration in commondirPatrick Steinhardt2017-02-131-2/+1
| | | | | | | A repository's configuartion file can always be found in the GIT_COMMON_DIR, which has been newly introduced. For normal repositories this does change nothing, but for working trees this change allows to access the shared configuration file.
* repository: introduce is_worktree variablePatrick Steinhardt2017-02-131-0/+11
|
* repository: use `git_repository_item_path`Patrick Steinhardt2017-02-131-1/+2
| | | | | | | | | | | | | | The recent introduction of the commondir variable of a repository requires callers to distinguish whether their files are part of the dot-git directory or the common directory shared between multpile worktrees. In order to take the burden from callers and unify knowledge on which files reside where, the `git_repository_item_path` function has been introduced which encapsulate this knowledge. Modify most existing callers of `git_repository_path` to use `git_repository_item_path` instead, thus making them implicitly aware of the common directory.
* repository: add function to retrieve paths for repo itemsPatrick Steinhardt2017-02-131-0/+61
|
* repository: introduce commondir variablePatrick Steinhardt2017-02-131-15/+66
| | | | | | | | | The commondir variable stores the path to the common directory. The common directory is used to store objects and references shared across multiple repositories. A current use case is the newly introduced `git worktree` feature, which sets up a separate working copy, where the backing git object store and references are pointed to by the common directory.
* Allow for caching of submodules.Brock Peabody2017-01-201-0/+33
| | | | | | | | | | | | | | Added `git_repository_submodule_cache_all` to initialze a cache of submodules on the repository so that operations looking up N submodules are O(N) and not O(N^2). Added a `git_repository_submodule_cache_clear` function to remove the cache. Also optimized the function that loads all submodules as it was itself O(N^2) w.r.t the number of submodules, having to loop through the `.gitmodules` file once per submodule. I changed it to process the `.gitmodules` file once, into a map. Signed-off-by: David Turner <dturner@twosigma.com>
* giterr_set: consistent error messagesEdward Thomson2016-12-291-13/+13
| | | | | | | | Error messages should be sentence fragments, and therefore: 1. Should not begin with a capital letter, 2. Should not conclude with punctuation, and 3. Should not end a sentence and begin a new one
* repository: do not interpret all files as gitlinks in discoveryPatrick Steinhardt2016-11-141-1/+1
| | | | | | | | | | | | | | When trying to find a discovery, we walk up the directory structure checking if there is a ".git" file or directory and, if so, check its validity. But in the case that we've got a ".git" file, we do not want to unconditionally assume that the file is in fact a ".git" file and treat it as such, as we would error out if it is not. Fix the issue by only treating a file as a gitlink file if it ends with "/.git". This allows users of the function to discover a repository by handing in any path contained inside of a git repository.
* git_repository_open_ext: fix handling of $GIT_NAMESPACEJosh Triplett2016-11-111-5/+8
| | | | | | | | | | | | The existing code would set a namespace of "" (empty string) with GIT_NAMESPACE unset. In a repository where refs/heads/namespaces/ exists, that can produce incorrect results. Detect that case and avoid setting the namespace at all. Since that makes the last assignment to error conditional, and the previous assignment can potentially get GIT_ENOTFOUND, set error to 0 explicitly to prevent the call from incorrectly failing with GIT_ENOTFOUND.
* repository: don't cast to `int` for no reasonEdward Thomson2016-07-241-4/+4
| | | | | And give it a default so that some compilers don't (unnecessarily) complain.
* find_repo: Clean up and simplify logicJosh Triplett2016-06-241-22/+30
| | | | | | | | | | | | | | | find_repo had a complex loop and heavily nested conditionals, making it difficult to follow. Simplify this as much as possible: - Separate assignments from conditionals. - Check the complex loop condition in the only place it can change. - Break out of the loop on error, rather than going through the rest of the loop body first. - Handle error cases by immediately breaking, rather than nesting conditionals. - Free repo_link unconditionally on the way out of the function, rather than in multiple places. - Add more comments on the remaining complex steps.
* Add GIT_REPOSITORY_OPEN_FROM_ENV flag to respect $GIT_* environment varsJosh Triplett2016-06-241-0/+169
| | | | | | | | | | | | | | | | | | | | | git_repository_open_ext provides parameters for the start path, whether to search across filesystems, and what ceiling directories to stop at. git commands have standard environment variables and defaults for each of those, as well as various other parameters of the repository. To avoid duplicate environment variable handling in users of libgit2, add a GIT_REPOSITORY_OPEN_FROM_ENV flag, which makes git_repository_open_ext automatically handle the appropriate environment variables. Commands that intend to act just like those built into git itself can use this flag to get the expected default behavior. git_repository_open_ext with the GIT_REPOSITORY_OPEN_FROM_ENV flag respects $GIT_DIR, $GIT_DISCOVERY_ACROSS_FILESYSTEM, $GIT_CEILING_DIRECTORIES, $GIT_INDEX_FILE, $GIT_NAMESPACE, $GIT_OBJECT_DIRECTORY, and $GIT_ALTERNATE_OBJECT_DIRECTORIES. In the future, when libgit2 gets worktree support, git_repository_open_env will also respect $GIT_WORK_TREE and $GIT_COMMON_DIR; until then, git_repository_open_ext with this flag will error out if either $GIT_WORK_TREE or $GIT_COMMON_DIR is set.
* Add GIT_REPOSITORY_OPEN_NO_DOTGIT flag to avoid appending /.gitJosh Triplett2016-06-241-7/+10
| | | | | | | | | | GIT_REPOSITORY_OPEN_NO_SEARCH does not search up through parent directories, but still tries the specified path both directly and with /.git appended. GIT_REPOSITORY_OPEN_BARE avoids appending /.git, but opens the repository in bare mode even if it has a working directory. To support the semantics git uses when given $GIT_DIR in the environment, provide a new GIT_REPOSITORY_OPEN_NO_DOTGIT flag to not try appending /.git.
* Fix repository discovery with ceiling_dirs at current directoryJosh Triplett2016-06-241-17/+25
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | git only checks ceiling directories when its search ascends to a parent directory. A ceiling directory matching the starting directory will not prevent git from finding a repository in the starting directory or a parent directory. libgit2 handled the former case correctly, but differed from git in the latter case: given a ceiling directory matching the starting directory, but no repository at the starting directory, libgit2 would stop the search at that point rather than finding a repository in a parent directory. Test case using git command-line tools: /tmp$ git init x Initialized empty Git repository in /tmp/x/.git/ /tmp$ cd x/ /tmp/x$ mkdir subdir /tmp/x$ cd subdir/ /tmp/x/subdir$ GIT_CEILING_DIRECTORIES=/tmp/x git rev-parse --git-dir fatal: Not a git repository (or any of the parent directories): .git /tmp/x/subdir$ GIT_CEILING_DIRECTORIES=/tmp/x/subdir git rev-parse --git-dir /tmp/x/.git Fix the testsuite to test this case (in one case fixing a test that depended on the current behavior), and then fix find_repo to handle this case correctly. In the process, simplify and document the logic in find_repo(): - Separate the concepts of "currently checking a .git directory" and "number of iterations left before going further counts as a search" into two separate variables, in_dot_git and min_iterations. - Move the logic to handle in_dot_git and append /.git to the top of the loop. - Only search ceiling_dirs and find ceiling_offset after running out of min_iterations; since ceiling_offset only tracks the longest matching ceiling directory, if ceiling_dirs contained both the current directory and a parent directory, this change makes find_repo stop the search at the parent directory.