Add probably working commits

This commit is contained in:
Colin Walters 2011-10-13 17:11:01 -04:00
parent 5e0d58dd72
commit 98a043c671
16 changed files with 1394 additions and 42 deletions

View File

@ -46,6 +46,7 @@ bin_PROGRAMS += hacktree
hacktree_SOURCES = src/main.c \
src/ht-builtins.h \
src/ht-builtin-commit.c \
src/ht-builtin-init.c \
src/ht-builtin-link-file.c \
src/ht-builtin-fsck.c \

4
TODO
View File

@ -1,5 +1,5 @@
* hacktree import-tar
- Accepts tarball on stdin, stores the objects
* tree and commit objects
* hacktree import
* hacktree import-tar
- Accepts tarball on stdin, stores the objects
* branches

View File

@ -11,6 +11,19 @@ AM_SILENT_RULES([yes])
AC_PROG_CC
AM_PROG_CC_C_O
changequote(,)dnl
if test "x$GCC" = "xyes"; then
case " $CFLAGS " in
*[\ \ ]-Wall[\ \ ]*) ;;
*) CFLAGS="$CFLAGS -Wall" ;;
esac
case " $CFLAGS " in
*[\ \ ]-Wmissing-prototypes[\ \ ]*) ;;
*) CFLAGS="$CFLAGS -Wmissing-prototypes" ;;
esac
fi
changequote([,])dnl
# Initialize libtool
LT_PREREQ([2.2])
LT_INIT

115
src/ht-builtin-commit.c Normal file
View File

@ -0,0 +1,115 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2011 Colin Walters <walters@verbum.org>
*
* 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 <walters@verbum.org>
*/
#include "config.h"
#include "ht-builtins.h"
#include "hacktree.h"
#include <glib/gi18n.h>
static char *repo_path;
static char *subject;
static char *body;
static char **additions;
static char **removals;
static GOptionEntry options[] = {
{ "repo", 0, 0, G_OPTION_ARG_FILENAME, &repo_path, "Repository path", "repo" },
{ "subject", 's', 0, G_OPTION_ARG_STRING, &subject, "One line subject", "subject" },
{ "body", 'b', 0, G_OPTION_ARG_STRING, &body, "Full description", "body" },
{ "add", 'a', 0, G_OPTION_ARG_FILENAME_ARRAY, &additions, "Relative file path to add", "filename" },
{ "remove", 'r', 0, G_OPTION_ARG_FILENAME_ARRAY, &removals, "Relative file path to remove", "filename" },
{ NULL }
};
gboolean
hacktree_builtin_commit (int argc, char **argv, const char *prefix, GError **error)
{
GOptionContext *context;
gboolean ret = FALSE;
HacktreeRepo *repo = NULL;
GPtrArray *additions_array = NULL;
GPtrArray *removals_array = NULL;
GChecksum *commit_checksum = NULL;
char **iter;
context = g_option_context_new ("- Commit a new revision");
g_option_context_add_main_entries (context, options, NULL);
if (!g_option_context_parse (context, &argc, &argv, error))
goto out;
if (repo_path == NULL)
repo_path = ".";
if (prefix == NULL)
prefix = ".";
repo = hacktree_repo_new (repo_path);
if (!hacktree_repo_check (repo, error))
goto out;
if (!(removals || additions))
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"No additions or removals specified");
goto out;
}
if (!subject)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"A subject must be specified with --subject");
goto out;
}
additions_array = g_ptr_array_new ();
removals_array = g_ptr_array_new ();
if (additions)
for (iter = additions; *iter; iter++)
g_ptr_array_add (additions_array, *iter);
if (removals)
for (iter = removals; *iter; iter++)
g_ptr_array_add (removals_array, *iter);
if (!hacktree_repo_commit (repo, subject, body, NULL,
prefix, additions_array,
removals_array,
&commit_checksum,
error))
goto out;
ret = TRUE;
g_print ("%s\n", g_checksum_get_string (commit_checksum));
out:
if (context)
g_option_context_free (context);
g_clear_object (&repo);
if (removals_array)
g_ptr_array_free (removals_array, TRUE);
if (additions_array)
g_ptr_array_free (additions_array, TRUE);
if (commit_checksum)
g_checksum_free (commit_checksum);
return ret;
}

View File

@ -94,7 +94,6 @@ hacktree_builtin_fsck (int argc, char **argv, const char *prefix, GError **error
HtFsckData data;
gboolean ret = FALSE;
HacktreeRepo *repo = NULL;
int i;
context = g_option_context_new ("- Check the repository for consistency");
g_option_context_add_main_entries (context, options, NULL);

View File

@ -36,6 +36,7 @@ typedef struct {
int flags; /* HacktreeBuiltinFlags */
} HacktreeBuiltin;
gboolean hacktree_builtin_commit (int argc, char **argv, const char *prefix, GError **error);
gboolean hacktree_builtin_init (int argc, char **argv, const char *prefix, GError **error);
gboolean hacktree_builtin_link_file (int argc, char **argv, const char *prefix, GError **error);
gboolean hacktree_builtin_fsck (int argc, char **argv, const char *prefix, GError **error);

View File

@ -24,10 +24,16 @@
#include "hacktree.h"
#include "htutil.h"
char *
#include <sys/types.h>
#include <attr/xattr.h>
static char *
stat_to_string (struct stat *stbuf)
{
return g_strdup_printf ("%d:%d:%d", stbuf->st_mode, stbuf->st_uid, stbuf->st_gid);
return g_strdup_printf ("%u:%u:%u",
(guint32)(stbuf->st_mode & ~S_IFMT),
(guint32)stbuf->st_uid,
(guint32)stbuf->st_gid);
}
static char *
@ -55,6 +61,52 @@ canonicalize_xattrs (char *xattr_string, size_t len)
return g_string_free (result, FALSE);
}
gboolean
hacktree_get_xattrs_for_directory (const char *path,
char **out_xattrs, /* out */
gsize *out_len, /* out */
GError **error)
{
gboolean ret = FALSE;
char *xattrs = NULL;
ssize_t bytes_read;
bytes_read = llistxattr (path, NULL, 0);
if (bytes_read < 0)
{
if (errno != ENOTSUP)
{
ht_util_set_error_from_errno (error, errno);
goto out;
}
}
else if (bytes_read > 0)
{
xattrs = g_malloc (bytes_read);
if (!llistxattr (path, xattrs, bytes_read))
{
ht_util_set_error_from_errno (error, errno);
g_free (xattrs);
xattrs = NULL;
goto out;
}
*out_xattrs = canonicalize_xattrs (xattrs, bytes_read);
*out_len = (gsize)bytes_read;
}
else
{
*out_xattrs = NULL;
*out_len = 0;
}
ret = TRUE;
out:
g_free (xattrs);
return ret;
}
gboolean
hacktree_stat_and_checksum_file (int dir_fd, const char *path,
GChecksum **out_checksum,
@ -73,6 +125,7 @@ hacktree_stat_and_checksum_file (int dir_fd, const char *path,
gboolean ret = FALSE;
char *symlink_target = NULL;
char *device_id = NULL;
struct stat stbuf;
basename = g_path_get_basename (path);
@ -89,13 +142,13 @@ hacktree_stat_and_checksum_file (int dir_fd, const char *path,
dir_fd = dirfd (temp_dir);
}
if (fstatat (dir_fd, basename, out_stbuf, AT_SYMLINK_NOFOLLOW) < 0)
if (fstatat (dir_fd, basename, &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
{
ht_util_set_error_from_errno (error, errno);
goto out;
}
if (!S_ISLNK(out_stbuf->st_mode))
if (!S_ISLNK(stbuf.st_mode))
{
fd = ht_util_open_file_read_at (dir_fd, basename, error);
if (fd < 0)
@ -105,10 +158,10 @@ hacktree_stat_and_checksum_file (int dir_fd, const char *path,
}
}
stat_string = stat_to_string (out_stbuf);
stat_string = stat_to_string (&stbuf);
/* FIXME - Add llistxattrat */
if (!S_ISLNK(out_stbuf->st_mode))
if (!S_ISLNK(stbuf.st_mode))
bytes_read = flistxattr (fd, NULL, 0);
else
bytes_read = llistxattr (path, NULL, 0);
@ -121,12 +174,12 @@ hacktree_stat_and_checksum_file (int dir_fd, const char *path,
goto out;
}
}
if (errno != ENOTSUP)
else if (bytes_read > 0)
{
gboolean tmp;
xattrs = g_malloc (bytes_read);
/* FIXME - Add llistxattrat */
if (!S_ISLNK(out_stbuf->st_mode))
if (!S_ISLNK(stbuf.st_mode))
tmp = flistxattr (fd, xattrs, bytes_read);
else
tmp = llistxattr (path, xattrs, bytes_read);
@ -142,7 +195,7 @@ hacktree_stat_and_checksum_file (int dir_fd, const char *path,
content_sha256 = g_checksum_new (G_CHECKSUM_SHA256);
if (S_ISREG(out_stbuf->st_mode))
if (S_ISREG(stbuf.st_mode))
{
guint8 buf[8192];
@ -154,7 +207,7 @@ hacktree_stat_and_checksum_file (int dir_fd, const char *path,
goto out;
}
}
else if (S_ISLNK(out_stbuf->st_mode))
else if (S_ISLNK(stbuf.st_mode))
{
symlink_target = g_malloc (PATH_MAX);
@ -163,12 +216,12 @@ hacktree_stat_and_checksum_file (int dir_fd, const char *path,
ht_util_set_error_from_errno (error, errno);
goto out;
}
g_checksum_update (content_sha256, symlink_target, strlen (symlink_target));
g_checksum_update (content_sha256, (guint8*)symlink_target, strlen (symlink_target));
}
else if (S_ISCHR(out_stbuf->st_mode) || S_ISBLK(out_stbuf->st_mode))
else if (S_ISCHR(stbuf.st_mode) || S_ISBLK(stbuf.st_mode))
{
device_id = g_strdup_printf ("%d", out_stbuf->st_rdev);
g_checksum_update (content_sha256, device_id, strlen (device_id));
device_id = g_strdup_printf ("%u", (guint)stbuf.st_rdev);
g_checksum_update (content_sha256, (guint8*)device_id, strlen (device_id));
}
else
{
@ -181,9 +234,10 @@ hacktree_stat_and_checksum_file (int dir_fd, const char *path,
content_and_meta_sha256 = g_checksum_copy (content_sha256);
g_checksum_update (content_and_meta_sha256, stat_string, strlen (stat_string));
g_checksum_update (content_and_meta_sha256, xattrs_canonicalized, strlen (stat_string));
g_checksum_update (content_and_meta_sha256, (guint8*)stat_string, strlen (stat_string));
g_checksum_update (content_and_meta_sha256, (guint8*)xattrs_canonicalized, strlen (stat_string));
*out_stbuf = stbuf;
*out_checksum = content_and_meta_sha256;
ret = TRUE;
out:

View File

@ -18,7 +18,6 @@
*
* Author: Colin Walters <walters@verbum.org>
*/
/* hacktree-repo.h */
#ifndef _HACKTREE_CORE
#define _HACKTREE_CORE
@ -27,6 +26,55 @@
G_BEGIN_DECLS
#define HACKTREE_EMPTY_STRING_SHA256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
typedef enum {
HACKTREE_SERIALIZED_TREE_VARIANT = 1,
HACKTREE_SERIALIZED_COMMIT_VARIANT = 2,
HACKTREE_SERIALIZED_DIRMETA_VARIANT = 3
} HacktreeSerializedVariantType;
#define HACKTREE_SERIALIZED_VARIANT_FORMAT "(uv)"
#define HACKTREE_DIR_META_VERSION 0
/*
* dirmeta objects:
* u - Version
* u - uid
* u - gid
* u - mode
* ay - xattrs
*/
#define HACKTREE_DIRMETA_GVARIANT_FORMAT "(uuuuay)"
#define HACKTREE_TREE_VERSION 0
/*
* Tree objects:
* u - Version
* a{sv} - Metadata
* a(ss) - array of (filename, checksum) for files
* a(sss) - array of (dirname, tree_checksum, meta_checksum) for directories
*/
#define HACKTREE_TREE_GVARIANT_FORMAT "(ua{sv}a(ss)a(sss)"
#define HACKTREE_COMMIT_VERSION 0
/*
* Commit objects:
* u - Version
* a{sv} - Metadata
* s - parent checksum
* s - subject
* s - body
* t - Timestamp in seconds since the epoch (UTC)
* s - Tree SHA256
*/
#define HACKTREE_COMMIT_GVARIANT_FORMAT "(ua{sv}sssts)"
gboolean hacktree_get_xattrs_for_directory (const char *path,
char **out_xattrs,
gsize *out_len,
GError **error);
gboolean hacktree_stat_and_checksum_file (int dirfd, const char *path,
GChecksum **out_checksum,
struct stat *out_stbuf,

File diff suppressed because it is too large Load Diff

View File

@ -59,6 +59,16 @@ gboolean hacktree_repo_link_file (HacktreeRepo *repo,
gboolean force,
GError **error);
gboolean hacktree_repo_commit (HacktreeRepo *repo,
const char *subject,
const char *body,
GVariant *metadata,
const char *base,
GPtrArray *modified_files,
GPtrArray *removed_files,
GChecksum **out_commit,
GError **error);
typedef void (*HacktreeRepoObjectIter) (HacktreeRepo *repo, const char *path,
GFileInfo *fileinfo, gpointer user_data);

View File

@ -23,10 +23,11 @@
#include <glib-unix.h>
#include <gio/gio.h>
#include <gio/gunixinputstream.h>
#include <string.h>
#include "ht-gio-utils.h"
#include "htutil.h"
gboolean
ht_util_ensure_directory (const char *path, gboolean with_parents, GError **error)
@ -56,3 +57,51 @@ ht_util_ensure_directory (const char *path, gboolean with_parents, GError **erro
g_clear_object (&dir);
return ret;
}
char *
ht_util_get_file_contents_utf8 (const char *path,
GError **error)
{
char *contents;
gsize len;
if (!g_file_get_contents (path, &contents, &len, error))
return NULL;
if (!g_utf8_validate (contents, len, NULL))
{
g_free (contents);
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_FAILED,
"File %s contains invalid UTF-8",
path);
return NULL;
}
return contents;
}
GInputStream *
ht_util_read_file_noatime (GFile *file, GCancellable *cancellable, GError **error)
{
GInputStream *ret = NULL;
int fd;
int flags = O_RDONLY;
char *path = NULL;
path = g_file_get_path (file);
#ifdef O_NOATIME
flags |= O_NOATIME;
#endif
fd = open (path, flags);
if (fd < 0)
{
ht_util_set_error_from_errno (error, errno);
goto out;
}
ret = (GInputStream*)g_unix_input_stream_new (fd, TRUE);
out:
g_free (path);
return ret;
}

View File

@ -19,8 +19,8 @@
* Author: Colin Walters <walters@verbum.org>
*/
#ifndef __HACKTREE_UNIX_UTILS_H__
#define __HACKTREE_UNIX_UTILS_H__
#ifndef __HACKTREE_GIO_UTILS_H__
#define __HACKTREE_GIO_UTILS_H__
#include <gio/gio.h>
@ -28,6 +28,10 @@ G_BEGIN_DECLS
gboolean ht_util_ensure_directory (const char *path, gboolean with_parents, GError **error);
char * ht_util_get_file_contents_utf8 (const char *path, GError **error);
GInputStream *ht_util_read_file_noatime (GFile *file, GCancellable *cancellable, GError **error);
G_END_DECLS
#endif

View File

@ -30,6 +30,125 @@
#include <sys/types.h>
#include <dirent.h>
static int
compare_filenames_by_component_length (const char *a,
const char *b)
{
char *a_slash, *b_slash;
a_slash = strchr (a, '/');
b_slash = strchr (b, '/');
while (a_slash && b_slash)
{
a = a_slash + 1;
b = b_slash + 1;
a_slash = strchr (a, '/');
b_slash = strchr (b, '/');
}
if (a_slash)
return -1;
else if (b_slash)
return 1;
else
return 0;
}
GPtrArray *
ht_util_sort_filenames_by_component_length (GPtrArray *files)
{
GPtrArray *array = g_ptr_array_sized_new (files->len);
memcpy (array->pdata, files->pdata, sizeof (gpointer) * files->len);
g_ptr_array_sort (array, (GCompareFunc) compare_filenames_by_component_length);
return array;
}
int
ht_util_count_filename_components (const char *path)
{
int i = 0;
while (path)
{
i++;
path = strchr (path, '/');
if (path)
path++;
}
return i;
}
gboolean
ht_util_filename_has_dotdot (const char *path)
{
char *p;
char last;
if (strcmp (path, "..") == 0)
return TRUE;
if (g_str_has_prefix (path, "../"))
return TRUE;
p = strstr (path, "/..");
if (!p)
return FALSE;
last = *(p + 1);
return last == '\0' || last == '/';
}
GPtrArray *
ht_util_path_split (const char *path)
{
GPtrArray *ret = NULL;
const char *p;
const char *slash;
g_return_val_if_fail (path[0] != '/', NULL);
ret = g_ptr_array_new ();
g_ptr_array_set_free_func (ret, g_free);
p = path;
do {
slash = strchr (p, '/');
if (!slash)
{
g_ptr_array_add (ret, g_strdup (p));
p = NULL;
}
else
{
g_ptr_array_add (ret, g_strndup (p, slash - p));
p += 1;
}
} while (p);
return ret;
}
char *
ht_util_path_join_n (const char *base, GPtrArray *components, int n)
{
int max = MAX(n+1, components->len);
GPtrArray *subcomponents;
char *path;
int i;
subcomponents = g_ptr_array_new ();
if (base != NULL)
g_ptr_array_add (subcomponents, (char*)base);
for (i = 0; i < max; i++)
{
g_ptr_array_add (subcomponents, components->pdata[i]);
}
g_ptr_array_add (subcomponents, NULL);
path = g_build_filenamev ((char**)subcomponents->pdata);
g_ptr_array_free (subcomponents, TRUE);
return path;
}
void
ht_util_set_error_from_errno (GError **error,
gint saved_errno)

View File

@ -31,9 +31,21 @@
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
G_BEGIN_DECLS
gboolean ht_util_filename_has_dotdot (const char *path);
GPtrArray *ht_util_sort_filenames_by_component_length (GPtrArray *files);
GPtrArray* ht_util_path_split (const char *path);
char *ht_util_path_join_n (const char *base, GPtrArray *components, int n);
int ht_util_count_filename_components (const char *path);
int ht_util_open_file_read (const char *path, GError **error);
int ht_util_open_file_read_at (int dirfd, const char *name, GError **error);

View File

@ -30,6 +30,7 @@
static HacktreeBuiltin builtins[] = {
{ "init", hacktree_builtin_init, 0 },
{ "commit", hacktree_builtin_commit, 0 },
{ "link-file", hacktree_builtin_link_file, 0 },
{ "fsck", hacktree_builtin_fsck, 0 },
{ NULL }

38
tests/t0002-commit-one.sh Executable file
View File

@ -0,0 +1,38 @@
#!/bin/bash
#
# Copyright (C) 2011 Colin Walters <walters@verbum.org>
#
# 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 <walters@verbum.org>
set -e
. libtest.sh
echo '1..3'
mkdir files
cd files
echo hello > yy
mkdir ../repo
repo="--repo=../repo"
hacktree init $repo
echo 'ok init'
hacktree commit $repo -s "Test Commit" -b "Commit body" --add=yy
echo 'ok commit'
hacktree fsck -q $repo
echo 'ok fsck'