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

Iulian Stana julian.stana at gmail.com
Mon Dec 2 13:10:11 CST 2013


# HG changeset patch
# User Iulian Stana <julian.stana at 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.

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


More information about the Mercurial-devel mailing list