Commit Graph

330 Commits

Author SHA1 Message Date
Patrick Steinhardt
13deb8745d index: fix NULL pointer access in index_remove_entry
When removing an entry from the index by its position, we first
retrieve the position from the index's entries and then try to
remove the retrieved value from the index map with
`DELETE_IN_MAP`. When `index_remove_entry` returns `NULL` we try
to feed it into the `DELETE_IN_MAP` macro, which will
unconditionally call `idxentry_hash` and then happily dereference
the `NULL` entry pointer.

Fix the issue by not passing a `NULL` entry into `DELETE_IN_MAP`.
2016-06-07 09:14:14 +02:00
Edward Thomson
46082c384f index_read_index: invalidate new paths in tree cache
When adding a new entry to an existing index via `git_index_read_index`,
be sure to remove the tree cache entry for that new path.  This will
mark all parent trees as dirty.
2016-06-02 02:38:55 -05:00
Edward Thomson
9167c1450e index_read_index: set flags for path_len correctly
Update the flags to reset the path_len (to emulate `index_insert`)
2016-06-02 02:36:50 -05:00
Edward Thomson
046ec3c9d6 index_read_index: differentiate on mode
Treat index entries with different modes as different, which they
are, at least for the purposes of up-to-date calculations.
2016-06-02 02:36:46 -05:00
Edward Thomson
93de20b8d2 index_read_index: reset error correctly
Clear any error state upon each iteration.  If one of the iterations
ends (with an error of `GIT_ITEROVER`) we need to reset that error to 0,
lest we stop the whole process prematurely.
2016-06-02 02:36:44 -05:00
Patrick Steinhardt
f80852af80 index: fix memory leak on error case 2016-05-02 14:30:14 +02:00
Carlos Martín Nieto
60a194aa86 tree: re-use the id and filename in the odb object
Instead of copying over the data into the individual entries, point to
the originals, which are already in a format we can use.
2016-03-20 11:00:12 +01:00
Patrick Steinhardt
80a834a5af index: assert required OID are non-NULL 2016-03-11 12:14:01 +01:00
Edward Thomson
6ddf533afc git_index_add: validate objects in index entries (optionally)
When `GIT_OPT_ENABLE_STRICT_OBJECT_CREATION` is turned on, validate
the index entries given to `git_index_add`.
2016-02-28 18:54:39 -05:00
Carlos Martín Nieto
9f4e7c8490 Merge pull request #3638 from ethomson/nsec
USE_NSECS fixes
2016-02-25 18:42:09 +01:00
Edward Thomson
3d6a42d1e1 nsec: support NDK's crazy nanoseconds
Android NDK does not have a `struct timespec` in its `struct stat`
for nanosecond support, instead it has a single nanosecond member inside
the struct stat itself.  We will use that and use a macro to expand to
the `st_mtim` / `st_mtimespec` definition on other systems (much like
the existing `st_mtime` backcompat definition).
2016-02-25 11:40:48 -05:00
Patrick Steinhardt
0f1e2d2066 index: fix contradicting comparison
The overflow check in `read_reuc` tries to verify if the
`git__strtol32` parses an integer bigger than UINT_MAX. The `tmp`
variable is casted to an unsigned int for this and then checked
for being greater than UINT_MAX, which obviously can never be
true.

Fix this by instead fixing the `mode` field's size in `struct
git_index_reuc_entry` to `uint32_t`. We can now parse the int
with `git__strtol64`, which can never return a value bigger than
`UINT32_MAX`, and additionally checking if the returned value is
smaller than zero.

We do not need to handle overflows explicitly here, as
`git__strtol64` returns an error when the returned value would
overflow.
2016-02-23 12:07:14 +01:00
Patrick Steinhardt
7808c93797 index: plug memory leak in read_conflict_names 2016-02-23 11:50:23 +01:00
Carlos Martín Nieto
5663d4f691 Merge pull request #3613 from ethomson/fixups
Remove most of the silly warnings
2016-02-18 12:31:56 +01:00
Carlos Martín Nieto
594a5d12d4 Merge pull request #3619 from ethomson/win32_forbidden
win32: allow us to read indexes with forbidden paths on win32
2016-02-18 12:28:06 +01:00
Edward Thomson
318b825e76 index: allow read of index w/ illegal entries
Allow `git_index_read` to handle reading existing indexes with
illegal entries.  Allow the low-level `git_index_add` to add
properly formed `git_index_entry`s even if they contain paths
that would be illegal for the current filesystem (eg, `AUX`).
Continue to disallow `git_index_add_bypath` from adding entries
that are illegal universally illegal (eg, `.git`, `foo/../bar`).
2016-02-17 13:10:33 +00:00
Edward Thomson
b2ca8d9c29 index: explicitly cast the teeny index entry members 2016-02-16 11:50:47 +00:00
Edward Thomson
997e030154 index: don't use seek return as an error code 2016-02-16 11:50:47 +00:00
Edward Thomson
9a634cba85 index: explicitly cast new hash size to an int 2016-02-16 11:50:46 +00:00
Arthur Schreiber
3679ebaef5 Horrible fix for #3173. 2016-02-11 23:41:34 +01:00
Carlos Martín Nieto
9d81509ab1 index: get rid of the locking
We don't support using an index object from multiple threads at the same
time, so the locking doesn't have any effect when following the
rules. If not following the rules, things are going to break down
anyway.
2015-12-28 13:55:44 +00:00
Vicent Marti
ef8b7febc5 index: Also size-hint the hash table
Note that we're not checking whether the resize succeeds; in OOM cases,
we let it run with a "small" vector and hash table and see if by chance
we can grow it dynamically as we insert the new entries. Nothing to
lose really.
2015-12-16 19:36:50 +01:00
Vicent Marti
d7d46cfbfd index: Preallocate the entries vector with size hint 2015-12-16 17:04:08 +01:00
Vicent Marti
0cc20a8c48 index: Adjust namemask & mode when filling 2015-12-16 17:04:08 +01:00
Vicent Marti
879ebab314 merge: Use git_index__fill to populate the index
Instead of calling `git_index_add` in a loop, use the new
`git_index_fill` internal API to fill the index with the initial staged
entries.

The new `fill` helper assumes that all the entries will be unique and
valid, so it can append them at the end of the entries vector and only
sort it once at the end. It performs no validation checks.

This prevents the quadratic behavior caused by having to sort the
entries list once after every insertion.
2015-12-16 12:30:52 +01:00
Carlos Martín Nieto
dc49eb585f Merge pull request #3538 from pks-t/pks/index-memory-leak
index: always queue `remove_entry` for removal
2015-12-10 11:57:44 +01:00
Patrick Steinhardt
b057fdef69 index: always queue remove_entry for removal
When replacing an index with a new one, we need to iterate
through all index entries in order to determine which entries are
equal. When it is not possible to re-use old entries for the new
index, we move it into a list of entries that are to be removed
and thus free'd.

When we encounter a non-zero error code, though, we skip adding
the current index entry to the remove-queue. `INSERT_MAP_EX`,
which is the function last run before adding to the remove-queue,
may return a positive non-zero code that indicates what exactly
happened while inserting the element. In this case we skip adding
the entry to the remove-queue but still continue the current
operation, leading to a leak of the current entry.

Fix this by checking for a negative return value instead of a
non-zero one when we want to add the current index entry to the
remove-queue.
2015-12-08 16:29:08 +01:00
Edward Thomson
626f9e243e index: canonicalize inserted paths safely
When adding to the index, we look to see if a portion of the given
path matches a portion of a path in the index.  If so, we will use
the existing path information.  For example, when adding `foo/bar.c`,
if there is an index entry to `FOO/other` and the filesystem is case
insensitive, then we will put `bar.c` into the existing tree instead
of creating a new one with a different case.

Use `strncmp` to do that instead of `memcmp`.  When we `bsearch`
into the index, we locate the position where the new entry would
go.  The index entry at that position does not necessarily have
a relation to the entry we're adding, so we cannot make assumptions
and use `memcmp`.  Instead, compare them as strings.

When canonicalizing paths, we look for the first index entry that
matches a given substring.
2015-12-03 16:27:15 -05:00
Edward Thomson
25e84f959a checkout: only consider nsecs when built that way
When examining the working directory and determining whether it's
up-to-date, only consider the nanoseconds in the index entry when
built with `GIT_USE_NSEC`.  This prevents us from believing that
the working directory is always dirty when the index was originally
written with a git client that uinderstands nsecs (like git 2.x).
2015-11-23 15:49:54 -05:00
Edward Thomson
5f32c50683 racy: make git_index_read_index handle raciness
Ensure that `git_index_read_index` clears the uptodate bit on
files that it modifies.

Further, do not propagate the cache from an on-disk index into
another on-disk index.  Although this should not be done, as
`git_index_read_index` is used to bring an in-memory index into
another index (that may or may not be on-disk), ensure that we do
not accidentally bring in these bits when misused.
2015-11-16 22:59:02 -05:00
Edward Thomson
27bc41cf17 index: clear uptodate bit on save
The uptodate bit should have a lifecycle of a single read->write
on the index.  Once the index is written, the files within it should
be scanned for racy timestamps against the new index timestamp.
2015-11-16 22:59:01 -05:00
Edward Thomson
d1101263f7 index: don't detect raciness in uptodate entries
Keep track of entries that we believe are up-to-date, because we
added the index entries since the index was loaded.  This prevents
us from unnecessarily examining files that we wrote during the
cleanup of racy entries (when we smudge racily clean files that have
a timestamp newer than or equal to the index's timestamp when we
read it).  Without keeping track of this, we would examine every
file that we just checked out for raciness, since all their timestamps
would be newer than the index's timestamp.
2015-11-16 22:58:58 -05:00
Edward Thomson
cb0ff012d3 racy-git: do a single index->workdir diff
When examining paths that are racily clean, do a single index->workdir
diff over the entirety of the racily clean files, instead of a diff
per file.
2015-11-16 22:58:57 -05:00
Carlos Martín Nieto
75a0ccf52f Merge pull request #3170 from CmdrMoozy/nsec_fix
git_index_entry__init_from_stat: set nsec fields in entry stats
2015-11-12 19:53:09 +01:00
Carlos Martín Nieto
ad8509ef9f index: overwrite the path when inserting conflicts
When we insert a conflict in a case-insensitive index, accept the
new entry's path as the correct case instead of leaving the path we
already had.

This puts `git_index_conflict_add()` on the same level as
`git_index_add()` in this respect.
2015-11-12 12:11:45 +01:00
Carlos Martín Nieto
16604d7469 index: correctly report which conflict stage has a wrong filemode
When we're at offset 'i', we're dealing with the 'i+1' stage, since
conflicts start at 1.
2015-11-12 12:11:45 +01:00
Edward Thomson
0bf77e3283 index: read_index must update hashes 2015-10-30 13:07:51 -04:00
Vicent Marti
1e5e02b4f4 pool: Simplify implementation 2015-10-28 10:13:13 +01:00
Vicent Marti
d307a0134b reuc: Be smarter when inserting new REUC entries
Inserting new REUC entries can quickly become pathological given that
each insert unsorts the REUC vector, and both subsequent lookups *and*
insertions will require sorting it again before being successful.

To avoid this, we're switching to `git_vector_insert_sorted`: this keeps
the REUC vector constantly sorted and lets us use the `on_dup` callback
to skip an extra binary search on each insertion.
2015-10-27 22:44:13 +01:00
Vicent Marti
128e94bbbb index: Remove unneeded consts 2015-10-21 12:04:53 +02:00
Edward Thomson
21515f228b index: also try conflict mode when inserting
When we do not trust the on-disk mode, we use the mode of an existing
index entry.  This allows us to preserve executable bits on platforms
that do not honor them on the filesystem.

If there is no stage 0 index entry, also look at conflicts to attempt
to answer this question:  prefer the data from the 'ours' side, then
the 'theirs' side before falling back to the common ancestor.
2015-09-30 09:06:09 -04:00
Axel Rasmussen
0226f7dd36 diff/index: respect USE_NSEC for racily clean file detection 2015-09-18 23:33:56 -07:00
Axel Rasmussen
e9e6df2c8f cmake: Only provide USE_NSEC if struct stat members are avilable.
This allows us to remove OS checks from source code, instead relying
on CMake to detect whether or not `struct stat` has the nanoseconds
members we rely on.
2015-09-18 23:33:56 -07:00
Axel Rasmussen
e7de893ef8 cmake: add USE_NSEC, and only check nanosec m/ctime if enabled 2015-09-18 23:33:55 -07:00
Carlos Martín Nieto
6d6020defc Merge pull request #3353 from ethomson/wrongcase_add
index: canonicalize directory case when adding
2015-09-08 18:34:51 +02:00
Edward Thomson
2964cbeae1 Merge pull request #3381 from leoyanggit/index_directory_iterator
New feature: add the ablility to iterate through a directory in index
2015-09-08 11:50:08 -04:00
Edward Thomson
a32bc85e84 git_index_add: allow case changing renames
On case insensitive platforms, allow `git_index_add` to provide a new
path for an existing index entry.  Previously, we would maintain the
case in an index entry without the ability to change it (except by
removing an entry and re-adding it.)

Higher-level functions (like `git_index_add_bypath` and
`git_index_add_frombuffers`) continue to keep the old path for easier
usage.
2015-09-08 11:34:00 -04:00
Edward Thomson
280adb3f94 index: canonicalize directory case when adding
On case insensitive systems, when given a user-provided path in the
higher-level index addition functions (eg `git_index_add_bypath` /
`git_index_add_frombuffer`), examine the index to try to match the
given path to an existing directory.

Various mechanisms can cause the on-disk representation of a folder
to not match the representation in HEAD or the index - for example,
a case changing rename of some file `a/file.txt` to `A/file.txt`
will update the paths in the index, but not rename the folder on
disk.

If a user subsequently adds `a/other.txt`, then this should be stored
in the index as `A/other.txt`.
2015-09-08 11:32:40 -04:00
Edward Thomson
9fd4c9c867 Merge pull request #3366 from libgit2/cmn/index-hashmap
Use a hashmap for path-based lookups in the index
2015-09-06 10:50:22 -04:00
Leo Yang
c097f7173d New API: git_index_find_prefix
Find the first index entry matching a prefix.
2015-09-04 12:24:36 -04:00