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

login
register
mail settings
Submitter Giovanni Gherdovich
Date Feb. 7, 2014, 10:25 p.m.
Message ID <85e083102b3cbb501ae6.1391811959@tosh>
Download mbox | patch
Permalink /patch/3517/
State Accepted
Headers show

Comments

Giovanni Gherdovich - Feb. 7, 2014, 10:25 p.m.
# HG changeset patch
# User Iulian Stana and Giovanni Gherdovich <g.gherdovich@gmail.com>
# Date 1380290128 -10800
#      Fri Sep 27 16:55:28 2013 +0300
# Node ID 85e083102b3cbb501ae65997257570c2b653e523
# Parent  eb78cae73f00e1bd60011ef34a354ec9b8e4a5fa
examples: verify command example, implemented using level 0

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

Patch

diff -r eb78cae73f00 -r 85e083102b3c README
--- a/README	Fri Sep 27 16:32:50 2013 +0300
+++ b/README	Fri Sep 27 16:55:28 2013 +0300
@@ -171,6 +171,42 @@ 
   > hg up 0
   > echo baz > foo ; hg commit -m 'baz 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 examples
+
+This action will create an executable file named verify_level0.
+
+To run this executable, give the path of a repository as argument.
+
+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, which is likely to be the path to c-hglib sources (it's the case if
+only `make build` has been run).
+
+  e.g: LD_LIBRARY_PATH=/path/to/c-hglib:$LD_LIBRARY_PATH \
+       ./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 isn't corrupted`
+  > hg init tmp
+  > cd tmp
+  > touch foo ; hg add foo ; hg commit -m foo
+  > touch bar ; hg add bar ; hg commit -m bar
+
+  `Corrupted repository`
+  > hg init tmp
+  > cd tmp
+  > touch foo ; hg add foo ; hg commit -m foo
+  > rm .hg/store/data/foo.i
+
+
 Contact
 """"""""
 
diff -r eb78cae73f00 -r 85e083102b3c examples/verify.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/verify.c	Fri Sep 27 16:55:28 2013 +0300
@@ -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;
+}