[PATCH 1 of 5 V3 c-hglib] examples: import command example, implemented using level 0

Giovanni Gherdovich g.gherdovich at gmail.com
Fri Feb 7 16:25:57 CST 2014


# HG changeset patch
# User Iulian Stana and Giovanni Gherdovich <g.gherdovich at gmail.com>
# Date 1380286561 -10800
#      Fri Sep 27 15:56:01 2013 +0300
# Node ID 50d143796f41c2478a844dccd84e679a544a0371
# Parent  965467fac9f9a1e273750247acf48be0293759ad
examples: import command example, implemented using level 0

This is a illustative implementation for import command. The implementation is
using just level0 functions.

diff -r 965467fac9f9 -r 50d143796f41 README
--- a/README	Fri Sep 20 16:14:35 2013 +0300
+++ b/README	Fri Sep 27 15:56:01 2013 +0300
@@ -78,6 +78,38 @@
 In the example folder you will find examples on how you can use c-hglib API.
 To compile the binary files you can use the Makefile from the root directory.
 
+* Import example:
+
+The import example will use the level 0 implementation and will import to the
+given repository the given patch.
+To compile the binary file you can use the make tool with "examples" target.
+  > make examples
+
+This action will create an executable file named import_level0.
+
+To run this executable, the first argument must be a path to the repository
+where you want to import the patch, and the second argument must be the patch
+itself.
+
+If c-hglib has not been installed system-wide via `make install`, the
+environment variable LD_LIBRARY_PATH has to be augmented with the path of
+libhg.so, which is likely to be the path to c-hglib sources (it's the case if
+only `make build` has been run).
+
+  e.g: LD_LIBRARY_PATH=/path/to/c-hglib:$LD_LIBRARY_PATH \
+       ./import_level0 repository_path import_patch
+
+To run this example, you can use an existing mercurial repository, or create
+a throw-away one just for the example. Here is how
+you can create an ad-hoc repository
+  e.g:
+  > hg init export
+  > cd export
+  > touch foo ; echo foo > foo; hg add foo ; hg commit -m foo
+  > hg export -r 0 -o ../rev0.diff
+  > cd .. ; rm -rf export/
+  > hg init import
+
 * Log example:
 
 The log example will use the level 0 implementation and will provide the history
diff -r 965467fac9f9 -r 50d143796f41 examples/import.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/import.c	Fri Sep 27 15:56:01 2013 +0300
@@ -0,0 +1,78 @@
+/* For more details please check the README file from the root directory.*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "client.h"
+#include "utils.h"
+
+#define BUFF_SIZE 4096
+
+/**
+ * \brief Import command example.
+ *
+ * The purpose is to illustrate the functionality, it's not the import function.
+ * This function will import to the handle the import_patch, using input
+ * environment.
+ * \param handle The handle of the connection, wherewith I want to communicate
+ * \param import_patch the file from where to simulate the input importing.
+ * \retval exitcode
+ * */
+int hg_import_by_hand(hg_handle *handle, char *import_patch)
+{
+       char *comm[] = {"import", "-", NULL};
+       char buff[BUFF_SIZE];
+       int exitcode = 0;
+       int fd, ns;
+
+       fd = open(import_patch, O_RDONLY);
+       if (fd < 0) {
+               printf("The '%s' file couldn't be open\n", import_patch);
+               return 1;
+       }
+       hg_rawcommand(handle, comm);
+
+       hg_header *head;
+       while (head = hg_read_header(handle), head != NULL &&
+                                                       head->channel != r) {
+               if (head->channel == o) {
+                       if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
+                               printf("%s", buff);
+                       }
+               } else if (head->channel == e) {
+                       if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
+                               printf("error data: %s", buff);
+                       }
+               } else if (head->channel == L || head->channel == I) {
+                       int length = read(fd, buff, head->length);
+                       hg_rawwrite(handle, buff, length);
+               }
+       }
+
+       exitcode = hg_exitcode(handle);
+       printf("exitcode = %d \n", exitcode);
+       return exitcode;
+}
+
+/**
+ * \brief The main function
+ * */
+int main(int argc, char **argv)
+{
+       hg_handle *handle;
+       int exitcode;
+
+       if (argc != 3) {
+               printf("Usage: %s repository_path import_patch\n", argv[0]);
+               return 1;
+       }
+
+       handle = hg_open(argv[1], NULL);
+       exitcode = hg_import_by_hand(handle, argv[2]);
+       hg_close(&handle);
+
+       return exitcode;
+}


More information about the Mercurial-devel mailing list