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

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


# HG changeset patch
# User Iulian Stana <julian.stana at gmail.com>
# Date 1380290128 -10800
# Node ID dd12b094d3bb1fd5fd9b60d7332a4e644b9ee4bd
# Parent  d18cd4d134b3664eb6389b858bdc76bd9cec7a4e
examples: verify command example, implemented using level 0

This is a illustative implementation for verify command. The implementation is
using just level0 functions.

diff --git a/README b/README
--- a/README
+++ b/README
@@ -160,6 +160,37 @@
   > hg up 0
   > echo voodoo > foo ; hg commit -m 'voodoo text'
 
+* Verify example:
+
+The verify example will use the level 0 implementation and will verify the 
+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 verify_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: ./verify_level0 repository_path
+
+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:
+  `Repository that it's not corrupt`
+  > hg init tmp
+  > cd tmp
+  > touch foo ; hg add foo ; hg commit -m foo
+  > echo baloo > foo ; hg commit -m 'baloo text'
+
+  `Repository that it's corrupt`
+  > hg init tmp
+  > cd tmp
+  > touch foo ; hg add foo ; hg commit -m foo
+  > echo baloo > foo ; hg commit -m 'baloo text'
+  > rm .hg/store/data/foo.i
+
+
 Contact
 """"""""
 
diff --git a/examples/verify.c b/examples/verify.c
new file mode 100644
--- /dev/null
+++ b/examples/verify.c
@@ -0,0 +1,65 @@
+/* For more details please check the README file from the root directory.*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "client.h"
+#include "utils.h"
+
+#define BUFF_SIZE 4096
+
+/**
+ * \brief The verify command example.
+ *
+ * Will receive data, either from output channel, either from error channel.
+ * The data is printed on stdout.
+ * \param handle The handle of the connection, wherewith I want to communicate
+ * \retval exitcode
+ * */
+int hg_verify_by_hand(hg_handle *handle)
+{
+	char *comm[] = {"verify", NULL};
+	char buff[BUFF_SIZE];
+	int exitcode = 0;
+	int ns;
+
+	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("out = %s", buff);
+			}
+		} else if (head->channel == e) {
+			if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
+				printf("err = %s", buff);
+			}
+		}
+	}
+
+	exitcode = hg_exitcode(handle);
+	printf("exitcode = %d\n", exitcode);
+
+	return exitcode;
+}
+
+/**
+ * \brief The main function
+ * */
+int main(int argc, char **argv)
+{
+	hg_handle *handle;
+
+	if (argc != 2) {
+		printf("Usage: %s repository_path\n", argv[0]);
+		return 1;
+	}
+
+	handle = hg_open(argv[1], NULL);
+	hg_verify_by_hand(handle);
+	hg_close(&handle);
+
+	return 0;
+}


More information about the Mercurial-devel mailing list