Commit Graph

6521 Commits

Author SHA1 Message Date
Patrick Steinhardt
88520151fd openssl_stream: use new initialization function on OpenSSL version >=1.1
Previous to OpenSSL version 1.1, the user had to initialize at least the error
strings as well as the SSL algorithms by himself. OpenSSL version 1.1 instead
provides a new function `OPENSSL_init_ssl`, which handles initialization of all
subsystems. As the new API call will by default load error strings and
initialize the SSL algorithms, we can safely replace these calls when compiling
against version 1.1 or later.

This fixes a compiler error when compiling against OpenSSL version 1.1 which has
been built without stubs for deprecated syntax.
2017-04-10 11:29:07 +02:00
Patrick Steinhardt
29081c2fd4 openssl_stream: remove locking initialization on OpenSSL version >=1.1
Up to version 1.0, OpenSSL required us to provide a callback which implements
a locking mechanism. Due to problems in the API design though this mechanism was
inherently broken, especially regarding that the locking callback cannot report
errors in an obvious way. Due to this shortcoming, the locking initialization
has been completely removed in OpenSSL version 1.1. As the library has also been
refactored to not make any use of these callback functions, we can safely remove
all initialization of the locking subsystem if compiling against OpenSSL version
1.1 or higher.

This fixes a compilation error when compiling against OpenSSL version 1.1 which
has been built without stubs for deprecated syntax.
2017-04-10 11:29:07 +02:00
Patrick Steinhardt
cf07db2f1b filter: only close filter if it's been initialized correctly
In the function `git_filter_list_stream_data`, we initialize, write and
subesquently close the stream which should receive content processed by
the filter. While we skip writing to the stream if its initialization
failed, we still try to close it unconditionally -- even if the
initialization failed, where the stream might not be set at all, leading
us to segfault.

Semantics in this code is not really clear. The function handling the
same logic for files instead of data seems to do the right thing here in
only closing the stream when initialization succeeded. When stepping
back a bit, this is only reasonable: if a stream cannot be initialized,
the caller would not expect it to be closed again. So actually, both
callers of `stream_list_init` fail to do so. The data streaming function
will always close the stream and the file streaming function will not
close the stream if writing to it has failed.

The fix is thus two-fold:

- callers of `stream_list_init` now close the stream iff it has been
  initialized
- `stream_list_init` now closes the lastly initialized stream if
  the current stream in the chain failed to initialize

Add a test which segfaulted previous to these changes.
2017-04-07 16:34:47 +02:00
Edward Thomson
e572b63121 Merge pull request #4183 from pks-t/pks/coverity
Coverity
2017-04-07 09:03:56 +01:00
Patrick Steinhardt
2a485dabc0 refs: update worktree HEADs when renaming branches
Whenever we rename a branch, we update the repository's symbolic HEAD
reference if it currently points to the branch that is to be renamed.
But with the introduction of worktrees, we also have to iterate over all
HEADs of linked worktrees to adjust them. Do so.
2017-04-05 13:50:38 +02:00
Patrick Steinhardt
38fc5ab0a2 branch: use foreach_head to see if a branch is checked out
Previously, we have extracted the logic to find and iterate over all
HEADs of a repository. Use this function in `git_branch_is_checked_out`.
2017-04-05 13:50:38 +02:00
Patrick Steinhardt
74511aa204 repository: add function to iterate over all HEADs
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.
2017-04-05 13:50:38 +02:00
Patrick Steinhardt
3e84aa506d repository: get worktree HEAD via git_reference__read_head
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.
2017-04-05 13:50:38 +02:00
Patrick Steinhardt
987f565917 repository: extract function to get path to a file in a work tree
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.
2017-04-05 13:49:31 +02:00
Patrick Steinhardt
8242cc1a23 repository: set error message if trying to set HEAD to a checked out one
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.
2017-04-05 13:49:31 +02:00
Patrick Steinhardt
5b65ac2578 refs: implement function to read references from file
Currently, we only provide functions to read references directly from a
repository's reference store via e.g. `git_reference_lookup`. But in
some cases, we may want to read files not connected to the current
repository, e.g. when looking up HEAD of connected work trees. This
commit implements `git_reference__read_head`, which will read out and
allocate a reference at an arbitrary path.
2017-04-05 13:49:31 +02:00
Edward Thomson
89d403cce2 win32: enable p_utimes for readonly files
Instead of failing to set the timestamp of a read-only file (like any
object file), set it writable temporarily to update the timestamp.
2017-04-05 11:04:29 +01:00
Patrick Steinhardt
9daba9f4b6 fileops: do not overwrite correct error message on mmap
When executing `git_futils_mmap_ro_file`, we first try to guess whether
the file is mmapable at all. Part of this check is whether the file is
too large to be mmaped, which can be true on systems with 32 bit
`size_t` types.

The check is performed by first getting the file size wtih
`git_futils_filesize` and then checking whether the returned size can be
represented as `size_t`, returning an error if so. While this test also
catches the case where the function returned an error (as `-1` is not
representable by `size_t`), we will set the misleading error message
"file too large to mmap". But in fact, a negative return value from
`git_futils_filesize` will be caused by the inability to fstat the file.

Fix the error message by handling negative return values separately and
not overwriting the error message in that case.
2017-04-04 11:58:46 +02:00
Patrick Steinhardt
756138e475 blame_git: check return value of git__calloc
We do not check the return value of `git__calloc`, which may return
`NULL` in out-of-memory situations. Fix the error by using
`GITERR_CHECK_ALLOC`.
2017-04-04 11:58:46 +02:00
Patrick Steinhardt
a76d75021c path: short-circuit git_path_apply_relative on error
Short-circuit the call to `git_path_resolve_relative` in case
`git_buf_joinpath` returns an error. While this does not fix any
immediate errors, the resulting code is easier to read and handles
potential new error conditions raised by `git_buf_joinpath`.
2017-04-04 11:58:46 +02:00
Patrick Steinhardt
cffd616a72 path: handle error returned by git_buf_joinpath
In the `_check_dir_contents` function, we first allocate memory for
joining the directory and subdirectory together and afterwards use
`git_buf_joinpath`. While this function in fact should not fail as
memory is already allocated, err on the safe side and check for returned
errors.
2017-04-04 11:58:46 +02:00
Patrick Steinhardt
4467aeac42 config_file: handle errors other than OOM while parsing section headers
The current code in `parse_section_header_ext` is only prepared to
properly handle out-of-memory conditions for the `git_buf` structure.
While very unlikely and probably caused by a programming error, it is
also possible to run into error conditions other than out-of-memory
previous to reaching the actual parsing loop. In these cases, we will
run into undefined behavior as the `rpos` variable is only initialized
after these triggerable errors, but we use it in the cleanup-routine.

Fix the issue by unifying the function's cleanup code with an
`end_error` section, which will not use the `rpos` variable.
2017-04-04 11:58:46 +02:00
Edward Thomson
7ece906598 win32: make posix emulation retries configurable
POSIX emulation retries should be configurable so that tests can disable
them.  In particular, maniacally threading tests may end up trying to
open locked files and need retries, which will slow continuous
integration tests significantly.
2017-04-03 23:14:24 +01:00
Edward Thomson
1069ad3c54 win32: do not inherit file descriptors 2017-04-03 23:13:38 +01:00
Sven Strickroth
d5e6ca1e4a Allow to configure default file share mode for opening files
This can prevent FILE_SHARED_VIOLATIONS when used in tools such as TortoiseGit TGitCache and FILE_SHARE_DELETE, because files can be opened w/o being locked any more.

Signed-off-by: Sven Strickroth <email@cs-ware.de>
2017-04-03 23:13:00 +01:00
Edward Thomson
e86d02f92d git_repository_set_head: use remote name in reflog
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`.
2017-04-03 00:10:47 +01:00
Sven Strickroth
92d5a6377a win32: deduplicate code: use p_open in p_creat
Signed-off-by: Sven Strickroth <email@cs-ware.de>
2017-04-02 19:42:41 +01:00
Sven Strickroth
ef5cfcdb4f win32: use CreateFile in p_open
Signed-off-by: Sven Strickroth <email@cs-ware.de>
2017-04-02 19:41:26 +01:00
Edward Thomson
fbc6910f6b win32: teach p_open about do_with_retries 2017-04-02 19:38:20 +01:00
Edward Thomson
a0f67e4a26 win32: teach p_unlink about do_with_retries 2017-04-02 19:38:19 +01:00
Edward Thomson
8a4e151301 win32: make p_rename use do_with_retries 2017-04-02 19:38:19 +01:00
Edward Thomson
cc8d9a29e7 win32: introduce do_with_retries macro
Provide a macro that will allow us to run a function with posix-like
return values multiple times in a retry loop, with an optional cleanup
function called between invocations.
2017-04-01 10:47:30 +01:00
Edward Thomson
dcaa90991f win32: map windows error codes to errno
Introduce mapping from windows error codes to errno values.  This
allows us to replace our calls to the Windows posix emulation functions
with calls to the Win32 APIs for more fine-grained control over the
emulation.

These mappings match the Windows CRT's mappings for its posix emulation
as they were described to me.
2017-04-01 10:47:29 +01:00
Edward Thomson
fcb322f5a0 Merge remote-tracking branch 'origin/pr/3790' into win32_posix 2017-03-31 23:39:33 +01:00
Patrick Steinhardt
06abbb7f07 treebuilder: exit early if running OOM in write_with_buffer
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.
2017-03-28 08:39:29 +02:00
Patrick Steinhardt
8d1e71f5a2 treebuilder: remove shadowing variable in write_with_buffer
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.
2017-03-28 08:39:29 +02:00
Patrick Steinhardt
4f9327faf9 treebuilder: fix memory leaks in write_with_buffer
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.
2017-03-28 08:39:29 +02:00
Patrick Steinhardt
fbdf2a79d5 worktree: unconditionally free the worktree's name 2017-03-24 09:26:31 +01:00
Patrick Steinhardt
4004d68f99 blame_git: remove spuriuous goto
The recent addition of an error code to `pass_whole_blame` in ff8d2eb15
(blame_git: check return value of object lookup, 2017-03-20) introduced
a spurious goto. Remove it.
2017-03-24 08:41:10 +01:00
Patrick Steinhardt
1d39a60319 Merge pull request #4175 from libgit2/ethomson/dont_trunc_and_excl
git_futils: don't O_EXCL and O_TRUNC
2017-03-24 08:26:33 +01:00
Carlos Martín Nieto
fa86a095cb Merge pull request #4178 from libgit2/ethomson/enfasten_sha1
sha1dc: perf improvements from upstream
2017-03-23 20:45:28 +01:00
Edward Thomson
d672963535 sha1dc: SHA1DCUpdate now takes a size_t 2017-03-23 17:25:11 +00:00
Edward Thomson
e65b5e960d config: expand paths with git_sysdir_expand... 2017-03-23 12:12:39 +00:00
Edward Thomson
29aef94830 config, attrcache: don't fallback to dirs literally named ~
The config and attrcache file reading code would attempt to load a file
in a home directory by expanding the `~` and looking for the file, using
`git_sysdir_find_global_file`.  If the file was not found, the error
handling would look for the literal path, eg `~/filename.txt`.

Use the new `git_config_expand_global_file` instead, which allows us to
get the path to the file separately, when the path is prefixed with
`~/`, and fail with a not found error without falling back to looking
for the literal path.
2017-03-23 12:12:39 +00:00
Edward Thomson
5135ddaac6 Introduce git_sysdir_expand_global_file
Provide a mechanism for callers to expand the full path of a file in the
global configuration directory (that is to say, the home directory) even
if the file doesn't necessarily exist.  This lets callers use their own
logic for building paths separate from handling file existence.
2017-03-23 12:12:39 +00:00
Sim Domingo
301dc26a5a fix error when including a missing config file relative to the home directory 2017-03-23 12:12:38 +00:00
Edward Thomson
6987368565 Merge branch 'pr/3957' 2017-03-23 09:49:09 +00:00
Edward Thomson
b53d834fca merge: indentation fixup 2017-03-23 09:46:22 +00:00
Edward Thomson
c9efa995e4 sha1dc: perf improvements from upstream
Update SHA-1 collision detection code (cr-marcstevens/sha1collisiondetection)
to master to include performance improvements.
2017-03-23 09:16:24 +00:00
Edward Thomson
f623cf894a Merge pull request #4163 from pks-t/pks/submodules-with-worktrees
Worktree fixes
2017-03-22 20:32:55 +00:00
Edward Thomson
6fd6c67824 Merge pull request #4030 from libgit2/ethomson/fsync
fsync all the things
2017-03-22 20:29:22 +00:00
Edward Thomson
4a26915d34 git_futils: don't O_EXCL and O_TRUNC
`O_EXCL` and `O_TRUNC` are mutually exclusive flags to open(2); you can't
truncate a file if you're asserting that it can't exist in the first place.
Drop `O_TRUNC`.
2017-03-22 19:48:50 +00:00
Edward Thomson
ea3bb5c0bb git_repository_set_head: use tag name in reflog
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`.
2017-03-21 18:12:02 +00:00
Patrick Steinhardt
e7330016af diff_parse: check return value of git_diff_init_options 2017-03-21 15:48:16 +01:00
Patrick Steinhardt
723bdf4864 patch_parse: check if advancing over header newline succeeds
While parsing patch header lines, we iterate over each line and check if
the line has trailing garbage. What we do not check though is that the
line is actually a line ending with a trailing newline.

Fix this by checking the return code of `parse_advance_expected_str`.
2017-03-21 15:48:16 +01:00
Patrick Steinhardt
8d452448bb odb_pack: initialize git_rawobj structure
The `pack_entry_find_prefix` function receives a `git_rawobj` structure
as argument. While the function first initializes the structure to a
sensible state, Coverity is unable to correctly detect this, resulting
in a warning.

Fix this warning by initializing the object to all-zeroes before passing
it to the function.
2017-03-21 15:48:15 +01:00
Patrick Steinhardt
2cf48e1326 config_file: check if section header buffer runs out of memory
While parsing section headers, we use a buffer to store the actual
section name. We do not check though if the buffer runs out of memory at
any stage. Do so.
2017-03-21 15:48:15 +01:00
Patrick Steinhardt
ff8d2eb15f blame_git: check return value of object lookup
The function `pass_whole_blame` performs an object lookup but does not
check if the lookup actually succeeds. Convert the function to return an
error code and check for it in the calling function.
2017-03-21 15:48:15 +01:00
Patrick Steinhardt
dd0b1e8cb6 openssl_stream: fix releasing OpenSSL locks
The OpenSSL library may require multiple locks to work correctly, where
it is the caller's responsibility to initialize and release the locks.
While we correctly initialized up to `n` locks, as determined by
`CRYPTO_num_locks`, we were repeatedly freeing the same mutex in our
shutdown procedure.

Fix the issue by freeing locks at the correct index.
2017-03-20 12:36:14 +01:00
Edward Thomson
1d50f95546 Merge pull request #4166 from pks-t/pks/map-free-fix
Remove `map_free` macros
2017-03-20 11:29:27 +00:00
Patrick Steinhardt
31059923bd Merge pull request #4169 from csware/absolute-symlink 2017-03-20 12:16:18 +01:00
Sven Strickroth2
86a8cd9f6a filebuf: fix resolving absolute symlinks
The symlink destination is always concatenated to the original path. Fix
this by using `git_buf_sets` instead of `git_buf_puts`.
2017-03-20 12:14:05 +01:00
Patrick Steinhardt
e30a6ee378 Merge pull request #4160 from pks-t/pks/diff-fixes
Diff fixes
2017-03-20 11:06:23 +01:00
Patrick Steinhardt
44b3b9feca Merge pull request #4162 from pks-t/pks/merge-source-fixes
merge_driver: fix const-correctness for source getters
2017-03-20 11:05:03 +01:00
Patrick Steinhardt
94af9155cf map: remove *map_free macros
The `map_free` functions were not implemented as functions but instead
as macros which also set the map to NULL. While this is most certainly
sensible in most cases, we should prefer the more obvious behavior,
namingly leaving the map pointer intact.

Furthermore, this macro has been refactored incorrectly during the
map-refactorings: the two statements are not actually grouped together
by a `do { ... } while (0)` block, as it is required for macros to
match the behavior of functions more closely. This has led to at least
one subtle nesting error in `pack-objects.c`. The following code block

```
    if (pb->object_ix)
        git_oidmap_free(pb->object_ix);
```

would be expanded to

```
    if (pb->object_ix)
        git_oidmap__free(pb->object_ix); pb->object_ix = NULL;
```

which is not what one woudl expect. While it is not a bug here as it
would simply become a no-op, the wrong implementation could lead to bugs
in other occasions.

Fix this by simply removing the macro altogether and replacing it with
real function calls. This leaves the burden of setting the pointer to
NULL afterwards to the caller, but this is actually expected and behaves
like other `free` functions.
2017-03-20 09:01:18 +01:00
Patrick Steinhardt
77c8ee74ff checkout: fix double-free of checkout_data's mkdir_map
We currently call `git_strmap_free` on `checkout_data.mkdir_map` in the
`checkout_data_clear` function. The only thing protecting us from a
double-free is that the `git_strmap_free` function is in fact not a
function, but a macro that also sets the map to NULL.

Remove the second call to `git_strmap_free` and explicitly set the map
member to NULL.
2017-03-20 08:59:30 +01:00
Patrick Steinhardt
b0c9bc920f submodule: resolve URLs relative to main worktree
It is possible to specify submodule URLs relative to the repository
location. E.g. having a submodule with URL "../submodule" will look for
the submodule at "repo/../submodule".

With the introduction of worktrees, though, we cannot simply resolve the
URL relative to the repository location itself. If the repository for
which a URL is to be resolved is a working tree, we have to resolve the
URL relative to the parent's repository path. Otherwise, the URL would
change depending on where the working tree is located.

Fix this by special-casing when we have a working tree while getting the
URL base.
2017-03-17 09:27:56 +01:00
Patrick Steinhardt
097f0105b4 refdb: create references in commondir
References for a repository are usually created inside of its gitdir.
When using worktrees, though, these references are not to be created
inside the worktree gitdir, but instead inside the gitdir of its parent
repository, which is the commondir. Like this, branches will still be
available after the worktree itself has been deleted.

The filesystem refdb currently still creates new references inside of
the gitdir. Fix this and have it create references in commondir.
2017-03-17 09:27:56 +01:00
Patrick Steinhardt
8f154be3eb worktree: write resolved paths into link files
The three link files "worktree/.git", ".git/worktrees/<name>/commondir"
and ".git/worktrees/<name>/gitdir" should always contain absolute and
resolved paths. Adjust the logic creating new worktrees to first use
`git_path_prettify_dir` before writing out these files, so that paths
are resolved first.
2017-03-17 09:27:56 +01:00
Patrick Steinhardt
7cf7a40749 worktree: rename variable in git_worktree_add 2017-03-17 09:27:56 +01:00
Patrick Steinhardt
9dcc79bc6a worktree: use fully qualified reference name for created HEAD
When creating a new worktree, we have to set up the initial data
structures. Next to others, this also includes the HEAD pseudo-ref.
We currently set it to the worktree respectively branch name, which is
actually not fully qualified.

Use the fully qualified branch name instead.
2017-03-17 09:27:55 +01:00
Patrick Steinhardt
20a368e2d7 worktree: parent path should point to the working dir
The working tree's parent path should not point to the parent's gitdir,
but to the parent's working directory. Pointing to the gitdir would not
make any sense, as the parent's working directory is actually equal to
both repository's common directory.

Fix the issue.
2017-03-17 09:27:55 +01:00
Patrick Steinhardt
3017ba94a3 worktree: implement git_worktree_open_from_repository
While we already provide functionality to look up a worktree from a
repository, we cannot do so the other way round. That is given a
repository, we want to look up its worktree if it actually exists.
Getting the worktree of a repository is useful when we want to get
certain meta information like the parent's location, getting the locked
status, etc.
2017-03-17 09:26:13 +01:00
Patrick Steinhardt
dfc9870647 worktree: split off function opening working directory
Separate the logic of finding the worktree directory of a repository and
actually opening the working tree's directory. This is a preparatory
step for opening the worktree structure of a repository itself.
2017-03-17 08:56:18 +01:00
Patrick Steinhardt
3e9c5d8a05 worktree: have is_worktree_dir accept a string instead of buffer
This will be used in later commits, where it becomes cumbersome to
always pass in a buffer.
2017-03-17 08:56:18 +01:00
Edward Thomson
6dfb983cfc Merge pull request #4161 from pks-t/pks/submodule-index-fix
submodule: catch when submodule is not staged on update
2017-03-15 15:03:45 +00:00
Patrick Steinhardt
bb342159d8 merge_driver: fix const-correctness for source getters 2017-03-15 11:46:15 +01:00
Patrick Steinhardt
8051b47f5b Merge branch 'gumenikav/merge_driver_source_info' 2017-03-15 08:20:26 +01:00
Gumenik Alexandr
0608d5df59 merge_driver: add unresolved getters for git_merge_driver_source 2017-03-15 08:19:48 +01:00
Patrick Steinhardt
13bf4d7f13 Merge pull request #4146 from pks-t/pks/winhttp-errors
winhttp: disambiguate error messages when sending requests
2017-03-14 15:04:18 +01:00
Patrick Steinhardt
32ecc98e51 submodule: catch when submodule is not staged on update
When calling `git_submodule_update` on a submodule, we have to retrieve
the ID of the submodule entry in the index. If the function is called on
a submodule which is only partly initialized, the submodule entry may
not be added to the index yet. This leads to an assert when trying to
look up the blob later on.

Fix the issue by checking if the index actually holds the submodule's
ID and erroring out if it does not.
2017-03-14 14:53:32 +01:00
Patrick Steinhardt
c0eba379d1 diff_parse: correctly set options for parsed diffs
The function `diff_parsed_alloc` allocates and initializes a
`git_diff_parsed` structure. This structure also contains diff options.
While we initialize its flags, we fail to do a real initialization of
its values. This bites us when we want to actually use the generated
diff as we do not se the option's version field, which is required to
operate correctly.

Fix the issue by executing `git_diff_init_options` on the embedded
struct.
2017-03-14 13:09:35 +01:00
Patrick Steinhardt
ad5a909cfb patch_parse: fix parsing minimal trailing diff line
In a diff, the shortest possible hunk with a modification (that is, no
deletion) results from a file with only one line with a single character
which is removed. Thus the following hunk

    @@ -1 +1 @@
    -a
    +

is the shortest valid hunk modifying a line. The function parsing the
hunk body though assumes that there must always be at least 4 bytes
present to make up a valid hunk, which is obviously wrong in this case.
The absolute minimum number of bytes required for a modification is
actually 2 bytes, that is the "+" and the following newline. Note: if
there is no trailing newline, the assumption will not be offended as the
diff will have a line "\ No trailing newline" at its end.

This patch fixes the issue by lowering the amount of bytes required.
2017-03-14 13:09:13 +01:00
Patrick Steinhardt
62a2fc06d4 patch_generate: move git_diff_foreach to diff.c
Now that the `git_diff_foreach` function does not depend on internals of
the `git_patch_generated` structure anymore, we can easily move it to
the actual diff code.
2017-03-14 13:08:51 +01:00
Patrick Steinhardt
ace3508f4c patch_generate: fix git_diff_foreach only working with generated diffs
The current logic of `git_diff_foreach` makes the assumption that all
diffs passed in are actually derived from generated diffs. With these
assumptions we try to derive the actual diff by inspecting either the
working directory files or blobs of a repository. This obviously cannot
work for diffs parsed from a file, where we do not necessarily have a
repository at hand.

Since the introduced split of parsed and generated patches, there are
multiple functions which help us to handle patches generically, being
indifferent from where they stem from. Use these functions and remove
the old logic specific to generated patches. This allows re-using the
same code for invoking the callbacks on the deltas.
2017-03-14 13:08:28 +01:00
Patrick Steinhardt
41019152a0 patch_generate: remove duplicated logic
Under the existing logic, we try to load patch contents differently,
depending on whether the patch files stem from the working directory or
not. But actually, the executed code paths are completely equal to each
other -- so we were always the code despite the condition.

Remove the condition altogether and conflate both code paths.
2017-03-14 12:04:04 +01:00
Carlos Martín Nieto
53454be87f Merge pull request #4157 from adamniedzielski/4099-git-sort-time-uninteresting
Skip uninteresting commits in revwalk timesort iterator
2017-03-11 19:52:05 +01:00
Carlos Martín Nieto
0b3623ac47 Merge pull request #4156 from libgit2/ethomson/readbuffer_dont_hash
git_futils_readbuffer: don't compute sha-1
2017-03-11 19:09:27 +01:00
Edward Thomson
ffd4df6bf2 Merge pull request #4151 from novalis/dturner/rebase-submodule-untracked
rebase: ignore untracked files in submodules
2017-03-09 16:32:38 +00:00
Adam Niedzielski
c11c08a552 Skip uninteresting commits in revwalk timesort iterator
Fixes #4099
2017-03-09 14:01:10 +01:00
Edward Thomson
11c8e756ac git_futils_readbuffer: don't compute sha-1
Don't compute the sha-1 in `git_futils_readbuffer_updated` unless the
checksum was requested.  This means that `git_futils_readbuffer` will
not calculate the checksum unnecessarily.
2017-03-09 12:26:23 +00:00
Edward Thomson
835805625a refdb_fs: honor the namespace 2017-03-06 14:20:12 +00:00
David Turner
2270ca9f8f rebase: ignore untracked files in submodules
An untracked file in a submodule should not prevent a rebase from
starting.  Even if the submodule's SHA is changed, and that file would
conflict with a new tracked file, it's still OK to start the rebase
and discover the conflict later.

Signed-off-by: David Turner <dturner@twosigma.com>
2017-03-03 15:07:54 -05:00
Edward Thomson
52d03f37f7 git_commit_create: freshen tree objects in commit
Freshen the tree object that a commit points to during commit time.
2017-03-03 14:12:00 +00:00
Edward Thomson
2dfd1294f7 hash: include sha1collisiondetection
Include the SHA1 collision attack detection library from
https://github.com/cr-marcstevens/sha1collisiondetection
2017-03-03 10:50:27 +00:00
Edward Thomson
a371a8ae94 hash: rename implementation selection constants 2017-03-03 10:50:27 +00:00
Edward Thomson
1c04a96b25 Honor core.fsyncObjectFiles 2017-03-02 09:11:33 +00:00
Patrick Steinhardt
b65a5e9ba9 winhttp: disambiguate error messages when sending requests 2017-03-01 07:58:40 +01:00
Edward Thomson
3ac05d1149 win32: don't fsync parent directories on Windows
Windows doesn't support it.
2017-02-28 13:29:01 +00:00
Edward Thomson
2a5ad7d0f2 fsync: call it "synchronous" object writing
Rename `GIT_OPT_ENABLE_SYNCHRONIZED_OBJECT_CREATION` ->
`GIT_OPT_ENABLE_SYNCHRONOUS_OBJECT_CREATION`.
2017-02-28 13:29:01 +00:00
Edward Thomson
1229e1c4d7 fsync parent directories when fsyncing
When fsync'ing files, fsync the parent directory in the case where we
rename a file into place, or create a new file, to ensure that the
directory entry is flushed correctly.
2017-02-28 13:28:36 +00:00
Edward Thomson
5a747e0c6c git_futils_writebuffer: default only when flags==0
Only use defaults for `git_futils_writebuffer` when flags == 0, lest
(1 << 31) be treated as the defaults.
2017-02-28 13:28:36 +00:00
Edward Thomson
eb56ed81af refdb_fs: optionally fsync packed refs 2017-02-28 13:28:36 +00:00
Edward Thomson
af3dcb0e50 refdb_fs: optionally fsync loose references 2017-02-28 13:28:36 +00:00
Edward Thomson
5312621bd4 git_futils_writebuffer: optionally fsync
Add a custom `O_FSYNC` bit (if it's not been defined by the operating
system`) so that `git_futils_writebuffer` can optionally do an `fsync`
when it's done writing.

We call `fsync` ourselves, even on systems that define `O_FSYNC` because
its definition is no guarantee of its actual support.  Mac, for
instance, defines it but doesn't support it in an `open(2)` call.
2017-02-28 13:27:50 +00:00
Edward Thomson
1c2c0ae2a4 packbuilder: honor git_object__synchronized_writing
Honor `git_object__synchronized_writing` when creating a packfile and
corresponding index.
2017-02-28 13:27:50 +00:00
Edward Thomson
e6ed0d2f03 odb_loose: fsync tests
Introduce a simple counter that `p_fsync` implements.  This is useful
for ensuring that `p_fsync` is called when we expect it to be, for
example when we have enabled an odb backend to perform `fsync`s when
writing objects.
2017-02-28 13:27:49 +00:00
Edward Thomson
6d3ad7e09e Add ENABLE_SYNCHRONIZED_OBJECT_CREATION option
Allow users to enable `SYNCHRONIZED_OBJECT_CREATION` with a setting.
2017-02-28 13:27:49 +00:00
Edward Thomson
fc27fe213c odb_loose: actually honor the fsync option
We've had an fsync option for a long time, but it was "ignored".
Stop ignoring it.
2017-02-28 13:27:49 +00:00
Edward Thomson
fc77891f63 git_filebuf: optionally fsync when committing 2017-02-28 13:27:49 +00:00
Richard Ipsum
1255a9ac0c Fix: make reflog include "(merge)" for merge commits
This fixes issue #4094
2017-02-27 23:53:12 +00:00
Patrick Steinhardt
7f875fb4f6 Merge pull request #4133 from stinb/khash-cleanup-regression
fix regression from #4092
2017-02-24 08:51:50 +01:00
Jason Haslam
685f22519c pack: fix looping over cache entries
Fixes a regression from #4092. This is a crash on 32-bit and I assume that
it doesn't do the right thing on 64-bit either. MSVC emits a warning for this,
but of course, it's easy to get lost among all of the similar 'possible loss
of data' warnings.
2017-02-22 09:29:00 -07:00
Carlos Martín Nieto
c03f00ea9c Merge pull request #4131 from pks-t/pks/attrcache-cleanups
Attrcache cleanups
2017-02-22 16:30:57 +01:00
Edward Thomson
aa5ea47f73 Provide error on gcc < 4.1 when THREADSAFE
Provide a descriptive error message when compiling THREADSAFE on gcc
versions < 4.1.  We require the atomic primitives (eg
`__sync_synchronize`) that were introduced in that version.

(Note, clang setes `__GNUC__` but appears to set its version > 4.1.)
2017-02-22 11:21:56 +00:00
Patrick Steinhardt
ce6f61daf0 attrcache: remove useless do_init indirection
Remove useless indirection from `git_attr_cache__init` to
`git_attr_cache__do_init`. The difference is that the
`git_attr_cache__init` macro first checks if the cache is already
initialized and, if so, not call `git_attr_cache__do_init`. But
actually, `git_attr_cache__do_init` already does the same thing and
returns immediately if the cache is already initialized.

Remove the indirection.
2017-02-21 16:17:36 +01:00
Patrick Steinhardt
c11510103d attrcache: replace existing file entry with git__swap
When doing an upsert of a file, we used to use `git__compare_and_swap`,
comparing the entry's file which is to be replaced with itself. This can
be more easily formulated by using `git__swap`, which unconditionally
replaces the value.
2017-02-21 16:17:36 +01:00
Patrick Steinhardt
b8ab782a6d attrcache: do not lock/unlock the mutex directly
Improve encapsulation by not referencing the attrcache mutex directly
but instead using the `attr_cache_lock` and `attr_cache_unlock`
functions.
2017-02-21 16:17:31 +01:00
Edward Thomson
b13f0da153 Merge pull request #4130 from libgit2/ethomson/clar_messages
Improve clar messages
2017-02-17 16:32:14 +00:00
Edward Thomson
cc17264270 p_snprintf: no need for arguments to a format
`snprintf` requires a _format_ but does not require _arguments_ to the
format.  eg: `snprintf(buf, 42, "hi")` is perfectly legal.  Expand the
macro to match.

Without this, `p_sprintf(buf, 42, "hi")` errors with:

```
error: expected expression
                p_snprintf(msg, 42, "hi");
                ^
src/unix/posix.h:53:34: note: expanded from macro 'p_snprintf'
                                 ^
/usr/include/secure/_stdio.h:57:73: note: expanded from macro 'snprintf'
  __builtin___snprintf_chk (str, len, 0, __darwin_obsz(str),
__VA_ARGS__)
```
2017-02-17 12:10:19 +00:00
Patrick Steinhardt
8f1ff26bd3 idxmap: remove GIT__USE_IDXMAP 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
cee9ca6609 idxmap: convert to use functions instead of macros 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
8f5fe903d6 offmap: remove GIT__USE_OFFMAP macro 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
cf6124d68b offmap: convert to use functions instead of macros 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
0d71690549 oidmap: remove GIT__USE_OIDMAP macro 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
659f5d07d5 oidmap: convert macros to functions 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
13c3bc9adf strmap: remove GIT__USE_STRMAP macro 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
a13cfd2af2 strmap: convert macros to functions 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
f14f75d4a1 khash: avoid using kh_resize directly 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
73028af85c khash: avoid using macro magic to get return address 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
85d2748c6f khash: avoid using kh_key/kh_val as lvalue 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
f31cb45ad2 khash: avoid using kh_put directly 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
a8cd560b10 khash: avoid using kh_del directly 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
71a54317ef khash: avoid using kh_key directly 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
cb18386f72 khash: avoid using kh_val/kh_value directly 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
76e671a653 khash: avoid using kh_exist directly 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
c37b069b9f khash: avoid using kh_clear directly 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
a853c52723 khash: avoid using kh_get directly 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
64e46dc3b5 khash: avoid using kh_end directly 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
036daa59e9 khash: use git_map_exists where applicable 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
9694d9ba79 khash: avoid using kh_foreach/kh_foreach_value directly 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
63e914cbf4 khash: avoid using kh_size directly 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
a1b23df58c offmap: remove unused macro git_offmap_insert2 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
bff9b7a5a3 strmap: remove unused macro git_strmap_insert2 2017-02-17 11:41:06 +01:00
Patrick Steinhardt
6da6b425d8 refdb: catch additional per-worktree refs
The upstream git.git project currently identifies all references inside
of `refs/bisect/` as well as `HEAD` as per-worktree references. This is
already incorrect and is currently being fixed by an in-flight topic
[1]. The new behavior will be to match all pseudo-references outside of
the `refs/` hierarchy as well as `refs/bisect/`.

Our current behavior is to mark a selection of pseudo-references as
per-worktree, only. This matches more pseudo-references than current
git, but forgets about `refs/bisect/`. Adjust behavior to match the
in-flight topic, that is classify the following references as
per-worktree:

- everything outside of `refs/`
- everything inside of `refs/bisect/`

[1]: <20170213152011.12050-1-pclouds@gmail.com>
2017-02-15 09:06:56 +01:00
Edward Thomson
a59545de5e Merge pull request #4122 from pks-t/pks/signature-dbl-free
Signature cleanups
2017-02-13 14:38:12 +00:00
Patrick Steinhardt
ade0d9c658 commit: avoid possible use-after-free
When extracting a commit's signature, we first free the object and only
afterwards put its signature contents into the result buffer. This works
in most cases - the free'd object will normally be cached anyway, so we
only end up decrementing its reference count without actually freeing
its contents. But in some more exotic setups, where caching is disabled,
this can definitly be a problem, as we might be the only instance
currently holding a reference to this object.

Fix this issue by first extracting the contents and freeing the object
afterwards only.
2017-02-13 13:50:52 +01:00
Patrick Steinhardt
dc851d9eae commit: clear user-provided buffers
The functions `git_commit_header_field` and
`git_commit_extract_signature` both receive buffers used to hand back
the results to the user. While these functions called `git_buf_sanitize`
on these buffers, this is not the right thing to do, as it will simply
initialize or zero-terminate passed buffers. As we want to overwrite
contents, we instead have to call `git_buf_clear` to completely reset
them.
2017-02-13 13:50:52 +01:00
Edward Thomson
c576d4fff2 Merge pull request #4115 from gsaralms/users/gsaral/optionalOfsDelta
Changes to provide option to turn off/on ofs_delta
2017-02-13 12:46:00 +00:00
Patrick Steinhardt
cdb2c2a0bf buffer: clarify how git_buf_sanitize handles non-NULL input
When `git_buf_sanitize` gets called, it converts a buffer with NULL
content to be correctly initialized. This is done by pointing it to
`git_buf__initbuf`. While the method's documentation states this
clearly, it may also lead to the conclusion that it will do the same to
buffers which do _not_ have NULL contents.

Clarify behavior when passing a buffer with non-NULL contents, where
`git_buf_sanitize` will ensure that the contents are `\0`-terminated.
2017-02-13 13:31:37 +01:00
Patrick Steinhardt
1ba242c9ab worktree: extract git_worktree_is_prunable 2017-02-13 11:13:08 +01:00
Patrick Steinhardt
39abd3adaa worktree: compute workdir for worktrees opened via their gitdir
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.
2017-02-13 11:08:57 +01:00
Patrick Steinhardt
84f56cb05a repository: rename path_repository and path_gitlink
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.
2017-02-13 11:07:52 +01:00
Patrick Steinhardt
384518d09d repository: restrict checking out checked out branches
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.
2017-02-13 11:06:52 +01:00
Patrick Steinhardt
143e539fd0 branch: restrict branch deletion for worktrees
Restrict the ability to delete branches that are checked out in
any linked repository.
2017-02-13 11:05:57 +01:00