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

Iulian Stana julian.stana at gmail.com
Mon Dec 2 19:10:14 UTC 2013


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

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

diff --git a/README b/README
--- a/README
+++ b/README
@@ -102,6 +102,19 @@
   > cd ..
   > 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 example
+
+This action will create an executable file named init_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_name
+
 * Log example:
 
 The log example will use the level 0 implementation and will provide the history
diff --git a/examples/init.c b/examples/init.c
new file mode 100644
--- /dev/null
+++ b/examples/init.c
@@ -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