/* vtgvcsbackendsgit.c generated by valac 0.12.1.1-323029-dirty, the Vala compiler
 * generated from vtgvcsbackendsgit.vala, do not modify */

/*
 *  vtgvcsbackendsgit.vala - Vala developer toys for GEdit
 *  
 *  Copyright (C) 2008 - Andrea Del Signore <sejerpz@tin.it>
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *   
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330,
 *  Boston, MA 02111-1307, USA.
 */

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <valagee.h>
#include <sys/wait.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>


#define VTG_VCS_BACKENDS_TYPE_VCS_BASE (vtg_vcs_backends_vcs_base_get_type ())
#define VTG_VCS_BACKENDS_VCS_BASE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), VTG_VCS_BACKENDS_TYPE_VCS_BASE, VtgVcsBackendsVcsBase))
#define VTG_VCS_BACKENDS_VCS_BASE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), VTG_VCS_BACKENDS_TYPE_VCS_BASE, VtgVcsBackendsVcsBaseClass))
#define VTG_VCS_BACKENDS_IS_VCS_BASE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VTG_VCS_BACKENDS_TYPE_VCS_BASE))
#define VTG_VCS_BACKENDS_IS_VCS_BASE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), VTG_VCS_BACKENDS_TYPE_VCS_BASE))
#define VTG_VCS_BACKENDS_VCS_BASE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), VTG_VCS_BACKENDS_TYPE_VCS_BASE, VtgVcsBackendsVcsBaseClass))

typedef struct _VtgVcsBackendsVcsBase VtgVcsBackendsVcsBase;
typedef struct _VtgVcsBackendsVcsBaseClass VtgVcsBackendsVcsBaseClass;
typedef struct _VtgVcsBackendsVcsBasePrivate VtgVcsBackendsVcsBasePrivate;

#define VTG_VCS_BACKENDS_TYPE_ITEM (vtg_vcs_backends_item_get_type ())
#define VTG_VCS_BACKENDS_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), VTG_VCS_BACKENDS_TYPE_ITEM, VtgVcsBackendsItem))
#define VTG_VCS_BACKENDS_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), VTG_VCS_BACKENDS_TYPE_ITEM, VtgVcsBackendsItemClass))
#define VTG_VCS_BACKENDS_IS_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VTG_VCS_BACKENDS_TYPE_ITEM))
#define VTG_VCS_BACKENDS_IS_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), VTG_VCS_BACKENDS_TYPE_ITEM))
#define VTG_VCS_BACKENDS_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), VTG_VCS_BACKENDS_TYPE_ITEM, VtgVcsBackendsItemClass))

typedef struct _VtgVcsBackendsItem VtgVcsBackendsItem;
typedef struct _VtgVcsBackendsItemClass VtgVcsBackendsItemClass;

#define VTG_VCS_BACKENDS_TYPE_GIT (vtg_vcs_backends_git_get_type ())
#define VTG_VCS_BACKENDS_GIT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), VTG_VCS_BACKENDS_TYPE_GIT, VtgVcsBackendsGit))
#define VTG_VCS_BACKENDS_GIT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), VTG_VCS_BACKENDS_TYPE_GIT, VtgVcsBackendsGitClass))
#define VTG_VCS_BACKENDS_IS_GIT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VTG_VCS_BACKENDS_TYPE_GIT))
#define VTG_VCS_BACKENDS_IS_GIT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), VTG_VCS_BACKENDS_TYPE_GIT))
#define VTG_VCS_BACKENDS_GIT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), VTG_VCS_BACKENDS_TYPE_GIT, VtgVcsBackendsGitClass))

typedef struct _VtgVcsBackendsGit VtgVcsBackendsGit;
typedef struct _VtgVcsBackendsGitClass VtgVcsBackendsGitClass;
typedef struct _VtgVcsBackendsGitPrivate VtgVcsBackendsGitPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _vala_iterable_unref0(var) ((var == NULL) ? NULL : (var = (vala_iterable_unref (var), NULL)))

#define VTG_VCS_BACKENDS_TYPE_STATES (vtg_vcs_backends_states_get_type ())
typedef struct _VtgVcsBackendsItemPrivate VtgVcsBackendsItemPrivate;
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_pattern_spec_free0(var) ((var == NULL) ? NULL : (var = (g_pattern_spec_free (var), NULL)))

struct _VtgVcsBackendsVcsBase {
	GObject parent_instance;
	VtgVcsBackendsVcsBasePrivate * priv;
};

struct _VtgVcsBackendsVcsBaseClass {
	GObjectClass parent_class;
	ValaList* (*get_items) (VtgVcsBackendsVcsBase* self, const gchar* path, GError** error);
	gboolean (*test) (VtgVcsBackendsVcsBase* self, const gchar* path);
};

struct _VtgVcsBackendsGit {
	VtgVcsBackendsVcsBase parent_instance;
	VtgVcsBackendsGitPrivate * priv;
};

struct _VtgVcsBackendsGitClass {
	VtgVcsBackendsVcsBaseClass parent_class;
};

typedef enum  {
	VTG_VCS_BACKENDS_VCS_ERROR_CommandFailed
} VtgVcsBackendsVcsError;
#define VTG_VCS_BACKENDS_VCS_ERROR vtg_vcs_backends_vcs_error_quark ()
typedef enum  {
	VTG_VCS_BACKENDS_STATES_UNTRACKED,
	VTG_VCS_BACKENDS_STATES_ADDED,
	VTG_VCS_BACKENDS_STATES_MODIFIED,
	VTG_VCS_BACKENDS_STATES_DELETED
} VtgVcsBackendsStates;

struct _VtgVcsBackendsItem {
	GObject parent_instance;
	VtgVcsBackendsItemPrivate * priv;
	gchar* name;
	VtgVcsBackendsStates state;
};

struct _VtgVcsBackendsItemClass {
	GObjectClass parent_class;
};


static gpointer vtg_vcs_backends_git_parent_class = NULL;
static GType vtg_vcs_backends_git_type_id = 0;

GType vtg_vcs_backends_vcs_base_get_type (void) G_GNUC_CONST;
GType vtg_vcs_backends_vcs_base_register_type (GTypeModule * module);
GType vtg_vcs_backends_item_get_type (void) G_GNUC_CONST;
GType vtg_vcs_backends_item_register_type (GTypeModule * module);
GType vtg_vcs_backends_git_get_type (void) G_GNUC_CONST;
GType vtg_vcs_backends_git_register_type (GTypeModule * module);
enum  {
	VTG_VCS_BACKENDS_GIT_DUMMY_PROPERTY
};
VtgVcsBackendsGit* vtg_vcs_backends_git_new (void);
VtgVcsBackendsGit* vtg_vcs_backends_git_construct (GType object_type);
VtgVcsBackendsVcsBase* vtg_vcs_backends_vcs_base_construct (GType object_type);
GQuark vtg_vcs_backends_vcs_error_quark (void);
static ValaList* vtg_vcs_backends_git_real_get_items (VtgVcsBackendsVcsBase* base, const gchar* path, GError** error);
VtgVcsBackendsItem* vtg_vcs_backends_item_new (void);
VtgVcsBackendsItem* vtg_vcs_backends_item_construct (GType object_type);
GType vtg_vcs_backends_states_get_type (void) G_GNUC_CONST;
static gboolean vtg_vcs_backends_git_real_test (VtgVcsBackendsVcsBase* base, const gchar* path);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);
static gint _vala_array_length (gpointer array);


VtgVcsBackendsGit* vtg_vcs_backends_git_construct (GType object_type) {
	VtgVcsBackendsGit * self = NULL;
	self = (VtgVcsBackendsGit*) vtg_vcs_backends_vcs_base_construct (object_type);
	return self;
}


VtgVcsBackendsGit* vtg_vcs_backends_git_new (void) {
	return vtg_vcs_backends_git_construct (VTG_VCS_BACKENDS_TYPE_GIT);
}


static gchar* string_replace (const gchar* self, const gchar* old, const gchar* replacement) {
	gchar* result = NULL;
	gchar* _tmp0_ = NULL;
	gchar* _tmp1_;
	GRegex* _tmp2_ = NULL;
	GRegex* _tmp3_;
	GRegex* regex;
	gchar* _tmp4_ = NULL;
	gchar* _tmp5_;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	_tmp0_ = g_regex_escape_string (old, -1);
	_tmp1_ = _tmp0_;
	_tmp2_ = g_regex_new (_tmp1_, 0, 0, &_inner_error_);
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	regex = _tmp3_;
	if (_inner_error_ != NULL) {
		if (_inner_error_->domain == G_REGEX_ERROR) {
			goto __catch41_g_regex_error;
		}
		g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return NULL;
	}
	_tmp4_ = g_regex_replace_literal (regex, self, (gssize) (-1), 0, replacement, 0, &_inner_error_);
	_tmp5_ = _tmp4_;
	if (_inner_error_ != NULL) {
		_g_regex_unref0 (regex);
		if (_inner_error_->domain == G_REGEX_ERROR) {
			goto __catch41_g_regex_error;
		}
		_g_regex_unref0 (regex);
		g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return NULL;
	}
	result = _tmp5_;
	_g_regex_unref0 (regex);
	return result;
	_g_regex_unref0 (regex);
	goto __finally41;
	__catch41_g_regex_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		g_assert_not_reached ();
		_g_error_free0 (e);
	}
	__finally41:
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return NULL;
	}
}


static ValaList* vtg_vcs_backends_git_real_get_items (VtgVcsBackendsVcsBase* base, const gchar* path, GError** error) {
	VtgVcsBackendsGit * self;
	ValaList* result = NULL;
	ValaArrayList* _tmp0_ = NULL;
	ValaList* results;
	gchar* stdo = NULL;
	gchar* stde = NULL;
	gint exit_status = 0;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar** _tmp3_ = NULL;
	gchar** _tmp4_;
	gint _tmp4__length1;
	gchar* _tmp5_ = NULL;
	gchar* _tmp6_ = NULL;
	gint _tmp7_;
	gboolean _tmp8_;
	gboolean _tmp9_;
	gboolean _tmp10_;
	GError * _inner_error_ = NULL;
	self = (VtgVcsBackendsGit*) base;
	g_return_val_if_fail (path != NULL, NULL);
	_tmp0_ = vala_array_list_new (VTG_VCS_BACKENDS_TYPE_ITEM, (GBoxedCopyFunc) g_object_ref, g_object_unref, g_direct_equal);
	results = (ValaList*) _tmp0_;
	_tmp1_ = g_strdup ("git");
	_tmp2_ = g_strdup ("status");
	_tmp3_ = g_new0 (gchar*, 2 + 1);
	_tmp3_[0] = _tmp1_;
	_tmp3_[1] = _tmp2_;
	_tmp4_ = _tmp3_;
	_tmp4__length1 = 2;
	_tmp8_ = g_spawn_sync (path, _tmp4_, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &_tmp5_, &_tmp6_, &_tmp7_, &_inner_error_);
	_g_free0 (stdo);
	stdo = _tmp5_;
	_g_free0 (stde);
	stde = _tmp6_;
	exit_status = _tmp7_;
	_tmp9_ = _tmp8_;
	_tmp4_ = (_vala_array_free (_tmp4_, _tmp4__length1, (GDestroyNotify) g_free), NULL);
	_tmp10_ = _tmp9_;
	if (_inner_error_ != NULL) {
		if ((_inner_error_->domain == G_SPAWN_ERROR) || (_inner_error_->domain == VTG_VCS_BACKENDS_VCS_ERROR)) {
			g_propagate_error (error, _inner_error_);
			_g_free0 (stde);
			_g_free0 (stdo);
			_vala_iterable_unref0 (results);
			return NULL;
		} else {
			_g_free0 (stde);
			_g_free0 (stdo);
			_vala_iterable_unref0 (results);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return NULL;
		}
	}
	if (_tmp10_) {
		gint _tmp11_;
		gint exit;
		gboolean _tmp12_ = FALSE;
		_tmp11_ = WEXITSTATUS (exit_status);
		exit = _tmp11_;
		if (exit == 0) {
			_tmp12_ = TRUE;
		} else {
			_tmp12_ = exit == 1;
		}
		if (_tmp12_) {
			gchar** _tmp13_;
			gchar** _tmp14_ = NULL;
			gchar** lines;
			gint lines_length1;
			gint _lines_size_;
			gint idx;
			GPatternSpec* _tmp15_ = NULL;
			GPatternSpec* modp;
			GPatternSpec* _tmp16_ = NULL;
			GPatternSpec* newp;
			GPatternSpec* _tmp17_ = NULL;
			GPatternSpec* delp;
			_tmp14_ = _tmp13_ = g_strsplit (stdo, "\n", 0);
			lines = _tmp14_;
			lines_length1 = _vala_array_length (_tmp13_);
			_lines_size_ = _vala_array_length (_tmp13_);
			idx = 0;
			_tmp15_ = g_pattern_spec_new ("#\tmodified:   *");
			modp = _tmp15_;
			_tmp16_ = g_pattern_spec_new ("#\tnew file:   *");
			newp = _tmp16_;
			_tmp17_ = g_pattern_spec_new ("#\tdeleted:    *");
			delp = _tmp17_;
			while (TRUE) {
				gchar* _tmp18_;
				gchar* line;
				gboolean _tmp19_;
				if (!(lines[idx] != NULL)) {
					break;
				}
				_tmp18_ = g_strdup (lines[idx]);
				line = _tmp18_;
				_tmp19_ = g_pattern_match_string (modp, line);
				if (_tmp19_) {
					VtgVcsBackendsItem* _tmp20_ = NULL;
					VtgVcsBackendsItem* item;
					gchar* _tmp21_ = NULL;
					_tmp20_ = vtg_vcs_backends_item_new ();
					item = _tmp20_;
					item->state = VTG_VCS_BACKENDS_STATES_MODIFIED;
					_tmp21_ = string_replace (line, "#\tmodified:   ", "");
					_g_free0 (item->name);
					item->name = _tmp21_;
					vala_collection_add ((ValaCollection*) results, item);
					_g_object_unref0 (item);
				} else {
					gboolean _tmp22_;
					_tmp22_ = g_pattern_match_string (newp, line);
					if (_tmp22_) {
						VtgVcsBackendsItem* _tmp23_ = NULL;
						VtgVcsBackendsItem* item;
						gchar* _tmp24_ = NULL;
						_tmp23_ = vtg_vcs_backends_item_new ();
						item = _tmp23_;
						item->state = VTG_VCS_BACKENDS_STATES_ADDED;
						_tmp24_ = string_replace (line, "#\tnew file:   ", "");
						_g_free0 (item->name);
						item->name = _tmp24_;
						vala_collection_add ((ValaCollection*) results, item);
						_g_object_unref0 (item);
					} else {
						gboolean _tmp25_;
						_tmp25_ = g_pattern_match_string (delp, line);
						if (_tmp25_) {
							VtgVcsBackendsItem* _tmp26_ = NULL;
							VtgVcsBackendsItem* item;
							gchar* _tmp27_ = NULL;
							_tmp26_ = vtg_vcs_backends_item_new ();
							item = _tmp26_;
							item->state = VTG_VCS_BACKENDS_STATES_DELETED;
							_tmp27_ = string_replace (line, "#\tdeleted:    ", "");
							_g_free0 (item->name);
							item->name = _tmp27_;
							vala_collection_add ((ValaCollection*) results, item);
							_g_object_unref0 (item);
						}
					}
				}
				idx++;
				_g_free0 (line);
			}
			_g_pattern_spec_free0 (delp);
			_g_pattern_spec_free0 (newp);
			_g_pattern_spec_free0 (modp);
			lines = (_vala_array_free (lines, lines_length1, (GDestroyNotify) g_free), NULL);
		} else {
			const gchar* _tmp28_ = NULL;
			gchar* _tmp29_ = NULL;
			gchar* _tmp30_;
			GError* _tmp31_ = NULL;
			GError* _tmp32_;
			_tmp28_ = _ ("error executing the git status command.\n%s");
			_tmp29_ = g_strdup_printf (_tmp28_, stde);
			_tmp30_ = _tmp29_;
			_tmp31_ = g_error_new_literal (VTG_VCS_BACKENDS_VCS_ERROR, VTG_VCS_BACKENDS_VCS_ERROR_CommandFailed, _tmp30_);
			_tmp32_ = _tmp31_;
			_g_free0 (_tmp30_);
			_inner_error_ = _tmp32_;
			if ((_inner_error_->domain == G_SPAWN_ERROR) || (_inner_error_->domain == VTG_VCS_BACKENDS_VCS_ERROR)) {
				g_propagate_error (error, _inner_error_);
				_g_free0 (stde);
				_g_free0 (stdo);
				_vala_iterable_unref0 (results);
				return NULL;
			} else {
				_g_free0 (stde);
				_g_free0 (stdo);
				_vala_iterable_unref0 (results);
				g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
				g_clear_error (&_inner_error_);
				return NULL;
			}
		}
	}
	result = results;
	_g_free0 (stde);
	_g_free0 (stdo);
	return result;
}


static gboolean vtg_vcs_backends_git_real_test (VtgVcsBackendsVcsBase* base, const gchar* path) {
	VtgVcsBackendsGit * self;
	gboolean result = FALSE;
	gchar* _tmp0_ = NULL;
	gchar* git_dir;
	gboolean _tmp1_;
	self = (VtgVcsBackendsGit*) base;
	g_return_val_if_fail (path != NULL, FALSE);
	_tmp0_ = g_build_filename (path, ".git", NULL);
	git_dir = _tmp0_;
	_tmp1_ = g_file_test (git_dir, G_FILE_TEST_IS_DIR);
	if (_tmp1_) {
		result = TRUE;
		_g_free0 (git_dir);
		return result;
	}
	result = FALSE;
	_g_free0 (git_dir);
	return result;
}


static void vtg_vcs_backends_git_class_init (VtgVcsBackendsGitClass * klass) {
	vtg_vcs_backends_git_parent_class = g_type_class_peek_parent (klass);
	VTG_VCS_BACKENDS_VCS_BASE_CLASS (klass)->get_items = vtg_vcs_backends_git_real_get_items;
	VTG_VCS_BACKENDS_VCS_BASE_CLASS (klass)->test = vtg_vcs_backends_git_real_test;
}


static void vtg_vcs_backends_git_instance_init (VtgVcsBackendsGit * self) {
}


GType vtg_vcs_backends_git_get_type (void) {
	return vtg_vcs_backends_git_type_id;
}


GType vtg_vcs_backends_git_register_type (GTypeModule * module) {
	static const GTypeInfo g_define_type_info = { sizeof (VtgVcsBackendsGitClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) vtg_vcs_backends_git_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (VtgVcsBackendsGit), 0, (GInstanceInitFunc) vtg_vcs_backends_git_instance_init, NULL };
	vtg_vcs_backends_git_type_id = g_type_module_register_type (module, VTG_VCS_BACKENDS_TYPE_VCS_BASE, "VtgVcsBackendsGit", &g_define_type_info, 0);
	return vtg_vcs_backends_git_type_id;
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}


static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}


static gint _vala_array_length (gpointer array) {
	int length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}



