From a2ffbdd0a4cb36abb2fc980f8b4530eb84a24f88 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Tue, 11 Oct 2011 14:26:48 -0400 Subject: [PATCH] Switch to GLib --- Makefile-hacktree.am | 21 +++++--- Makefile.am | 1 + src/hacktree-repo.c | 123 ++++++++++++++++++++++++++++++++++++++++++ src/hacktree-repo.h | 59 ++++++++++++++++++++ src/hacktree-types.h | 33 ++++++++++++ src/hacktree.h | 27 ++++++++++ src/hacktree.in | 31 ----------- src/ht-builtin-init.c | 63 ++++++++++++++++++++++ src/ht-builtins.h | 43 +++++++++++++++ src/main.c | 94 ++++++++++++++++++++++++++++++++ 10 files changed, 458 insertions(+), 37 deletions(-) create mode 100644 src/hacktree-repo.c create mode 100644 src/hacktree-repo.h create mode 100644 src/hacktree-types.h create mode 100644 src/hacktree.h delete mode 100644 src/hacktree.in create mode 100644 src/ht-builtin-init.c create mode 100644 src/ht-builtins.h create mode 100644 src/main.c diff --git a/Makefile-hacktree.am b/Makefile-hacktree.am index 3215ff68..d3f05f8e 100644 --- a/Makefile-hacktree.am +++ b/Makefile-hacktree.am @@ -1,9 +1,18 @@ -bin_SCRIPTS += hacktree +noinst_LTLIBRARIES += libhacktree.la -SUBSTITUTIONS = sed -e s,@libdir\@,$(libdir), -e s,@datarootdir\@,$(datarootdir), -e s,@PYTHON\@,$(PYTHON), +libhacktree_la_SOURCES = src/hacktree.h \ + src/hacktree-repo.c \ + src/hacktree-repo.h \ + src/hacktree-types.h \ + $(NULL) +libhacktree_la_CFLAGS = -I$(srcdir)/src -DLOCALEDIR=\"$(datadir)/locale\" $(GIO_UNIX_CFLAGS) +libhacktree_la_LIBADD = $(GIO_UNIX_LIBS) -hacktree: src/hacktree.in Makefile - $(AM_V_GEN) $(SUBSTITUTIONS) $< > $@.tmp && mv $@.tmp $@ - @chmod a+x $@ +bin_PROGRAMS += hacktree -CLEANFILES += hacktree +hacktree_SOURCES = src/main.c \ + src/ht-builtins.h \ + src/ht-builtin-init.c \ + $(NULL) +hacktree_CFLAGS = -I$(srcdir)/src -DLOCALEDIR=\"$(datadir)/locale\" $(GIO_UNIX_CFLAGS) +hacktree_LDADD = libhacktree.la $(GIO_UNIX_LIBS) diff --git a/Makefile.am b/Makefile.am index a2dc2690..9a09143b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,6 +2,7 @@ NULL = BUILT_SOURCES = CLEANFILES = EXTRA_DIST = +bin_PROGRAMS = bin_SCRIPTS = libexec_PROGRAMS = noinst_LTLIBRARIES = diff --git a/src/hacktree-repo.c b/src/hacktree-repo.c new file mode 100644 index 00000000..3a032f98 --- /dev/null +++ b/src/hacktree-repo.c @@ -0,0 +1,123 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters + * + * 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. + * + * Author: Colin Walters + */ + +#include "config.h" + +#include "hacktree-repo.h" + +enum { + PROP_0, + + PROP_PATH +}; + +G_DEFINE_TYPE (HacktreeRepo, hacktree_repo, G_TYPE_OBJECT) + +#define GET_PRIVATE(o) \ + (G_TYPE_INSTANCE_GET_PRIVATE ((o), HACKTREE_TYPE_REPO, HacktreeRepoPrivate)) + +typedef struct _HacktreeRepoPrivate HacktreeRepoPrivate; + +struct _HacktreeRepoPrivate { + char *path; +}; + +static void +hacktree_repo_finalize (GObject *object) +{ + G_OBJECT_CLASS (hacktree_repo_parent_class)->finalize (object); +} + +static void +hacktree_repo_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + HacktreeRepo *self = HACKTREE_REPO (object); + HacktreeRepoPrivate *priv = GET_PRIVATE (self); + + switch (prop_id) + { + case PROP_PATH: + priv->path = g_value_dup_string (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +hacktree_repo_get_property(GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + HacktreeRepo *self = HACKTREE_REPO (object); + HacktreeRepoPrivate *priv = GET_PRIVATE (self); + + switch (prop_id) + { + case PROP_PATH: + g_value_set_string (value, priv->path); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +hacktree_repo_class_init (HacktreeRepoClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (HacktreeRepoPrivate)); + + object_class->finalize = hacktree_repo_finalize; + + g_object_class_install_property (object_class, + PROP_PATH, + g_param_spec_string ("path", + "", + "", + NULL, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); +} + +static void +hacktree_repo_init (HacktreeRepo *self) +{ +} + +HacktreeRepo* +hacktree_repo_new (const char *path) +{ + return g_object_new (HACKTREE_TYPE_REPO, "path", path, NULL); +} + +gboolean +hacktree_repo_import (HacktreeRepo *repo, + const char *path) +{ + +} diff --git a/src/hacktree-repo.h b/src/hacktree-repo.h new file mode 100644 index 00000000..420dab4c --- /dev/null +++ b/src/hacktree-repo.h @@ -0,0 +1,59 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters + * + * 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. + * + * Author: Colin Walters + */ +/* hacktree-repo.h */ + +#ifndef _HACKTREE_REPO +#define _HACKTREE_REPO + +#include + +G_BEGIN_DECLS + +#define HACKTREE_TYPE_REPO hacktree_repo_get_type() +#define HACKTREE_REPO(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), HACKTREE_TYPE_REPO, HacktreeRepo)) +#define HACKTREE_REPO_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), HACKTREE_TYPE_REPO, HacktreeRepoClass)) +#define HACKTREE_IS_REPO(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), HACKTREE_TYPE_REPO)) +#define HACKTREE_IS_REPO_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), HACKTREE_TYPE_REPO)) +#define HACKTREE_REPO_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), HACKTREE_TYPE_REPO, HacktreeRepoClass)) + +typedef struct { + GObject parent; +} HacktreeRepo; + +typedef struct { + GObjectClass parent_class; +} HacktreeRepoClass; + +GType hacktree_repo_get_type (void); + +HacktreeRepo* hacktree_repo_new (const char *path); + +gboolean hacktree_repo_import (HacktreeRepo *repo, + const char *path); + +G_END_DECLS + +#endif /* _HACKTREE_REPO */ diff --git a/src/hacktree-types.h b/src/hacktree-types.h new file mode 100644 index 00000000..0824ad4c --- /dev/null +++ b/src/hacktree-types.h @@ -0,0 +1,33 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters . + * + * 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. + * + * Author: Colin Walters + */ + +#ifndef __HACKTREE_TYPES_H__ +#define __HACKTREE_TYPES_H__ + +#include + +G_BEGIN_DECLS + +#define HACKTREE_REPO_DIR ".ht" + +G_END_DECLS + +#endif diff --git a/src/hacktree.h b/src/hacktree.h new file mode 100644 index 00000000..11f0a89a --- /dev/null +++ b/src/hacktree.h @@ -0,0 +1,27 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters . + * + * 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. + * + * Author: Colin Walters + */ + +#ifndef __HACKTREE_H__ + +#include +#include + +#endif diff --git a/src/hacktree.in b/src/hacktree.in deleted file mode 100644 index fd43d118..00000000 --- a/src/hacktree.in +++ /dev/null @@ -1,31 +0,0 @@ -#!@PYTHON@ -# -*- Mode: Python -*- -# Copyright (C) 2011 Colin Walters -# -# 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., 51 Franklin Street, Fifth Floor, Boston, MA -# 02110-1301, USA. -# - -import os -import sys -import re - -import __builtin__ - -__builtin__.__dict__['DATADIR'] = "@datarootdir@" - -path = os.path.join('@libdir@', 'hacktree') -sys.path.insert(0, path) - diff --git a/src/ht-builtin-init.c b/src/ht-builtin-init.c new file mode 100644 index 00000000..18f031d0 --- /dev/null +++ b/src/ht-builtin-init.c @@ -0,0 +1,63 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters + * + * 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. + * + * Author: Colin Walters + */ + +#include "config.h" + +#include "ht-builtins.h" +#include "hacktree.h" + +#include + +static char *repo_path; +static GOptionEntry options[] = { + { "repo", 0, 0, G_OPTION_ARG_FILENAME, &repo_path, "Repository path", NULL }, + { NULL } +}; + +gboolean +hacktree_builtin_init (int argc, const char **argv, const char *prefix, GError **error) +{ + gboolean ret = FALSE; + char *htdir_path = NULL; + char *objects_path = NULL; + GFile *htdir = NULL; + GFile *objects_dir = NULL; + + if (repo_path == NULL) + repo_path = "."; + + htdir_path = g_build_filename (repo_path, HACKTREE_REPO_DIR, NULL); + htdir = g_file_new_for_path (htdir_path); + + if (!g_file_make_directory (htdir, NULL, error)) + goto out; + + objects_path = g_build_filename (htdir_path, "objects", NULL); + objects_dir = g_file_new_for_path (objects_path); + if (!g_file_make_directory (objects_dir, NULL, error)) + goto out; + + ret = TRUE; + out: + g_free (htdir_path); + g_clear_object (&htdir); + return ret; +} diff --git a/src/ht-builtins.h b/src/ht-builtins.h new file mode 100644 index 00000000..c40dc738 --- /dev/null +++ b/src/ht-builtins.h @@ -0,0 +1,43 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters + * + * 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. + * + * Author: Colin Walters + */ + +#ifndef __HACKTREE_BUILTINS__ +#define __HACKTREE_BUILTINS__ + +#include + +G_BEGIN_DECLS + +typedef enum { + HACKTREE_BUILTIN_FLAG_NONE = 0, +} HacktreeBuiltinFlags; + +typedef struct { + const char *name; + gboolean (*fn) (int argc, const char **argv, const char *prefix, GError **error); + int flags; /* HacktreeBuiltinFlags */ +} HacktreeBuiltin; + +gboolean hacktree_builtin_init (int argc, const char **argv, const char *prefix, GError **error); + +G_END_DECLS + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 00000000..1223bac2 --- /dev/null +++ b/src/main.c @@ -0,0 +1,94 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters + * + * 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. + * + * Author: Colin Walters + */ + +#include "config.h" + +#include +#include + +#include + +#include "ht-builtins.h" + +static HacktreeBuiltin builtins[] = { + { "init", hacktree_builtin_init, 0 }, + { NULL } +}; + +static int +usage (char **argv, gboolean is_error) +{ + HacktreeBuiltin *builtin = builtins; + void (*print_func) (const gchar *format, ...); + + if (is_error) + print_func = g_printerr; + else + print_func = g_print; + + print_func ("usage: %s COMMAND [options]\n", + argv[0]); + print_func ("Builtin commands:\n"); + + while (builtin->name) + { + print_func (" %s\n", builtin->name); + builtin++; + } + return (is_error ? 1 : 0); +} + + +int +main (int argc, + char **argv) +{ + HacktreeBuiltin *builtin; + const char *cmd; + + g_type_init (); + + builtin = builtins; + + if (argc < 2) + return usage (argv, 1); + + cmd = argv[1]; + + while (builtin->name) + { + GError *error = NULL; + if (strcmp (cmd, builtin->name) == 0) + { + if (!builtin->fn (argc - 2, (const char**)argv + 2, NULL, &error)) + { + g_printerr ("%s\n", error->message); + g_clear_error (&error); + return 1; + } + return 0; + } + builtin++; + } + + g_printerr ("Unknown command '%s'\n", cmd); + return usage (argv, 1); +}