summaryrefslogtreecommitdiff
path: root/src/tree.c
Commit message (Collapse)AuthorAgeFilesLines
* array: fix dereference from void * typePeter Pettersson2021-08-251-1/+2
|
* path: git_path_isvalid -> git_path_validateEdward Thomson2021-04-141-1/+1
| | | | | | | If we want to validate more and different types of paths, the name `git_path_validate` makes that easier and more expressive. We can add, for example, `git_path_validate_foo` while the current name makes that less ergonomic.
* tree: deprecate `git_treebuilder_write_with_buffer`Edward Thomson2021-03-041-56/+67
| | | | | | | | | The function `git_treebuilder_write_with_buffer` is unnecessary; it is used internally as part of treebuilder writing, but it has little use to external callers. For callers that repeatedly write a treebuilder, we can supply them with a buffer in the treebuilder struct instead of recreating it. For ourselves, when we want a single buffer in our write loop, we can use an internal function.
* tree: use GIT_ASSERTEdward Thomson2020-11-271-20/+31
|
* tree functions: return an intEdward Thomson2020-01-241-2/+6
| | | | | Stop returning a void for functions, future-proofing them to allow them to fail.
* fileops: rename to "futils.h" to match function signaturesPatrick Steinhardt2019-07-201-1/+1
| | | | | | | | | Our file utils functions all have a "futils" prefix, e.g. `git_futils_touch`. One would thus naturally guess that their definitions and implementation would live in files "futils.h" and "futils.c", respectively, but in fact they live in "fileops.h". Rename the files to match expectations.
* tree: return `size_t` for treebuilder entrycountEdward Thomson2019-06-241-1/+1
| | | | | | | We keep the treebuilder entrycount as a `size_t` - return that instead of downcasting to an `unsigned int`. Callers who were storing this value in an `unsigned int` will continue to downcast themselves, so there should be no behavior change for callers.
* oid: `is_zero` instead of `iszero`Edward Thomson2019-06-161-1/+1
| | | | | | The only function that is named `issomething` (without underscore) was `git_oid_iszero`. Rename it to `git_oid_is_zero` for consistency with the rest of the library.
* strmap: introduce high-level setter for key/value pairsPatrick Steinhardt2019-02-151-5/+2
| | | | | | | | | | | | Currently, one would use the function `git_strmap_insert` to insert key/value pairs into a map. This function has historically been a macro, which is why its syntax is kind of weird: instead of returning an error code directly, it instead has to be passed a pointer to where the return value shall be stored. This does not match libgit2's common idiom of directly returning error codes. Introduce a new function `git_strmap_set`, which takes as parameters the map, key and value and directly returns an error code. Convert all callers of `git_strmap_insert` to make use of it.
* strmap: introduce `git_strmap_get` and use it throughout the treePatrick Steinhardt2019-02-151-13/+2
| | | | | | | | | | | | | | The current way of looking up an entry from a map is tightly coupled with the map implementation, as one first has to look up the index of the key and then retrieve the associated value by using the index. As a caller, you usually do not care about any indices at all, though, so this is more complicated than really necessary. Furthermore, it invites for errors to happen if the correct error checking sequence is not being followed. Introduce a new high-level function `git_strmap_get` that takes a map and a key and returns a pointer to the associated value if such a key exists. Otherwise, a `NULL` pointer is returned. Adjust all callers that can trivially be converted.
* maps: provide a uniform entry count interfacePatrick Steinhardt2019-02-151-2/+2
| | | | | | | | | There currently exist two different function names for getting the entry count of maps, where offmaps offset and string maps use `num_entries` and OID maps use `size`. In most programming languages with built-in map types, this is simply called `size`, which is also shorter to type. Thus, this commit renames the other two functions `num_entries` to match the common way and adjusts all callers.
* maps: use uniform lifecycle management functionsPatrick Steinhardt2019-02-151-1/+1
| | | | | | | | | | | | | | | | Currently, the lifecycle functions for maps (allocation, deallocation, resize) are not named in a uniform way and do not have a uniform function signature. Rename the functions to fix that, and stick to libgit2's naming scheme of saying `git_foo_new`. This results in the following new interface for allocation: - `int git_<t>map_new(git_<t>map **out)` to allocate a new map, returning an error code if we ran out of memory - `void git_<t>map_free(git_<t>map *map)` to free a map - `void git_<t>map_clear(git<t>map *map)` to remove all entries from a map This commit also fixes all existing callers.
* tree: cast filename length in git_tree__parse_rawEdward Thomson2019-01-251-2/+2
| | | | | Quiet down a warning from MSVC about how we're potentially losing data. Ensure that we're within a uint16_t before we do.
* git_error: use new names in internal APIs and usageEdward Thomson2019-01-221-27/+27
| | | | | Move to the `git_error` name in the internal API for error-related functions.
* object_type: use new enumeration namesethomson/index_fixesEdward Thomson2018-12-011-12/+12
| | | | Use the new object_type enumeration names within the codebase.
* khash: remove intricate knowledge of khash typesPatrick Steinhardt2018-11-281-2/+2
| | | | | | | Instead of using the `khiter_t`, `git_strmap_iter` and `khint_t` types, simply use `size_t` instead. This decouples code from the khash stuff and makes it possible to move the khash includes into the implementation files.
* tree: fix integer overflow when reading unreasonably large filemodesPatrick Steinhardt2018-11-021-13/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | The `parse_mode` option uses an open-coded octal number parser. The parser is quite naive in that it simply parses until hitting a character that is not in the accepted range of '0' - '7', completely ignoring the fact that we can at most accept a 16 bit unsigned integer as filemode. If the filemode is bigger than UINT16_MAX, it will thus overflow and provide an invalid filemode for the object entry. Fix the issue by using `git__strntol32` instead and doing a bounds check. As this function already handles overflows, it neatly solves the problem. Note that previously, `parse_mode` was also skipping the character immediately after the filemode. In proper trees, this should be a simple space, but in fact the parser accepted any character and simply skipped over it. As a consequence of using `git__strntol32`, we now need to an explicit check for a trailing whitespace after having parsed the filemode. Because of the newly introduced error message, the test object::tree::parse::mode_doesnt_cause_oob_read needs adjustment to its error message check, which in fact is a good thing as it demonstrates that we now fail looking for the whitespace immediately following the filemode. Add a test that shows that we will fail to parse such invalid filemodes now.
* tree: fix mode parsing reading out-of-boundsPatrick Steinhardt2018-11-021-3/+4
| | | | | | | | | | | When parsing a tree entry's mode, we will eagerly parse until we hit a character that is not in the accepted set of octal digits '0' - '7'. If the provided buffer is not a NUL terminated one, we may thus read out-of-bounds. Fix the issue by passing the buffer length to `parse_mode` and paying attention to it. Note that this is not a vulnerability in our usual code paths, as all object data read from the ODB is NUL terminated.
* tree: unify the entry validity checksCarlos Martín Nieto2018-10-081-29/+34
| | | | | | | | | | | | | | | We have two similar functions, `git_treebuilder_insert` and `append_entry` which are used in different codepaths as part of creating a new tree. The former learnt to check for object existence under strict object creation, but the latter did not. This allowed the creation of a tree from an unowned index to bypass some of the checks and create a tree pointing to a nonexistent object. Extract a single function which performs these checks and call it from both codepaths. In `append_entry` we still do not validate when asked not to, as this is data which is already in the tree and we want to allow users to deal with repositories which already have some invalid data.
* Merge pull request #4727 from libgit2/cmn/null-oid-existing-treeEdward Thomson2018-08-261-6/+8
|\ | | | | tree: accept null ids in existing trees when updating
| * tree: rename from_tree to validate and clarify the tree in the testcmn/null-oid-existing-treeCarlos Martín Nieto2018-07-271-6/+6
| |
| * tree: accept null ids in existing trees when updatingCarlos Martín Nieto2018-07-181-6/+8
| | | | | | | | | | | | | | | | | | When we add entries to a treebuilder we validate them. But we validate even those that we're adding because they exist in the base tree. This disables using the normal mechanisms on these trees, even to fix them. Keep track of whether the entry we're appending comes from an existing tree and bypass the name and id validation if it's from existing data.
* | tree: implement function to parse raw dataPatrick Steinhardt2018-06-221-6/+19
|/ | | | | | | | | | | | | Currently, parsing objects is strictly tied to having an ODB object available. This makes it hard to parse an object when all that is available is its raw object and size. Furthermore, hacking around that limitation by directly creating an ODB structure either on stack or on heap does not really work that well due to ODB objects being reference counted and then automatically free'd when reaching a reference count of zero. Implement a function `git_tree__parse_raw` to parse a tree object from a pair of `data` and `size`.
* tree: remove unused function `git_tree__prefix_position`Patrick Steinhardt2018-06-151-35/+0
|
* tree: remove unused function `git_tree_entry_icmp`Patrick Steinhardt2018-06-151-8/+0
|
* Convert usage of `git_buf_free` to new `git_buf_dispose`Patrick Steinhardt2018-06-101-4/+4
|
* path: reject .gitmodules as a symlinkCarlos Martín Nieto2018-05-231-1/+1
| | | | | | | | Any part of the library which asks the question can pass in the mode to have it checked against `.gitmodules` being a symlink. This is particularly relevant for adding entries to the index from the worktree and for checking out files.
* Explicitly mark fallthrough cases with commentsPatrick Steinhardt2018-02-161-1/+1
| | | | | | | | | | | | | | | | | | A lot of compilers nowadays generate warnings when there are cases in a switch statement which implicitly fall through to the next case. To avoid this warning, the last line in the case that is falling through can have a comment matching a regular expression, where one possible comment body would be `/* fall through */`. An alternative to the comment would be an explicit attribute like e.g. `[[clang::fallthrough]` or `__attribute__ ((fallthrough))`. But GCC only introduced support for such an attribute recently with GCC 7. Thus, and also because the fallthrough comment is supported by most compilers, we settle for using comments instead. One shortcoming of that method is that compilers are very strict about that. Most interestingly, that comment _really_ has to be the last line. In case a closing brace follows the comment, the heuristic will fail.
* tree: reject writing null-OID entries to a treePatrick Steinhardt2018-01-261-0/+6
| | | | | | | | | | | | In commit a96d3cc3f (cache-tree: reject entries with null sha1, 2017-04-21), the git.git project has changed its stance on null OIDs in tree objects. Previously, null OIDs were accepted in tree entries to help tools repair broken history. This resulted in some problems though in that many code paths mistakenly passed null OIDs to be added to a tree, which was not properly detected. Align our own code base according to the upstream change and reject writing tree entries early when the OID is all-zero.
* tree: standard error messages are lowercaseethomson/tree_error_messagesEdward Thomson2017-12-311-10/+10
| | | | | | | | Our standard error messages begin with a lower case letter so that they can be prefixed or embedded nicely. These error messages were missed during the standardization pass since they use the `tree_error` helper function.
* Make sure to always include "common.h" firstPatrick Steinhardt2017-07-031-2/+2
| | | | | | | | | | | | | | | | | | | | | | 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.
* treebuilder: exit early if running OOM in `write_with_buffer`Patrick Steinhardt2017-03-281-3/+4
| | | | | | | | | | While writing the tree inside of a buffer, we check whether the buffer runs out of memory after each tree entry. While we set the error code as soon as we detect the OOM situation, we happily proceed iterating over the entries. This is not useful at all, as we will try to write into the buffer repeatedly, which cannot work. Fix this by exiting as soon as we are OOM.
* treebuilder: remove shadowing variable in `write_with_buffer`Patrick Steinhardt2017-03-281-1/+1
| | | | | | The `git_tree_entry *entry` variable is defined twice inside of this function. While this is not a problem currently, remove the shadowing variable to avoid future confusion.
* treebuilder: fix memory leaks in `write_with_buffer`Patrick Steinhardt2017-03-281-7/+8
| | | | | | | While we detect errors in `git_treebuilder_write_with_buffer`, we just exit directly instead of freeing allocated memory. Fix this by remembering error codes and skipping forward to the function's cleanup code.
* strmap: remove GIT__USE_STRMAP macroPatrick Steinhardt2017-02-171-2/+0
|
* khash: avoid using macro magic to get return addressPatrick Steinhardt2017-02-171-2/+2
|
* Merge pull request #3892 from mitesch/shared_bufferEdward Thomson2017-01-211-15/+30
|\ | | | | Use a shared buffer in calls of git_treebuilder_write to avoid heap contention
| * write_tree: use shared buffer for writing treesMichael Tesch2016-12-121-15/+30
| | | | | | | | | | | | | | | | | | The function to write trees allocates a new buffer for each tree. This causes problems with performance when performing a lot of actions involving writing trees, e.g. when doing many merges. Fix the issue by instead handing in a shared buffer, which is then re-used across the calls without having to re-allocate between calls.
* | giterr_set: consistent error messagesEdward Thomson2016-12-291-5/+5
| | | | | | | | | | | | | | | | 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
* | tree: look for conflicts in the new tree when updatingcmn/tree-updater-orderingCarlos Martín Nieto2016-11-141-0/+3
| | | | | | | | | | | | | | | | | | | | We look at whether we're trying to replace a blob with a tree during the update phase, but we fail to look at whether we've just inserted a blob where we're now trying to insert a tree. Update the check to look at both places. The test for this was previously succeeding due to the bu where we did not look at the sorted output.
* | tree: use the sorted update list in our loopCarlos Martín Nieto2016-11-141-2/+2
| | | | | | | | | | The loop is made with the assumption that the inputs are sorted and not using it leads to bad outputs.
* | common: cast precision specifiers to intPatrick Steinhardt2016-11-141-2/+2
| |
* | tree: validate filename and OID length when parsing objectPatrick Steinhardt2016-10-071-1/+6
|/ | | | | | | | When parsing tree entries from raw object data, we do not verify that the tree entry actually has a filename as well as a valid object ID. Fix this by asserting that the filename length is non-zero as well as asserting that there are at least `GIT_OID_RAWSZ` bytes left when parsing the OID.
* Merge pull request #3792 from edquist/miscEdward Thomson2016-05-261-1/+1
|\ | | | | Fix comment for GIT_FILEMODE_LINK
| * Fix comment for GIT_FILEMODE_LINKCarl Edquist2016-05-181-1/+1
| | | | | | | | 0120000 is symbolic link, not commit
* | tree: handle removal of all entries in the updatercmn/remove-single-entryCarlos Martín Nieto2016-05-241-0/+9
| | | | | | | | | | When we remove all entries in a tree, we should remove that tree from its parent rather than include the empty tree.
* | tree: plug leaks in the tree updatercmn/tree-update-basenameCarlos Martín Nieto2016-05-191-3/+11
| |
* | tree: use the basename for the entry removalCarlos Martín Nieto2016-05-191-1/+1
|/ | | | | | When we want to remove the file, use the basename as the name of the entry to remove, instead of the full one, which includes the directories we've inserted into the stack.
* Introduce a function to create a tree based on a different onecmn/tree-updateCarlos Martín Nieto2016-05-171-0/+245
| | | | | | | | | | | Instead of going through the usual steps of reading a tree recursively into an index, modifying it and writing it back out as a tree, introduce a function to perform simple updates more efficiently. `git_tree_create_updated` avoids reading trees which are not modified and supports upsert and delete operations. It is not as versatile as modifying the index, but it makes some common operations much more efficient.
* Plug a few leaksCarlos Martín Nieto2016-03-311-0/+2
|