Commit Graph

167 Commits

Author SHA1 Message Date
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
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
909d549436 giterr_set: consistent error messages
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
2016-12-29 12:26:03 +00:00
Patrick Steinhardt
a719ef5e6d commit: always initialize commit message
When parsing a commit, we will treat all bytes left after parsing
the headers as the commit message. When no bytes are left, we
leave the commit's message uninitialized. While uncommon to have
a commit without message, this is the right behavior as Git
unfortunately allows for empty commit messages.

Given that this scenario is so uncommon, most programs acting on
the commit message will never check if the message is actually
set, which may lead to errors. To work around the error and not
lay the burden of checking for empty commit messages to the
developer, initialize the commit message with an empty string
when no commit message is given.
2016-10-09 13:26:21 +02:00
Edward Thomson
0d77a56f39 checkout: drop unused repo 2016-06-01 10:03:51 -05:00
John Haley
225cb8809e Fix git_commit_create for an initial commit
When calling `git_commit_create` with an empty array of `parents` and `parent_count == 0` 
the call will segfault at https://github.com/libgit2/libgit2/blob/master/src/commit.c#L107 
when it's trying to compare `current_id` to a null parent oid. 

This just puts in a check to stop that segfault.
2016-05-03 13:32:32 -07:00
Edward Thomson
f0224772ee git_object_dup: introduce typesafe versions 2016-03-23 17:08:37 -04:00
Edward Thomson
ba3493228c Merge pull request #3673 from libgit2/cmn/commit-with-signature
commit: add function to attach a signature to a commit
2016-03-17 06:57:56 -07:00
Carlos Martín Nieto
bf804d407e commit: fix extraction of single-line signatures
The function to extract signatures suffers from a similar bug to the
header field finding one by having an unecessary line feed check as a
break condition of its loop.

Fix that and add a test for this single-line signature situation.
2016-03-17 10:48:06 +01:00
Carlos Martín Nieto
02d61a3b66 commit: add function to attach a signature to a commit
In combination with the function which creates a commit into a buffer,
this allows us to more easily create signed commits.
2016-03-15 12:55:03 +01:00
Carlos Martín Nieto
47cb42da5a commit: split creating the commit and writing it out
Sometimes you want to create a commit but not write it out to the
objectdb immediately. For these cases, provide a new function to
retrieve the buffer instead of having to go through the db.
2016-03-08 13:11:49 +01:00
Edward Thomson
ef63bab306 git_commit: validate tree and parent ids
When `GIT_OPT_ENABLE_STRICT_OBJECT_CREATION` is turned on, validate
the tree and parent ids given to commit creation functions.
2016-02-28 12:38:39 -05:00
Carlos Martín Nieto
eadd0f05f6 commit: expose the different kinds of errors
We should be checking whether the object we're looking up is a commit,
and we should let the caller know whether the not-found return code
comes from a bad object type or just a missing signature.
2016-02-16 14:06:48 +01:00
Carlos Martín Nieto
460ae11f0a commit: don't forget the last header field
When we moved the logic to handle the first one, wrong loop logic was
kept in place which meant we still finished early. But we now notice it
because we're not reading past the last LF we find.

This was not noticed before as the last field in the tested commit was
multi-line which does not trigger the early break.
2016-02-11 22:19:20 +01:00
Vicent Marti
488e2b8505 Merge pull request #3599 from libgit2/gpgsign
Introduce git_commit_extract_signature
2016-02-09 16:26:58 +01:00
Carlos Martín Nieto
a65afb757e Introduce git_commit_extract_signature
This returns the GPG signature for a commit and its contents without the
signature block, allowing for the verification of the commit's
signature.
2016-02-09 15:58:16 +01:00
Carlos Martín Nieto
f55eca167c commit: also match the first header field when searching
We were searching only past the first header field, which meant we were
unable to find e.g. `tree` which is the first field.

While here, make sure to set an error message in case we cannot find the
field.
2016-02-09 07:17:26 +01:00
Patrick Steinhardt
7f8fe1d45e commit: introduce git_commit_body
It is already possible to get a commit's summary with the
`git_commit_summary` function. It is not possible to get the
remaining part of the commit message, that is the commit
message's body.

Fix this by introducing a new function `git_commit_body`.
2015-12-01 10:07:00 +01:00
Stjepan Rajko
f5f96a23ee Fix git_commit_summary to convert newlines to spaces even after
whitespace.  Collapse spaces around newlines for the summary.
2015-11-03 17:50:55 -05:00
Carlos Martín Nieto
a3f42fe8e4 commit: allow retrieving an arbitrary header field
This allows the user to look up fields which we don't parse in libgit2,
and allows them to access gpgsig or mergetag fields if they wish to
check the signature.
2015-06-22 15:56:31 +02:00
Carlos Martín Nieto
65d69fe854 commit: ignore multiple author fields
Some tools create multiple author fields. git is rather lax when parsing
them, although fsck does complain about them. This means that they exist
in the wild.

As it's not too taxing to check for them, and there shouldn't be a
noticeable slowdown when dealing with correct commits, add logic to skip
over these extra fields when parsing the commit.
2015-06-11 08:24:58 +02:00
Carlos Martín Nieto
659cf2029f Remove the signature from ref-modifying functions
The signature for the reflog is not something which changes
dynamically. Almost all uses will be NULL, since we want for the
repository's default identity to be used, making it noise.

In order to allow for changing the identity, we instead provide
git_repository_set_ident() and git_repository_ident() which allow a user
to override the choice of signature.
2015-03-03 14:40:50 +01:00
Stefan Widgren
c8e02b8776 Remove extra semicolon outside of a function
Without this change, compiling with gcc and pedantic generates warning:
ISO C does not allow extra ‘;’ outside of a function.
2015-02-15 21:07:05 +01:00
Edward Thomson
a612a25fa6 git_rebase_commit: write HEAD's reflog appropriately 2014-10-26 22:59:27 -04:00
Carlos Martín Nieto
217c029b54 commit: safer commit creation with reference update
The current version of the commit creation and amend function are unsafe
to use when passing the update_ref parameter, as they do not check that
the reference at the moment of update points to what the user expects.

Make sure that we're moving history forward when we ask the library to
update the reference for us by checking that the first parent of the new
commit is the current value of the reference. We also make sure that the
ref we're updating hasn't moved between the read and the write.

Similarly, when amending a commit, make sure that the current tip of the
branch is the commit we're amending.
2014-04-30 00:41:37 +02:00
Carlos Martín Nieto
7c1ee212b7 commit: simplify and correct refcounting in nth_gen_ancestor
We can make use of git_object_dup to use refcounting instead of pointer
comparison to make sure we don't free the caller's object.

This also lets us simplify the case for '~0' which is now just an
assignment instead of looking up the object we have at hand.
2014-03-07 16:03:10 +01:00
Edward Thomson
4f46a98b6e Remove now-duplicated stdarg.h include 2014-02-24 23:32:25 -08:00
Russell Belfer
80c29fe93e Add git_commit_amend API
This adds an API to amend an existing commit, basically a shorthand
for creating a new commit filling in missing parameters from the
values of an existing commit.  As part of this, I also added a new
"sys" API to create a commit using a callback to get the parents.
This allowed me to rewrite all the other commit creation APIs so
that temporary allocations are no longer needed.
2014-02-07 16:17:59 -08:00
Ben Straub
0de2c4e3a3 Merge remote-tracking branch 'libgit2/development' into bs/more-reflog-stuff 2014-02-05 13:15:57 -08:00
Carlos Martín Nieto
a6563619e9 commit: faster parsing
The current code issues a lot of strncmp() calls in order to check for
the end of the header, simply in order to copy it and start going
through it again. These are a lot of calls for something we can check as
we go along. Knowing the amount of parents beforehand to reduce
allocations in extreme cases does not make up for them.

Instead start parsing immediately and check for the double-newline after
each header field, leaving the raw_header allocation for the end, which
lets us go through the header once and reduces the amount of strncmp()
calls significantly.

In unscientific testing, this has reduced a shortlog-like usage (walking
though the whole history of a branch and extracting data from the
commits) of git.git from ~830ms to ~700ms and makes the time we spend in
strncmp() negligible.
2014-02-05 14:31:13 +01:00
Ben Straub
0adb06065b Fix reflog message when creating commits 2014-02-04 15:32:57 -08:00
Carlos Martín Nieto
47e28349bc commit: remvoe legacy 'oid' naming 2014-01-25 08:15:44 +01:00
Edward Thomson
238e814972 Summarize empty messages 2014-01-22 14:41:04 -05:00
Carlos Martín Nieto
0b28217bda refs: remove the _with_log differentiation
Any well-behaved program should write a descriptive message to the
reflog whenever it updates a reference. Let's make this more prominent
by removing the version without the reflog parameters.
2014-01-15 13:32:43 +01:00
Paul Holden
be0a1a7958 commit: Fix potential segfault in git_commit_message
Dereferencing commit pointer before asserting
2013-12-08 10:21:13 -08:00
Edward Thomson
300d192f7e Introduce git_revert to revert a single commit 2013-12-02 16:57:41 -06:00
nulltoken
598f069b99 commit: Introduce git_commit_message_raw() 2013-10-03 07:59:55 +02:00
nulltoken
d27a441dde commit: Trim message leading newlines
Fix libgit2/libgit2sharp#522
2013-09-30 11:33:58 +02:00
Russell Belfer
584f2d3013 Fix warnings on Win64 2013-07-11 11:04:42 -07:00
Russell Belfer
9abc78ae61 Convert commit->parent_ids to git_array_t
This converts the array of parent SHAs from a git_vector where
each SHA has to be separately allocated to a git_array_t where
all the SHAs can be kept in one block.  Since the two collections
have almost identical APIs, there isn't much involved in making
the change.  I did add an API to git_array_t so that it could be
allocated at a precise initial size.
2013-07-10 20:50:32 +02:00
Russell Belfer
f094f9052f Add raw header access to commit API 2013-07-10 20:50:32 +02:00
Russell Belfer
58206c9ae7 Add cat-file example and increase const use in API
This adds an example implementation that emulates git cat-file.
It is a convenient and relatively simple example of getting data
out of a repository.

Implementing this also revealed that there are a number of APIs
that are still not using const pointers to objects that really
ought to be.  The main cause of this is that `git_vector_bsearch`
may need to call `git_vector_sort` before doing the search, so a
const pointer to the vector is not allowed.  However, for tree
objects, with a little care, we can ensure that the vector of
tree entries is always sorted and allow lookups to take a const
pointer.  Also, the missing const in commit objects just looks
like an oversight.
2013-05-16 10:38:27 -07:00
Linquize
e583334c00 Fix broken build when MSVC SDL checks is enabled 2013-05-11 20:13:26 +08:00
nulltoken
467cbec73d commit: make create_from_oids() accept plain oid 2013-05-05 16:48:34 +02:00
nulltoken
ce72e399d2 commit: guard create() against not owned trees 2013-05-05 16:47:07 +02:00
Russell Belfer
3f27127d15 Simplify object table parse functions
This unifies the object parse functions into one signature that
takes an odb_object.
2013-04-22 16:52:06 +02:00
Russell Belfer
786062639f Add callback to git_objects_table
This adds create and free callback to the git_objects_table so
that more of the creation and destruction of objects can be table
driven instead of using switch statements.  This also makes the
semantics of certain object creation functions consistent so that
we can make better use of function pointers.  This also fixes a
theoretical error case where an object allocation fails and we
end up storing NULL into the cache.
2013-04-22 16:51:40 +02:00
Russell Belfer
badd85a613 Use git_odb_object_data/_size whereever possible
This uses the odb object accessors so we can change the internals
more easily...
2013-04-22 16:50:51 +02:00
Vicent Marti
8842c75f17 What has science done. 2013-04-22 16:50:50 +02:00