[PATCH 5 of 5 c-hglib] examples: export-import command example, implemented using level 0
Iulian Stana
julian.stana at gmail.com
Mon Dec 2 13:10:15 CST 2013
# HG changeset patch
# User Iulian Stana <julian.stana at gmail.com>
# Date 1380292048 -10800
# Node ID 941e5c278c5b7a8c6d439b6dfe70b96a8ea027ef
# Parent f0bd82679df1b156a0f247f837b777f93ae42dd8
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 --git a/README b/README
--- a/README
+++ b/README
@@ -78,6 +78,31 @@
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 example
+
+This action will create an executable file named export_import_level0.
+
+To run this executable, the first argument must be a path to the repository
+where you want to import the patch, second argument.
+ e.g: ./export_import_level0 export_repoy_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
+ > hg export -r 0 -o rev0.diff
+ > cd ..
+ > hg init import
+
* Import example:
The import example will use the level 0 implementation and will import to the
diff --git a/examples/export-import.c b/examples/export-import.c
new file mode 100644
--- /dev/null
+++ b/examples/export-import.c
@@ -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