gnu-arch-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnu-arch-users] [PATCH] tla revert


From: Momchil Velikov
Subject: [Gnu-arch-users] [PATCH] tla revert
Date: 07 Sep 2003 03:14:00 +0300
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

  This patch adds the ``revert'' command - undo local changes to a file
and restore it to a previous revision.  The implementaion is heavily
copied from ``file-diffs.''

~velco
diff -rudpN -x {arch} -x .arch-ids -x ',,*' 
tla--revert--1.1--base-0/libarch/cmd-revert.c 
tla--revert--1.1--patch-1/libarch/cmd-revert.c
--- tla--revert--1.1--base-0/libarch/cmd-revert.c       1970-01-01 
02:00:00.000000000 +0200
+++ tla--revert--1.1--patch-1/libarch/cmd-revert.c      2003-09-07 
03:09:26.000000000 +0300
@@ -0,0 +1,228 @@
+/* cmd-revert.c
+ *
+ ****************************************************************
+ * Copyright (C) 2003 Tom Lord
+ *
+ * See the file "COPYING" for further information about
+ * the copyright and warranty status of this work.
+ */
+
+
+#include "config-options.h"
+#include "hackerlab/cmd/main.h"
+#include "hackerlab/fs/file-names.h"
+#include "tla/libfsutils/dir-as-cwd.h"
+#include "tla/libarch/my.h"
+#include "tla/libarch/libraries.h"
+#include "tla/libarch/project-tree.h"
+#include "tla/libarch/patch-logs.h"
+#include "tla/libarch/inv-tags.h"
+#include "tla/libarch/revert.h"
+#include "tla/libarch/namespace.h"
+#include "tla/libfsutils/dir-as-cwd.h"
+#include "tla/libarch/cmd.h"
+#include "tla/libarch/cmd-revert.h"
+
+
+
+static t_uchar * usage = "[options] file [revision]";
+static t_uchar * version_string = (cfg__std__package " from regexps.com\n"
+                                   "\n"
+                                   "Copyright 2003 Tom Lord\n"
+                                   "\n"
+                                   "This is free software; see the source for 
copying conditions.\n"
+                                   "There is NO warranty; not even for 
MERCHANTABILITY or FITNESS FOR A\n"
+                                   "PARTICULAR PURPOSE.\n"
+                                   "\n"
+                                   "Report bugs to " cfg__tla_bug_mail ".\n"
+                                   "\n");
+
+#define OPTS(OP, OP2) \
+  OP (opt_help_msg, "h", "help", 0, \
+      "Display a help message and exit.") \
+  OP (opt_long_help, "H", 0, 0, \
+      "Display a verbose help message and exit.") \
+  OP (opt_version, "V", "version", 0, \
+      "Display a release identifier string") \
+  OP2 (opt_version, 0, 0, 0, "and exit.") \
+  OP (opt_archive, "A", "archive", 1, \
+      "Override `my-default-archive'")
+
+t_uchar arch_cmd_revert_help[] = ("revert local changes to a file\n"
+                                 "Revert local changes to FILE\n");
+
+enum options
+{
+  OPTS (OPT_ENUM, OPT_IGN)
+};
+
+static struct opt_desc opts[] =
+{
+  OPTS (OPT_DESC, OPT_DESC)
+    {-1, 0, 0, 0, 0}
+};
+
+
+
+int
+arch_cmd_revert (t_uchar * program_name, int argc, char * argv[])
+{
+  int o;
+  struct opt_parsed * option;
+  t_uchar * default_archive;
+  int status = 2;
+
+  default_archive = 0;
+
+  safe_buffer_fd (1, 0, O_WRONLY, 0);
+
+  option = 0;
+
+  while (1)
+    {
+      o = opt_standard (lim_use_must_malloc, &option, opts, &argc, argv, 
program_name, usage, version_string, arch_cmd_revert_help, opt_help_msg, 
opt_long_help, opt_version);
+      if (o == opt_none)
+        break;
+      switch (o)
+        {
+        default:
+          safe_printfmt (2, "unhandled option `%s'\n", option->opt_string);
+          panic ("internal error parsing arguments");
+
+        usage_error:
+          opt_usage (2, argv[0], program_name, usage, 1);
+          exit (1);
+
+          /* bogus_arg: */
+          safe_printfmt (2, "ill-formed argument for `%s' (`%s')\n", 
option->opt_string, option->arg_string);
+          goto usage_error;
+
+        case opt_archive:
+          {
+            default_archive = str_save (0, option->arg_string);
+            break;
+          }
+        }
+    }
+
+  if ((argc < 2) || (argc > 3))
+    goto usage_error;
+
+  if (default_archive && !arch_valid_archive_name (default_archive))
+    {
+      safe_printfmt (2, "%s: invalid archive name (%s)\n",
+                    argv[0], default_archive);
+      exit (1);
+    }
+
+  {
+    t_uchar * filespec;
+    t_uchar * filedir = 0;
+    t_uchar * filedir_path = 0;
+    t_uchar * filedir_loc = 0;
+    t_uchar * file_tail = 0;
+    t_uchar * mod_loc = 0;
+    t_uchar * tree_root = 0;
+    t_uchar * revspec = 0;
+    t_uchar * archive = 0;
+    t_uchar * revision = 0;
+
+    filespec = argv[1];
+
+    filedir = file_name_directory_file (0, filespec);
+    if (!filedir)
+      filedir = str_save (0, ".");
+
+    filedir_path = directory_as_cwd (filedir);
+
+    tree_root = arch_tree_root (0, filedir, 0);
+    if (!tree_root)
+      {
+        safe_printfmt (2, "%s: file is not in a project tree (%s)\n",
+                      argv[0], filespec);
+        exit (2);
+      }
+
+    invariant (!str_cmp_prefix (tree_root, filedir_path)
+               && (!filedir_path[str_length (tree_root)] || ('/' == 
filedir_path[str_length (tree_root)])));
+
+    filedir_loc = str_alloc_cat (0, "./", filedir_path + str_length 
(tree_root) + 1);
+    file_tail = file_name_tail (0, filespec);
+    mod_loc = file_name_in_vicinity (0, filedir_loc, file_tail);
+
+
+    if (argc == 3)
+      {
+        revspec = str_save (0, argv[2]);
+      }
+    else
+      {
+        revspec = arch_tree_version (tree_root);
+        if (!revspec)
+          {
+            safe_printfmt (2, "%s: tree has no default version (%s)\n",
+                          argv[0], tree_root);
+            exit (2);
+          }
+      }
+
+    if (!arch_valid_package_name (revspec, arch_maybe_archive, 
arch_req_version, 1))
+      {
+        safe_printfmt (2, "%s: invalid revision specification (%s)\n",
+                      argv[0], revspec);
+        exit (2);
+      }
+
+
+    archive = arch_parse_package_name (arch_ret_archive, default_archive, 
revspec);
+    if (!archive)
+      {
+        safe_printfmt (2, "%s: no archive specified for revert\n",
+                      argv[0]);
+        exit (2);
+      }
+
+
+    if (arch_valid_package_name (revspec, arch_maybe_archive, 
arch_req_version, 0))
+      {
+        t_uchar * version = 0;
+        t_uchar * level = 0;
+
+        version = arch_parse_package_name (arch_ret_package_version, 0, 
revspec);
+        level  = arch_highest_patch_level (tree_root, archive, version);
+        revision = str_alloc_cat_many (0, version, "--", level, str_end);
+
+        lim_free (0, level);
+        lim_free (0, version);
+      }
+    else
+      {
+        revision = arch_parse_package_name (arch_ret_non_archive, 0, revspec);
+      }
+
+
+    status = arch_revert (1, tree_root, mod_loc, archive, revision);
+
+
+    lim_free (0, filedir);
+    lim_free (0, filedir_path);
+    lim_free (0, filedir_loc);
+    lim_free (0, file_tail);
+    lim_free (0, mod_loc);
+    lim_free (0, tree_root);
+    lim_free (0, revspec);
+    lim_free (0, archive);
+    lim_free (0, revision);
+  }
+
+  lim_free (0, default_archive);
+
+  exit (status);
+  return status;
+}
+
+
+
+
+/* tag: Tom Lord Fri May 30 21:37:57 2003 (cmd-revert.c)
+ */
diff -rudpN -x {arch} -x .arch-ids -x ',,*' 
tla--revert--1.1--base-0/libarch/cmd-revert.h 
tla--revert--1.1--patch-1/libarch/cmd-revert.h
--- tla--revert--1.1--base-0/libarch/cmd-revert.h       1970-01-01 
02:00:00.000000000 +0200
+++ tla--revert--1.1--patch-1/libarch/cmd-revert.h      2003-09-07 
03:09:26.000000000 +0300
@@ -0,0 +1,27 @@
+/* cmd-revert.h:
+ *
+ ****************************************************************
+ * Copyright (C) 2003 Tom Lord, Stig Brautaset
+ *
+ * See the file "COPYING" for further information about
+ * the copyright and warranty status of this work.
+ */
+
+#ifndef INCLUDE__LIBARCH__CMD_REVERT_H
+#define INCLUDE__LIBARCH__CMD_REVERT_H
+
+
+#include "hackerlab/machine/types.h"
+
+
+
+extern t_uchar arch_cmd_revert_help[];
+
+
+/* automatically generated __STDC__ prototypes */
+extern int arch_cmd_revert (t_uchar * program_name, int argc, char * argv[]);
+#endif  /* INCLUDE__LIBARCH__CMD_REVERT_H */
+
+
+/* tag: Stig Brautaset Sat Jun  7 14:09:26 BST 2003 (cmd-revert.h)
+ */
diff -rudpN -x {arch} -x .arch-ids -x ',,*' 
tla--revert--1.1--base-0/libarch/cmds.c tla--revert--1.1--patch-1/libarch/cmds.c
--- tla--revert--1.1--base-0/libarch/cmds.c     2003-09-07 03:09:08.000000000 
+0300
+++ tla--revert--1.1--patch-1/libarch/cmds.c    2003-09-07 03:09:26.000000000 
+0300
@@ -94,6 +94,7 @@
 #include "tla/libarch/cmd-tree-version.h"
 #include "tla/libarch/cmd-uncacherev.h"
 #include "tla/libarch/cmd-undo-changes.h"
+#include "tla/libarch/cmd-revert.h"
 #include "tla/libarch/cmd-update.h"
 #include "tla/libarch/cmd-valid-package-name.h"
 #include "tla/libarch/cmd-versions.h"
@@ -134,6 +135,7 @@ struct arch_command arch_commands[] =
   {"undo-changes", arch_cmd_undo_changes, arch_cmd_undo_changes_help, "undo", 
0},
   {"redo", arch_cmd_redo_changes, arch_cmd_redo_changes_help, 0, 0},
   {"redo-changes", arch_cmd_redo_changes, arch_cmd_redo_changes_help, "redo", 
0},
+  {"revert", arch_cmd_revert, arch_cmd_revert_help, 0, 0},
   {"", 0},
   {"what-changed", arch_cmd_what_changed, arch_cmd_what_changed_help, 0, 0},
   {"file-diffs", arch_cmd_file_diffs, arch_cmd_file_diffs_help, 0, 0},
diff -rudpN -x {arch} -x .arch-ids -x ',,*' 
tla--revert--1.1--base-0/libarch/revert.c 
tla--revert--1.1--patch-1/libarch/revert.c
--- tla--revert--1.1--base-0/libarch/revert.c   1970-01-01 02:00:00.000000000 
+0200
+++ tla--revert--1.1--patch-1/libarch/revert.c  2003-09-07 03:09:26.000000000 
+0300
@@ -0,0 +1,85 @@
+/* revert.c:
+ *
+ ****************************************************************
+ * Copyright (C) 2003 Tom Lord
+ *
+ * See the file "COPYING" for further information about
+ * the copyright and warranty status of this work.
+ */
+
+
+#include "hackerlab/fs/file-names.h"
+#include "hackerlab/char/str.h"
+#include "hackerlab/vu/safe.h"
+#include "tla/libarch/diffs.h"
+#include "tla/libarch/cached-inventory.h"
+#include "tla/libarch/local-cache.h"
+#include "tla/libarch/inv-tags.h"
+#include "tla/libarch/revert.h"
+
+
+
+int
+arch_revert (int out_fd, t_uchar * tree_root, t_uchar * mod_loc, t_uchar * 
archive, t_uchar * revision)
+{
+  t_uchar * mod_path = 0;
+  t_uchar * tag = 0;
+  t_uchar * cached_tree = 0;
+  rel_table index = 0;
+  int x;
+  t_uchar * orig_loc = 0;
+  t_uchar * orig_path = 0;
+  t_uchar * tmp_path = 0;
+  t_uchar * dir = 0;
+  int status = 2;
+
+  mod_path = file_name_in_vicinity (0, tree_root, mod_loc);
+  safe_chdir (tree_root);
+  tag = arch_inventory_tag (arch_unspecified_tagging, mod_loc);
+
+  cached_tree = arch_find_or_make_local_copy (out_fd, ".", 0, 0, archive, 
revision);
+  if (!cached_tree)
+    {
+      safe_printfmt (2, "arch_revert: tree not found in cache for revert -- 
%s/%s\n", archive, revision);
+    }
+
+  index = arch_cached_index (cached_tree);
+
+  for (x = 0; x < rel_n_records (index); ++x)
+    {
+      if (!str_cmp (tag, index[x][1]))
+        {
+          orig_loc = str_save (0, index[x][0]);
+          orig_path = file_name_in_vicinity (0, cached_tree, index[x][0]);
+          break;
+        }
+    }
+
+  if (!orig_path)
+    {
+      safe_printfmt (out_fd, "file (not present in %s/%s)\n", archive, 
revision);
+      status = 1;
+    }
+  else
+    {
+      dir = file_name_directory_file (0, mod_path);
+      tmp_path = tmp_file_name (dir, ",,new-file");
+      copy_file (orig_path, tmp_path);
+      safe_rename (tmp_path, mod_path);
+      status = 0;
+    }
+
+  lim_free (0, mod_path);
+  lim_free (0, tag);
+  lim_free (0, cached_tree);
+  rel_free_table (index);
+  lim_free (0, orig_loc);
+  lim_free (0, orig_path);
+
+  return status;
+}
+
+
+
+/* tag: Tom Lord Fri May 30 20:19:00 2003 (revert.c)
+ */
diff -rudpN -x {arch} -x .arch-ids -x ',,*' 
tla--revert--1.1--base-0/libarch/revert.h 
tla--revert--1.1--patch-1/libarch/revert.h
--- tla--revert--1.1--base-0/libarch/revert.h   1970-01-01 02:00:00.000000000 
+0200
+++ tla--revert--1.1--patch-1/libarch/revert.h  2003-09-07 03:09:26.000000000 
+0300
@@ -0,0 +1,21 @@
+/* revert.h:
+ *
+ ****************************************************************
+ * Copyright (C) 2003 Tom Lord
+ *
+ * See the file "COPYING" for further information about
+ * the copyright and warranty status of this work.
+ */
+
+#ifndef INCLUDE__LIBARCH__REVERT_H
+#define INCLUDE__LIBARCH__REVERT_H
+
+
+
+/* automatically generated __STDC__ prototypes */
+extern int arch_revert (int out_fd, t_uchar * tree_root, t_uchar * mod_loc, 
t_uchar * archive, t_uchar * revision);
+#endif  /* INCLUDE__LIBARCH__REVERT_H */
+
+
+/* tag: Tom Lord Sat Jan  5 15:26:10 2002 (revert.h)
+ */

reply via email to

[Prev in Thread] Current Thread [Next in Thread]