Commit Graph

64 Commits

Author SHA1 Message Date
Vicent Martí
dda708e78f error-handling: On-disk config file backend
Includes:

	- Proper error reporting when encountering syntax errors in a
	config file (file, line number, column).

	- Rewritten `config_write`, now with 99% less goto-spaghetti

	- Error state in `git_filebuf`: filebuf write functions no longer
	need to be checked for error returns. If any of the writes performed
	on a buffer fail, the last call to `git_filebuf_commit` or
	`git_filebuf_hash` will fail accordingly and set the appropiate error
	message. Baller!
2012-03-09 20:09:22 +01:00
Russell Belfer
854eccbb2d Clean up GIT_UNUSED macros on all platforms
It turns out that commit 31e9cfc4cbcaf1b38cdd3dbe3282a8f57e5366a5
did not fix the GIT_USUSED behavior on all platforms.  This commit
walks through and really cleans things up more thoroughly, getting
rid of the unnecessary stuff.

To remove the use of some GIT_UNUSED, I ended up adding a couple
of new iterators for hashtables that allow you to iterator just
over keys or just over values.

In making this change, I found a bug in the clar tests (where we
were doing *count++ but meant to do (*count)++ to increment the
value).  I fixed that but then found the test failing because it
was not really using an empty repo.  So, I took some of the code
that I wrote for iterator testing and moved it to clar_helpers.c,
then made use of that to make it easier to open fixtures on a
per test basis even within a single test file.
2012-03-02 15:51:55 -08:00
Vicent Martí
13224ea4aa buffer: Unify git_fbuffer and git_buf
This makes so much sense that I can't believe it hasn't been done
before. Kill the old `git_fbuffer` and read files straight into
`git_buf` objects.

Also: In order to fully support 4GB files in 32-bit systems, the
`git_buf` implementation has been changed from using `ssize_t` for
storage and storing negative values on allocation failure, to using
`size_t` and changing the buffer pointer to a magical pointer on
allocation failure.

Hopefully this won't break anything.
2012-02-27 05:30:07 +01:00
Carlos Martín Nieto
0a43d7cb19 config: correctly deal with setting a multivar with regex where there are no matches
We used to erroneously consider "^$" as a special case for appending a
value to a multivar. This was a misunderstanding and we should always
append a value if there are no existing values that match.

While we're in the area, replace all the variables in-memory in one
swoop and then replace them on disk so as to avoid matching a value
we've just introduced.
2012-02-25 19:00:06 +01:00
Carlos Martín Nieto
3005855f7e Implement setting multivars 2012-02-17 19:50:30 +01:00
Carlos Martín Nieto
5e0dc4af01 Support getting multivars 2012-02-17 19:43:43 +01:00
Carlos Martín Nieto
0774d94d31 Store multivars in the multimap 2012-02-17 19:43:43 +01:00
Carlos Martín Nieto
fefd4551a5 First round of config multimap changes
Move the configuration to use a multimap instead of a list. This
commit doesn't provide any functional changes but changes the support
structures.
2012-02-17 19:43:43 +01:00
schu
5e0de32818 Update Copyright header
Signed-off-by: schu <schu-github@schulog.org>
2012-02-13 17:11:09 +01:00
schu
15f52ae1d6 config_file: fix clang sizeof-pointer-memaccess 2012-02-13 13:30:04 +01:00
Carlos Martín Nieto
a9fe8ae0ee config: don't use 'section "subsection"' internal form on config_set
This had been left over from a time when I believed what the git
documentation had to say about case-sensitivity. The rest of the code
doesn't recognize this form and we hadn't noticed because most tests
don't try to get a recently-set variable but free and reload the
configuration, causing the right format to be used.
2012-01-23 22:14:04 +01:00
Carlos Martín Nieto
c1c399cf27 config: handle EOF properly
In the main loop we peek to see what kind of line the next one is. If
there are multiple newlines before the end of the file, the eof marker
won't be set after we read the last line with data and we'll try to
peek again. This peek will return LF (as it pretends that we have a
newline at EOF so other function don't need any special handling).

Fix cfg_getchar so it doesn't try to read past the last character in
the file and config_parse so it considers LF as EOF on peek (as we're
ignoring spaces) and sets the reader's EOF flag to exit the parsing
loop.
2012-01-13 19:33:54 +01:00
Vicent Martí
9dd4c3e806 config: Rename the delete callback name
`delete` is a reserved keyword in C++.
2011-12-31 05:58:26 +01:00
schu
e95849c14f config_file: honor error
Return an error if we can't write an updated version of the config file
after config_delete.

Along with that, fix an uninitialized warning.

Signed-off-by: schu <schu-github@schulog.org>
2011-12-19 16:07:21 +01:00
Carlos Martín Nieto
80a665aaca config: really delete variables
Instead of just setting the value to NULL, which gives unwanted
results when asking for that variable after deleting it, delete the
variable from the list and re-write the file.
2011-12-16 02:28:39 +01:00
Carlos Martín Nieto
7b2b4adfb1 Revert "config: Return ENOTFOUND when a variable was deleted"
This would make us think that config variables like

    [core]
        something

is missing.
2011-12-16 01:39:28 +01:00
Vicent Marti
2ea14da648 config: Return ENOTFOUND when a variable was deleted 2011-12-15 18:14:41 +01:00
Vicent Marti
9462c47143 repository: Change ownership semantics
The ownership semantics have been changed all over the library to be
consistent. There are no more "borrowed" or duplicated references.

Main changes:

	- `git_repository_open2` and `3` have been dropped.

	- Added setters and getters to hotswap all the repository owned
	objects:

		`git_repository_index`
		`git_repository_set_index`
		`git_repository_odb`
		`git_repository_set_odb`
		`git_repository_config`
		`git_repository_set_config`
		`git_repository_workdir`
		`git_repository_set_workdir`

	Now working directories/index files/ODBs and so on can be
	hot-swapped after creating a repository and between operations.

	- All these objects now have proper ownership semantics with
	refcounting: they all require freeing after they are no longer
	needed (the repository always keeps its internal reference).

	- Repository open and initialization has been updated to keep in
	mind the configuration files. Bare repositories are now always
	detected, and a default config file is created on init.

	- All the tests affected by these changes have been dropped from the
	old test suite and ported to the new one.
2011-11-26 08:37:08 +01:00
Carlos Martín Nieto
4e90a0a4a0 config: allow to open and write to a new file 2011-11-26 01:54:12 +01:00
Russell Belfer
b762e576c6 filebuf: add GIT_FILEBUF_INIT and protect multiple opens and cleanups
Update all stack allocations of git_filebuf to use GIT_FILEBUF_INIT
and make git_filebuf_open and git_filebuf_cleanup safe to be called
multiple times on the same buffer.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
2011-11-22 01:53:56 +01:00
Vicent Martí
89fb8f025a Merge pull request #456 from brodie/perm-fixes
Create objects, indexes, and directories with the right file permissions
2011-10-28 19:04:23 -07:00
Vicent Marti
3286c408ec global: Properly use git__ memory wrappers
Ensure that all memory related functions (malloc, calloc, strdup, free,
etc) are using their respective `git__` wrappers.
2011-10-28 19:02:36 -07:00
Oleg Andreev
9f861826be Fixed crash in config parser when empty value is encountered.
Example:

key1 = value1
key2 =

In this config the value will be a bad pointer which config object will attempt to free() causing a crash.
2011-10-27 16:45:44 +02:00
Brodie Rao
01ad7b3a9e *: correct and codify various file permissions
The following files now have 0444 permissions:

- loose objects
- pack indexes
- pack files
- packs downloaded by fetch
- packs downloaded by the HTTP transport

And the following files now have 0666 permissions:

- config files
- repository indexes
- reflogs
- refs

This brings libgit2 more in line with Git.

Note that git_filebuf_commit() and git_filebuf_commit_at() have both
gained a new mode parameter.

The latter change fixes an important issue where filebufs created with
GIT_FILEBUF_TEMPORARY received 0600 permissions (due to mkstemp(3)
usage). Now we chmod() the file before renaming it into place.

Tests have been added to confirm that new commit, tag, and tree
objects are created with the right permissions. I don't have access to
Windows, so for now I've guarded the tests with "#ifndef GIT_WIN32".
2011-10-14 16:07:47 -07:00
Carlos Martín Nieto
cd19ca9584 Squelch a couple of warnings
Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
2011-10-01 20:16:13 +02:00
Carlos Martín Nieto
9ac581bf7f config: behave like git with [section.subsection]
The documentation is a bit misleading. The subsection name is always
case-sensitive, but with a [section.subsection] header, the subsection
is transformed to lowercase when the configuration is parsed.

Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
2011-10-01 19:58:26 +02:00
Vicent Martí
71a4c1f16f Merge pull request #384 from kiryl/warnings
Add more -W flags to CFLAGS
2011-09-18 20:07:59 -07:00
Vicent Martí
b6ed727a45 Merge pull request #392 from sschuberth/development
Fix a bug and GCC warning introduced in 932669b
2011-09-18 19:52:40 -07:00
Vicent Marti
87d9869fc3 Tabify everything
There were quite a few places were spaces were being used instead of
tabs. Try to catch them all. This should hopefully not break anything.
Except for `git blame`. Oh well.
2011-09-19 03:34:49 +03:00
Vicent Marti
bb742ede3d Cleanup legal data
1. The license header is technically not valid if it doesn't have a
copyright signature.

2. The COPYING file has been updated with the different licenses used in
the project.

3. The full GPLv2 header in each file annoys me.
2011-09-19 01:54:32 +03:00
Sebastian Schuberth
79a343968a Fix a bug and GCC warning introduced in 932669b
For unsigned types, the comparison >= 0 is always true, so avoid it by using
a post-decrement and integrating the initial assigment into the loop body.
No change in behavior is intended.
2011-09-12 22:22:59 +02:00
Sebastian Schuberth
45e93ef34e Fix minor indentation issues (spaces to tabs) 2011-09-08 17:07:52 +02:00
Sebastian Schuberth
26e74c6ace Fix some random size_t vs. int conversion warnings 2011-09-08 17:07:37 +02:00
Kirill A. Shutemov
d568d5856b CMakefile: add -Wmissing-prototypes and fix warnings
Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
2011-08-30 23:55:22 +03:00
Kirill A. Shutemov
932669b865 Drop STRLEN() macros
There is no need in STRLEN macros. Compilers can do this trivial
optimization on its own.

Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
2011-08-25 16:41:15 +03:00
Vicent Marti
84dd3820d4 posix: Properly handle snprintf in all platforms 2011-08-18 02:35:28 +02:00
schu
5a0659fe3b config_file.c: fix memory leaks
Signed-off-by: schu <schu-github@schulog.org>
2011-08-17 14:36:53 +02:00
Carlos Martín Nieto
f58c53ce66 Correctly detect truncated input in header parsing
If the section header is the last line in the file,
parse_section_header would incorrectly decide that the input had been
truncated.

Fix this by checking whether the actual input line is correctly
formatted.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
2011-07-05 02:32:17 +02:00
Carlos Martín Nieto
6d4b609718 Add git_config_del to delete a variable
Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
2011-07-05 02:31:26 +02:00
Vicent Marti
f79026b491 fileops: Cleanup
Cleaned up the structure of the whole OS-abstraction layer.

fileops.c now contains a set of utility methods for file management used
by the library. These are abstractions on top of the original POSIX
calls.

There's a new file called `posix.c` that contains
emulations/reimplementations of all the POSIX calls the library uses.
These are prefixed with `p_`. There's a specific posix file for each
platform (win32 and unix).

All the path-related methods have been moved from `utils.c` to `path.c`
and have their own prefix.
2011-07-05 02:04:03 +02:00
Vicent Marti
cfef5fb779 config: foreach now returns variable values too 2011-06-29 15:09:21 +02:00
Carlos Martín Nieto
b76934de6c Remove double-space
Noticed by txdv

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
2011-06-17 22:30:29 +02:00
Vicent Marti
c716b1878e config: Fix unitialized variable warning 2011-06-17 19:47:58 +02:00
Carlos Martín Nieto
b2e361cc5e Plug two leaks in config writing
Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
2011-06-16 20:22:05 +02:00
Carlos Martín Nieto
711b1096f3 Indent config variables with tags
Confg variables are indended using tags and not four spaces as was
being done by the code.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
2011-06-14 13:09:40 +02:00
Carlos Martín Nieto
8bb198e674 config: implement config writing
After each variable gets set, we store it in our list (not completely
in the right position, but the close enough). Then we write out the
new config file in the same way that git.git does it (keep the rest of
the file intact and insert or replace the variable in its line).

Overwriting variables and adding new ones is supported (even on new
sections), though deleting isn't yet.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
2011-06-08 00:23:25 +02:00
Carlos Martín Nieto
3b3577c764 config: store new variables with the internal representation of the section
The section name should be stored in its case-sensitive variant when
we are adding a new variable. Use the internalize_section function to
do just that.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
2011-06-08 00:23:12 +02:00
Carlos Martín Nieto
5ab50417b7 Remove an unfortunate optimisation from cvar_match_section
The (rather late) early-exit code, which provides a negligible
optimisation causes cvar_match_section to return false negatives when
it's called with a section name instead of a full variable name.

Remove this optimisation.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
2011-06-08 00:12:26 +02:00
Carlos Martín Nieto
c7e6e95841 Don't try to parse an empty config file
Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
2011-05-31 17:57:18 +02:00
Carlos Martín Nieto
5892277cd0 Config parse header ext: don't allow text after closing quote
Nothing is allowed betwen the closing quotation mark and the ] so
return an error if there is.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
2011-05-31 17:33:24 +02:00