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

login
register
mail settings
Submitter Iulian Stana
Date Dec. 2, 2013, 7:10 p.m.
Message ID <941e5c278c5b7a8c6d43.1386011415@doppler>
Download mbox | patch
Permalink /patch/3202/
State Superseded
Headers show

Comments

Iulian Stana - Dec. 2, 2013, 7:10 p.m.
# HG changeset patch
# User Iulian Stana <julian.stana@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.
Giovanni Gherdovich - Dec. 7, 2013, 11:56 a.m.
:::: 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.

The two lines above are confusing, and slightly inconsistent with what
the program is actually doing. The following would be better IMO:

"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)."

I would also add:

"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, wich is likely to be the path to c-hglib sources
(it's the case if only `make install` has been run).

e.g: LD_LIBRARY_PATH=/path/to/c-hglib:$LD_LIBRARY_PATH \
     ./export_import_level0 export_repo_path import_repo_path
"

:::: +  e.g: ./export_import_level0 export_repoy_path import_repo_path

remove the line above, redundant with the change I am proposing.
(Also, note the "repoy" typo"

:::: +
:::: +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

Remove the line above, it is not necessary to run `hg export`,
"export_import_level0" is already doing that with the c-hglib API.

:::: +  > 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;
:::: +}

Patch

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;
+}