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

Giovanni Gherdovich g.gherdovich at gmail.com
Fri Feb 7 16:26:01 CST 2014


# HG changeset patch
# User Iulian Stana and Giovanni Gherdovich <g.gherdovich at gmail.com>
# Date 1380292048 -10800
#      Fri Sep 27 17:27:28 2013 +0300
# Node ID d2f51737f8b2b819796c660b8fb5728862684ccc
# Parent  658ac21e531b82c1cc1208d6c2950013f7cc508c
examples: export-import command example, implemented using level 0

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

This example prove the possibility of creating a function that export data from
a repository and import it to a second repository without buffering the entire
data in memory.

diff -r 658ac21e531b -r d2f51737f8b2 README
--- a/README	Fri Sep 27 17:11:45 2013 +0300
+++ b/README	Fri Sep 27 17:27:28 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.
 
+* Export-Import example:
+
+The export-import example will use the level 0 implementation and will export
+the first revision from the first given repository and will import it to the
+second given repository.
+To compile the binary file you can use the make tool with "examples" target.
+  > make examples
+
+This action will create an executable file named export_import_level0.
+
+This executable is `export`-ing revision 0 from a repo (whose path is given as
+first argument) and `import`-ing it to another repo (whose path is given as
+second argument).
+
+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 \
+       ./export_import_level0 export_repo_path import_repo_path
+
+To run this example, you can use two existing mercurial repository,
+or create some throw-away ones just for the example. Here is how
+you can create an ad-hoc repository
+  e.g:
+  > hg init export
+  > cd export
+  > touch foo ; echo boloo > foo; hg add foo ; hg commit -m foo
+  > cd ..
+  > hg init import
+
 * Import example:
 
 The import example will use the level 0 implementation and will import to the
diff -r 658ac21e531b -r d2f51737f8b2 examples/export-import.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/export-import.c	Fri Sep 27 17:27:28 2013 +0300
@@ -0,0 +1,98 @@
+/* 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 The export-import command example.
+ *
+ * An export-import process that is not buffering the entire path in to memory
+ * \param ehandle The handle connection, from where I am exporting
+ * \param ihandle The handle connection, where I am importing.
+ * \retval exitcode
+ * */
+int hg_export_import_by_hand(hg_handle *ehandle, hg_handle *ihandle)
+{
+       char *export_comm[] = {"export", "-r", "0", NULL};
+       char *import_comm[] = {"import", "-", NULL};
+       char ebuff[BUFF_SIZE], ibuff[BUFF_SIZE];
+       int eexitcode, iexitcode;
+
+       hg_rawcommand(ehandle, export_comm);
+       hg_rawcommand(ihandle, import_comm);
+
+       hg_header *ehead, *ihead;
+       while (ehead = hg_read_header(ehandle), ehead->channel != r) {
+               if (ehead->channel == o) {
+                       if (hg_rawread(ehandle, ebuff, BUFF_SIZE) > 0) {
+                               ihead = hg_read_header(ihandle);
+                               while (ihead->channel == o ||
+                                                       ihead->channel == e) {
+                                       if (hg_rawread(ihandle, ibuff,
+                                                       BUFF_SIZE) > 0) {
+                                               printf("%s\n", ibuff);
+                                       }
+                                       ihead = hg_read_header(ihandle);
+                               }
+                               if (ihead->channel == L) {
+                                       hg_rawwrite(ihandle, ebuff,
+                                                               strlen(ebuff));
+                               }
+                       }
+               } else if (ehead->channel == e) {
+                       if (hg_rawread(ehandle, ebuff, BUFF_SIZE) > 0) {
+                               printf("err = %s", ebuff);
+                       }
+               }
+       }
+
+       ihead = hg_read_header(ihandle);
+       hg_rawwrite(ihandle, ebuff, 0);
+
+       while (ihead = hg_read_header(ihandle), ihead->channel != r) {
+               if (ihead->channel == o || ihead->channel == e) {
+                       if (hg_rawread(ihandle, ibuff, BUFF_SIZE) > 0) {
+                               printf("%s", ibuff);
+                       }
+               }
+       }
+
+       eexitcode = hg_exitcode(ehandle);
+       iexitcode = hg_exitcode(ihandle);
+       printf("exitcode for export process is %d \n", eexitcode);
+       printf("exitcode for import process is %d \n", iexitcode);
+
+       return eexitcode + iexitcode;
+}
+
+/**
+ * \brief The main function
+ * */
+int main(int argc, char **argv)
+{
+       hg_handle *ehandle, *ihandle;
+       int exitcode;
+
+       if (argc != 3) {
+               printf("Usage: %s export_repo_path import_repo_path\n",
+                                                               argv[0]);
+               return 1;
+       }
+
+       ehandle = hg_open(argv[1], NULL);
+       ihandle = hg_open(argv[2], NULL);
+
+       exitcode = hg_export_import_by_hand(ehandle, ihandle);
+       hg_close(&ehandle);
+       hg_close(&ihandle);
+
+       return exitcode;
+}


More information about the Mercurial-devel mailing list