<commit_before>#include <glib.h>
#include <glib/gstdio.h>

#include <libgitcore/sha1.h>
#include <libgitcore/object-loader.h>
#include <libgitcore/commit.h>
#include <libgitcore/tree.h>
#include <libgitcore/blob.h>
#include <libgitcore/tree-iterator.h>

#include <libgitcore/loose-object-writer.h>

#include "file.h"
#include "vref.h"

#define WIZ_BASE "/home/john/.wizbit/"
#define WIZ_OBJECTS WIZ_BASE"objects/"
#define WIZ_WORKING WIZ_BASE"wc/"

struct wiz_file {
	FILE *fp;
	struct git_commit_writer *commit_writer;
};

struct wiz_file *wiz_file_open(wiz_vref ref, int flags, enum wiz_file_mode mode)
{
	struct wiz_file *file;
	GError *gerror = NULL;

	file = (struct wiz_file *)g_new0(struct wiz_file, 1);

	if (wiz_vref_compare(ref, WIZ_FILE_NEW) == 0) {
		file->fp = g_fopen(WIZ_WORKING"foo", "w");
	} else {
		GMappedFile *tmpfile;
		struct git_object_loader *loader;
		struct git_object_cache *store;
		struct git_commit *commit;
		struct git_tree *tree;
		struct git_tree_iterator iter;
		git_sha1 blob;
		void *data;
		enum git_object_type type;
		unsigned long size;
		struct git_error *error;

		loader = git_object_loader_new(WIZ_OBJECTS);
		store = git_object_cache_new(loader);

		commit = git_object_cache_lookup_commit(store, ref, &error);
		git_object_cache_parse_commit(store, commit, &error);

		tree = git_commit_get_tree(commit);
		git_object_cache_parse_tree(store, tree, &error);

		git_tree_get_iterator(tree, &iter, &error);

		memcpy((unsigned char *)blob, (const unsigned char *)*git_tree_iterator_sha1(&iter), 20);

		data = git_object_loader_load(loader, blob, &type, &size, &error);

		tmpfile = g_mapped_file_new(WIZ_WORKING"foo", TRUE, &gerror);
		memcpy(g_mapped_file_get_contents(tmpfile), data, size);
		g_mapped_file_free(tmpfile);

		file->fp = g_fopen(WIZ_WORKING"foo", "w");
	}

	file->commit_writer = git_commit_writer_new();

	return file;
}

void wiz_file_add_parent(struct wiz_file *file, wiz_vref ref)
{
	git_commit_writer_add_parent_sha1(file->commit_writer, ref);
}

void wiz_file_snapshot(struct wiz_file *file, wiz_vref ref)
{
        struct git_object_loader *loader;
        struct git_loose_object_writer *writer;
        struct git_blob_writer *blob_writer;
        git_sha1 blob;
        struct git_tree_writer *tree_writer;
        git_sha1 tree;
        git_sha1 commit;
        struct git_error *error;

        loader = git_object_loader_new(WIZ_OBJECTS);
        writer = git_loose_object_writer_new(loader, WIZ_OBJECTS);

        blob_writer = git_blob_writer_new();
	git_blob_writer_set_contents_from_file(blob_writer, "/tmp/foo");
	git_blob_writer_write(blob_writer, writer, blob, &error);

        tree_writer = git_tree_writer_new();
        git_tree_writer_add_sha1(tree_writer, 0, "path", blob);
        git_tree_writer_write(tree_writer, writer, tree, &error);

        git_commit_writer_set_tree_sha1(file->commit_writer, tree);
        git_commit_writer_set_author(file->commit_writer, "<NAME> <<EMAIL>>", 0);
        git_commit_writer_set_committer(file->commit_writer, "<NAME> <<EMAIL>>", 0);
        git_commit_writer_set_message(file->commit_writer, "Loreum Ipsum");
        git_commit_writer_write(file->commit_writer, writer, commit, &error);

	git_commit_writer_free(file->commit_writer);
	file->commit_writer = git_commit_writer_new();

	/* wiz_vref_copy(ref, commit); */
	memcpy(ref, commit, sizeof(wiz_vref));
}

void wiz_file_close(struct wiz_file *file)
{
}
<commit_msg>fflush and look for the correct temp file<commit_after>#include <glib.h>
#include <glib/gstdio.h>

#include <libgitcore/sha1.h>
#include <libgitcore/object-loader.h>
#include <libgitcore/commit.h>
#include <libgitcore/tree.h>
#include <libgitcore/blob.h>
#include <libgitcore/tree-iterator.h>

#include <libgitcore/loose-object-writer.h>

#include "file.h"
#include "vref.h"

#define WIZ_BASE "/home/john/.wizbit/"
#define WIZ_OBJECTS WIZ_BASE"objects/"
#define WIZ_WORKING WIZ_BASE"wc/"

struct wiz_file {
	FILE *fp;
	struct git_commit_writer *commit_writer;
};

struct wiz_file *wiz_file_open(wiz_vref ref, int flags, enum wiz_file_mode mode)
{
	struct wiz_file *file;
	GError *gerror = NULL;

	file = (struct wiz_file *)g_new0(struct wiz_file, 1);

	if (wiz_vref_compare(ref, WIZ_FILE_NEW) == 0) {
		file->fp = g_fopen(WIZ_WORKING"foo", "w");
	} else {
		GMappedFile *tmpfile;
		struct git_object_loader *loader;
		struct git_object_cache *store;
		struct git_commit *commit;
		struct git_tree *tree;
		struct git_tree_iterator iter;
		git_sha1 blob;
		void *data;
		enum git_object_type type;
		unsigned long size;
		struct git_error *error;

		loader = git_object_loader_new(WIZ_OBJECTS);
		store = git_object_cache_new(loader);

		commit = git_object_cache_lookup_commit(store, ref, &error);
		git_object_cache_parse_commit(store, commit, &error);

		tree = git_commit_get_tree(commit);
		git_object_cache_parse_tree(store, tree, &error);

		git_tree_get_iterator(tree, &iter, &error);

		memcpy((unsigned char *)blob, (const unsigned char *)*git_tree_iterator_sha1(&iter), 20);

		data = git_object_loader_load(loader, blob, &type, &size, &error);

		tmpfile = g_mapped_file_new(WIZ_WORKING"foo", TRUE, &gerror);
		memcpy(g_mapped_file_get_contents(tmpfile), data, size);
		g_mapped_file_free(tmpfile);

		file->fp = g_fopen(WIZ_WORKING"foo", "w");
	}

	file->commit_writer = git_commit_writer_new();

	return file;
}

void wiz_file_add_parent(struct wiz_file *file, wiz_vref ref)
{
	git_commit_writer_add_parent_sha1(file->commit_writer, ref);
}

void wiz_file_snapshot(struct wiz_file *file, wiz_vref ref)
{
        struct git_object_loader *loader;
        struct git_loose_object_writer *writer;
        struct git_blob_writer *blob_writer;
        git_sha1 blob;
        struct git_tree_writer *tree_writer;
        git_sha1 tree;
        git_sha1 commit;
        struct git_error *error;

	/* Its early days. While we are writing to this temporary file, we need to
	   fflush our changes to disk before we can do a snapshot
	*/
	fflush(file->fp);

        loader = git_object_loader_new(WIZ_OBJECTS);
        writer = git_loose_object_writer_new(loader, WIZ_OBJECTS);

        blob_writer = git_blob_writer_new();
	git_blob_writer_set_contents_from_file(blob_writer, WIZ_WORKING"foo");
	git_blob_writer_write(blob_writer, writer, blob, &error);

        tree_writer = git_tree_writer_new();
        git_tree_writer_add_sha1(tree_writer, 0, "path", blob);
        git_tree_writer_write(tree_writer, writer, tree, &error);

        git_commit_writer_set_tree_sha1(file->commit_writer, tree);
        git_commit_writer_set_author(file->commit_writer, "<NAME> <<EMAIL>>", 0);
        git_commit_writer_set_committer(file->commit_writer, "<NAME> <<EMAIL>>", 0);
        git_commit_writer_set_message(file->commit_writer, "Loreum Ipsum");
        git_commit_writer_write(file->commit_writer, writer, commit, &error);

	git_commit_writer_free(file->commit_writer);
	file->commit_writer = git_commit_writer_new();

	/* wiz_vref_copy(ref, commit); */
	memcpy(ref, commit, sizeof(wiz_vref));
}

void wiz_file_close(struct wiz_file *file)
{
}

{'fraction_non_alphanumeric': 0.115400873608567, 'fraction_numerical': 0.004790756657742709, 'mean_word_length': 5.129533678756476, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '452305', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2619, 'n_words': 497}