[PATCH 4 of 5 V3 c-hglib] examples: init command example, implemented using level 0

Giovanni Gherdovich g.gherdovich at gmail.com
Fri Feb 7 16:26:00 CST 2014


# HG changeset patch
# User Iulian Stana and Giovanni Gherdovich <g.gherdovich at gmail.com>
# Date 1380291105 -10800
#      Fri Sep 27 17:11:45 2013 +0300
# Node ID 658ac21e531b82c1cc1208d6c2950013f7cc508c
# Parent  85e083102b3cbb501ae65997257570c2b653e523
examples: init command example, implemented using level 0

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

diff -r 85e083102b3c -r 658ac21e531b README
--- a/README	Fri Sep 27 16:55:28 2013 +0300
+++ b/README	Fri Sep 27 17:11:45 2013 +0300
@@ -110,6 +110,26 @@
   > cd .. ; rm -rf export/
   > hg init import
 
+* Init example:
+
+The init example will use the level 0 implementation and will create a new
+repository with the given name.
+To compile the binary file you can use the make tool with "examples" target.
+  > make examples
+
+This action will create an executable file named init_level0.
+
+To run this executable, give as argument the path where you want
+the repository to be initialized.
+
+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 \
+       ./import_level0 repository_name
+
 * Log example:
 
 The log example will use the level 0 implementation and will provide the history
diff -r 85e083102b3c -r 658ac21e531b examples/init.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/init.c	Fri Sep 27 17:11:45 2013 +0300
@@ -0,0 +1,67 @@
+/* For more details please check the README file from the root directory.*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "client.h"
+#include "utils.h"
+
+#define BUFF_SIZE 4096
+
+/**
+ * \brief Init command example.
+ *
+ * Create a repo to the specific path, and then open the connection with
+ * this new repo.
+ *
+ * The clone command will follow the same steps.
+ *  - clone repo
+ *  - open connection.
+ *
+ * \retval handle for the new repo.
+ * */
+hg_handle *hg_init_by_hand(char *init_repo)
+{
+       pid_t cpid;
+       int status;
+       hg_handle *handle;
+       char command[50];
+
+       sprintf(command, "hg init %s", init_repo);
+
+       if ((cpid = fork()) < 0) {
+               printf("Fork failed\n");
+               return NULL;
+
+       } else if (cpid == 0) {
+               execl("/bin/sh", "sh", "-c", command, NULL);
+       } else {
+                waitpid(cpid, &status, 0);
+       }
+
+       handle = hg_open(init_repo, NULL);
+
+       return handle;
+}
+
+
+/**
+ * \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_init_by_hand(argv[1]);
+       hg_close(&handle);
+
+       return 0;
+}


More information about the Mercurial-devel mailing list