/// <summary>
        /// Inserts a <see cref="LibGit2Sharp.Commit" /> into the object database by applying a <see cref="TreeDefinition"/>.
        /// </summary>
        /// <param name="repository">The repository.</param>
        /// <param name="changes">The changes.</param>
        /// <param name="message">The message.</param>
        /// <param name="author">The author.</param>
        /// <param name="committer">The committer.</param>
        /// <param name="hooks">The hooks.</param>
        /// <param name="options">The options.</param>
        /// <param name="mergeParent">The parent commit for a merge.</param>
        /// <returns>The created <see cref="LibGit2Sharp.Commit" />.</returns>
        internal static Commit CommitChanges(this IRepository repository, MetadataTreeChanges changes, string message, Signature author, Signature committer, GitHooks hooks, CommitOptions options = null, Commit mergeParent = null)
        {
            TreeDefinition definition;

            if (changes.OldRepository?.CommitId != null)
            {
                if (repository.Head.Tip.Id != changes.OldRepository.CommitId)
                {
                    throw new NotSupportedException("Changes are not based on the HEAD commit.");
                }
                var startCommit = repository.Lookup <Commit>(changes.OldRepository.CommitId);
                definition = TreeDefinition.From(startCommit);
            }
            else if (repository.Info.IsHeadUnborn)
            {
                definition = new TreeDefinition();
            }
            else
            {
                throw new NotSupportedException("Changes are not based on the HEAD commit.");
            }

            if (!hooks.OnCommitStarted(changes, message))
            {
                return(null);
            }

            changes.UpdateTreeDefinition(repository, definition);

            var result = Commit(repository, definition, message, author, committer, options, mergeParent);

            hooks.OnCommitCompleted(changes, message, result.Id);

            return(result);
        }