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

login
register
mail settings
Submitter Iulian Stana
Date Dec. 2, 2013, 7:10 p.m.
Message ID <4770f8c809b5b2b2ba23.1386011411@doppler>
Download mbox | patch
Permalink /patch/3198/
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 1380286561 -10800
# Node ID 4770f8c809b5b2b2ba23f0a49bc1213310b69e82
# 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.
Giovanni Gherdovich - Dec. 7, 2013, 11:53 a.m.
:::: diff --git a/README b/README
:::: --- a/README
:::: +++ b/README
:::: @@ -78,6 +78,30 @@
::::  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 example
:::: +
:::: +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, second argument.

replace line above with

"where you want to import the patch, and the second argument must be the
patch itself".

also add (as in previous comment I made)

"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 \
     ./import_level0 repository_path import_patch

:::: +  e.g: ./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 boloo > foo; hg add foo ; hg commit -m foo
:::: +  > hg export -r 0 -o rev0.diff

Here I think it's more clear that the "export" repo isn't strictly
necessary, we just want the patch, if you replace the line above with

  > hg export -r 0 -o ../rev0.diff

and then you do

  > cd .. ; rm -rf export/

:::: +  > cd ..

above line now redundant.

:::: +  > hg init import
:::: +
::::  * Log example:
::::
::::  The log example will use the level 0 implementation and will provide
the history
:::: diff --git a/examples/import.c b/examples/import.c
:::: new file mode 100644
:::: --- /dev/null
:::: +++ b/examples/import.c
:::: @@ -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;
:::: +}

Patch

diff --git a/README b/README
--- a/README
+++ b/README
@@ -78,6 +78,30 @@ 
 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 example
+
+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, second argument.
+  e.g: ./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 boloo > foo; hg add foo ; hg commit -m foo
+  > hg export -r 0 -o rev0.diff
+  > cd ..
+  > hg init import
+
 * Log example:
 
 The log example will use the level 0 implementation and will provide the history
diff --git a/examples/import.c b/examples/import.c
new file mode 100644
--- /dev/null
+++ b/examples/import.c
@@ -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;
+}