core: Implement diff command

This commit is contained in:
Colin Walters 2011-11-16 17:48:29 -05:00
parent 073aa5973c
commit 9fb390664a
5 changed files with 459 additions and 21 deletions

View File

@ -30,8 +30,8 @@ G_BEGIN_DECLS
#define OSTREE_TYPE_REPO_FILE (_ostree_repo_file_get_type ())
#define OSTREE_REPO_FILE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), OSTREE_TYPE_REPO_FILE, OstreeRepoFile))
#define OSTREE_REPO_FILE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), OSTREE_TYPE_REPO_FILE, OstreeRepoFileClass))
#define OSTREE_IS_LOCAL_FILE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), OSTREE_TYPE_REPO_FILE))
#define OSTREE_IS_LOCAL_FILE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), OSTREE_TYPE_REPO_FILE))
#define OSTREE_IS_REPO_FILE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), OSTREE_TYPE_REPO_FILE))
#define OSTREE_IS_REPO_FILE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), OSTREE_TYPE_REPO_FILE))
#define OSTREE_REPO_FILE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), OSTREE_TYPE_REPO_FILE, OstreeRepoFileClass))
typedef struct _OstreeRepoFile OstreeRepoFile;

View File

@ -1846,9 +1846,350 @@ ostree_repo_checkout (OstreeRepo *self,
return ret;
}
static gboolean
get_file_checksum (GFile *f,
char **out_checksum,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
GChecksum *tmp_checksum = NULL;
char *ret_checksum = NULL;
struct stat stbuf;
if (OSTREE_IS_REPO_FILE (f))
{
ret_checksum = g_strdup (_ostree_repo_file_nontree_get_checksum ((OstreeRepoFile*)f));
}
else
{
if (!ostree_stat_and_checksum_file (-1, ot_gfile_get_path_cached (f),
OSTREE_OBJECT_TYPE_FILE,
&tmp_checksum, &stbuf, error))
goto out;
ret_checksum = g_strdup (g_checksum_get_string (tmp_checksum));
}
ret = TRUE;
*out_checksum = ret_checksum;
ret_checksum = NULL;
out:
g_free (ret_checksum);
if (tmp_checksum)
g_checksum_free (tmp_checksum);
return ret;
}
OstreeRepoDiffItem *
ostree_repo_diff_item_ref (OstreeRepoDiffItem *diffitem)
{
g_atomic_int_inc (&diffitem->refcount);
return diffitem;
}
void
ostree_repo_diff_item_unref (OstreeRepoDiffItem *diffitem)
{
if (!g_atomic_int_dec_and_test (&diffitem->refcount))
return;
g_clear_object (&diffitem->src);
g_clear_object (&diffitem->target);
g_clear_object (&diffitem->src_info);
g_clear_object (&diffitem->target_info);
g_free (diffitem->src_checksum);
g_free (diffitem->target_checksum);
g_free (diffitem);
}
static OstreeRepoDiffItem *
diff_item_new (GFile *a,
GFileInfo *a_info,
GFile *b,
GFileInfo *b_info,
char *checksum_a,
char *checksum_b)
{
OstreeRepoDiffItem *ret = g_new0 (OstreeRepoDiffItem, 1);
ret->refcount = 1;
ret->src = a ? g_object_ref (a) : NULL;
ret->src_info = a_info ? g_object_ref (a_info) : NULL;
ret->target = b ? g_object_ref (b) : NULL;
ret->target_info = b_info ? g_object_ref (b_info) : b_info;
ret->src_checksum = g_strdup (checksum_a);
ret->target_checksum = g_strdup (checksum_b);
return ret;
}
static gboolean
diff_files (GFile *a,
GFileInfo *a_info,
GFile *b,
GFileInfo *b_info,
OstreeRepoDiffItem **out_item,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
char *checksum_a = NULL;
char *checksum_b = NULL;
OstreeRepoDiffItem *ret_item = NULL;
if (!get_file_checksum (a, &checksum_a, cancellable, error))
goto out;
if (!get_file_checksum (b, &checksum_b, cancellable, error))
goto out;
if (strcmp (checksum_a, checksum_b) != 0)
{
ret_item = diff_item_new (a, a_info, b, b_info,
checksum_a, checksum_b);
}
ret = TRUE;
*out_item = ret_item;
ret_item = NULL;
out:
if (ret_item)
ostree_repo_diff_item_unref (ret_item);
g_free (checksum_a);
g_free (checksum_b);
return ret;
}
static gboolean
diff_add_dir_recurse (GFile *d,
GPtrArray *added,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
GFileEnumerator *dir_enum = NULL;
GError *temp_error = NULL;
GFile *child = NULL;
GFileInfo *child_info = NULL;
dir_enum = g_file_enumerate_children (d, OSTREE_GIO_FAST_QUERYINFO,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable,
error);
if (!dir_enum)
goto out;
while ((child_info = g_file_enumerator_next_file (dir_enum, cancellable, &temp_error)) != NULL)
{
const char *name;
name = g_file_info_get_name (child_info);
g_clear_object (&child);
child = g_file_get_child (d, name);
g_ptr_array_add (added, g_object_ref (child));
if (g_file_info_get_file_type (child_info) == G_FILE_TYPE_DIRECTORY)
{
if (!diff_add_dir_recurse (child, added, cancellable, error))
goto out;
}
g_clear_object (&child_info);
}
if (temp_error != NULL)
{
g_propagate_error (error, temp_error);
goto out;
}
ret = TRUE;
out:
g_clear_object (&child_info);
g_clear_object (&child);
g_clear_object (&dir_enum);
return ret;
}
static gboolean
diff_dirs (GFile *a,
GFile *b,
GPtrArray *modified,
GPtrArray *removed,
GPtrArray *added,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
GFileEnumerator *dir_enum = NULL;
GError *temp_error = NULL;
GFile *child_a = NULL;
GFile *child_b = NULL;
GFileInfo *child_a_info = NULL;
GFileInfo *child_b_info = NULL;
dir_enum = g_file_enumerate_children (a, OSTREE_GIO_FAST_QUERYINFO,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable,
error);
if (!dir_enum)
goto out;
while ((child_a_info = g_file_enumerator_next_file (dir_enum, cancellable, &temp_error)) != NULL)
{
const char *name;
GFileType child_a_type;
GFileType child_b_type;
name = g_file_info_get_name (child_a_info);
g_clear_object (&child_a);
child_a = g_file_get_child (a, name);
child_a_type = g_file_info_get_file_type (child_a_info);
g_clear_object (&child_b);
child_b = g_file_get_child (b, name);
g_clear_object (&child_b_info);
child_b_info = g_file_query_info (child_b, OSTREE_GIO_FAST_QUERYINFO,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable,
&temp_error);
if (!child_b_info)
{
if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{
g_clear_error (&temp_error);
g_ptr_array_add (removed, g_object_ref (child_a));
}
else
{
g_propagate_error (error, temp_error);
goto out;
}
}
else
{
child_b_type = g_file_info_get_file_type (child_b_info);
if (child_a_type != child_b_type)
{
g_ptr_array_add (modified, g_object_ref (child_a));
}
else if (child_a_type == G_FILE_TYPE_DIRECTORY)
{
if (!diff_dirs (child_a, child_b, modified,
removed, added, cancellable, error))
goto out;
}
else
{
OstreeRepoDiffItem *diff_item = NULL;
if (!diff_files (child_a, child_a_info, child_b, child_b_info, &diff_item, cancellable, error))
goto out;
if (diff_item)
g_ptr_array_add (modified, diff_item); /* Transfer ownership */
}
}
g_clear_object (&child_a_info);
}
if (temp_error != NULL)
{
g_propagate_error (error, temp_error);
goto out;
}
g_clear_object (&dir_enum);
dir_enum = g_file_enumerate_children (b, OSTREE_GIO_FAST_QUERYINFO,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable,
error);
if (!dir_enum)
goto out;
while ((child_b_info = g_file_enumerator_next_file (dir_enum, cancellable, &temp_error)) != NULL)
{
const char *name;
name = g_file_info_get_name (child_b_info);
g_clear_object (&child_a);
child_a = g_file_get_child (a, name);
g_clear_object (&child_b);
child_b = g_file_get_child (b, name);
g_clear_object (&child_a_info);
child_a_info = g_file_query_info (child_a, OSTREE_GIO_FAST_QUERYINFO,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable,
&temp_error);
if (!child_a_info)
{
if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{
g_clear_error (&temp_error);
g_ptr_array_add (added, g_object_ref (child_b));
if (g_file_info_get_file_type (child_b_info) == G_FILE_TYPE_DIRECTORY)
{
if (!diff_add_dir_recurse (child_b, added, cancellable, error))
goto out;
}
}
else
{
g_propagate_error (error, temp_error);
goto out;
}
}
}
if (temp_error != NULL)
{
g_propagate_error (error, temp_error);
goto out;
}
ret = TRUE;
out:
g_clear_object (&dir_enum);
g_clear_object (&child_a_info);
g_clear_object (&child_b_info);
g_clear_object (&child_a);
g_clear_object (&child_b);
return ret;
}
gboolean
ostree_repo_read_commit (OstreeRepo *self,
const char *rev,
GFile **out_root,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
GFile *ret_root = NULL;
char *resolved_rev = NULL;
if (!resolve_rev (self, rev, FALSE, &resolved_rev, error))
goto out;
ret_root = _ostree_repo_file_new_root (self, resolved_rev);
if (!_ostree_repo_file_ensure_resolved ((OstreeRepoFile*)ret_root, error))
goto out;
ret = TRUE;
*out_root = ret_root;
ret_root = NULL;
out:
g_free (resolved_rev);
g_clear_object (&ret_root);
return ret;
}
gboolean
ostree_repo_diff (OstreeRepo *self,
const char *ref,
GFile *src,
GFile *target,
GPtrArray **out_modified,
GPtrArray **out_removed,
@ -1861,11 +2202,20 @@ ostree_repo_diff (OstreeRepo *self,
GPtrArray *ret_removed = NULL;
GPtrArray *ret_added = NULL;
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Not implemented yet");
goto out;
ret_modified = g_ptr_array_new_with_free_func ((GDestroyNotify)ostree_repo_diff_item_unref);
ret_removed = g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref);
ret_added = g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref);
if (!diff_dirs (src, target, ret_modified, ret_removed, ret_added, cancellable, error))
goto out;
ret = TRUE;
*out_modified = ret_modified;
ret_modified = NULL;
*out_removed = ret_removed;
ret_removed = NULL;
*out_added = ret_added;
ret_added = NULL;
out:
if (ret_modified)
g_ptr_array_free (ret_modified, TRUE);

View File

@ -125,23 +125,30 @@ gboolean ostree_repo_checkout (OstreeRepo *self,
GCancellable *cancellable,
GError **error);
gboolean ostree_repo_read_commit (OstreeRepo *self,
const char *rev,
GFile **out_root,
GCancellable *cancellable,
GError **error);
typedef struct {
guint content_differs : 1;
guint xattrs_differs : 1;
guint unused : 30;
volatile gint refcount;
GFile *src;
GFile *target;
GFileInfo *src_info;
GFileInfo *target_info;
char *src_file_checksum;
char *target_file_checksum;
GVariant *src_xattrs;
GVariant *target_xattrs;
char *src_checksum;
char *target_checksum;
} OstreeRepoDiffItem;
OstreeRepoDiffItem *ostree_repo_diff_item_ref (OstreeRepoDiffItem *diffitem);
void ostree_repo_diff_item_unref (OstreeRepoDiffItem *diffitem);
gboolean ostree_repo_diff (OstreeRepo *self,
const char *ref,
GFile *src,
GFile *target,
GPtrArray **out_modified, /* OstreeRepoDiffItem */
GPtrArray **out_removed, /* OstreeRepoDiffItem */

View File

@ -31,18 +31,50 @@ static GOptionEntry options[] = {
{ NULL }
};
static gboolean
parse_file_or_commit (OstreeRepo *repo,
const char *arg,
GFile **out_file,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
GFile *ret_file = NULL;
if (g_str_has_prefix (arg, "/")
|| g_str_has_prefix (arg, "./"))
{
ret_file = ot_util_new_file_for_path (arg);
}
else
{
if (!ostree_repo_read_commit (repo, arg, &ret_file, cancellable, NULL))
goto out;
}
ret = TRUE;
*out_file = ret_file;
ret_file = NULL;
out:
g_clear_object (&ret_file);
return ret;
}
gboolean
ostree_builtin_diff (int argc, char **argv, const char *repo_path, GError **error)
{
GOptionContext *context;
gboolean ret = FALSE;
OstreeRepo *repo = NULL;
const char *src;
const char *target;
const char *rev;
GFile *srcf = NULL;
GFile *targetf = NULL;
GFile *cwd = NULL;
GPtrArray *modified = NULL;
GPtrArray *removed = NULL;
GPtrArray *added = NULL;
int i;
context = g_option_context_new ("REV TARGETDIR - Compare directory TARGETDIR against revision REV");
g_option_context_add_main_entries (context, options, NULL);
@ -64,16 +96,47 @@ ostree_builtin_diff (int argc, char **argv, const char *repo_path, GError **erro
goto out;
}
rev = argv[1];
src = argv[1];
target = argv[2];
targetf = ot_util_new_file_for_path (target);
if (!ostree_repo_diff (repo, rev, targetf, &modified, &removed, &added, NULL, error))
cwd = ot_util_new_file_for_path (".");
if (!parse_file_or_commit (repo, src, &srcf, NULL, error))
goto out;
if (!parse_file_or_commit (repo, target, &targetf, NULL, error))
goto out;
if (!ostree_repo_diff (repo, srcf, targetf, &modified, &removed, &added, NULL, error))
goto out;
for (i = 0; i < modified->len; i++)
{
OstreeRepoDiffItem *diff = modified->pdata[i];
g_print ("M %s\n", ot_gfile_get_path_cached (diff->src));
}
for (i = 0; i < removed->len; i++)
{
g_print ("D %s\n", ot_gfile_get_path_cached (removed->pdata[i]));
}
for (i = 0; i < added->len; i++)
{
GFile *added_f = added->pdata[i];
if (g_file_is_native (added_f))
{
char *relpath = g_file_get_relative_path (cwd, added_f);
g_assert (relpath != NULL);
g_print ("A %s\n", relpath);
g_free (relpath);
}
else
g_print ("A %s\n", ot_gfile_get_path_cached (added_f));
}
ret = TRUE;
out:
g_clear_object (&repo);
g_clear_object (&cwd);
g_clear_object (&srcf);
g_clear_object (&targetf);
if (modified)
g_ptr_array_free (modified, TRUE);

View File

@ -19,7 +19,7 @@
set -e
echo "1..12"
echo "1..13"
. libtest.sh
@ -83,6 +83,7 @@ echo 4 > four
mkdir -p yet/another/tree
echo leaf > yet/another/tree/green
echo helloworld > yet/message
rm a/5
$OSTREE commit -b test2 -s "Current directory"
echo "ok cwd commit"
@ -93,6 +94,22 @@ assert_file_has_content yet/another/tree/green 'leaf'
assert_file_has_content four '4'
echo "ok cwd contents"
cd ${test_tmpdir}
$OSTREE diff test2^ test2 > diff-test2
assert_file_has_content diff-test2 'D */a/5'
assert_file_has_content diff-test2 'A */yet$'
assert_file_has_content diff-test2 'A */yet/message$'
assert_file_has_content diff-test2 'A */yet/another/tree/green$'
echo "ok diff revisions"
cd ${test_tmpdir}/checkout-test2-4
echo afile > oh-look-a-file
$OSTREE diff test2 ./ > ${test_tmpdir}/diff-test2-2
rm oh-look-a-file
cd ${test_tmpdir}
assert_file_has_content diff-test2-2 'A */oh-look-a-file$'
echo "ok diff cwd"
cd ${test_tmpdir}/checkout-test2-4
echo afile > oh-look-a-file
cat > ${test_tmpdir}/ostree-commit-metadata <<EOF
@ -107,3 +124,4 @@ $OSTREE show test2 > ${test_tmpdir}/show
assert_file_has_content ${test_tmpdir}/show 'example.com'
assert_file_has_content ${test_tmpdir}/show 'buildid'
echo "ok metadata content"