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

login
register
mail settings
Submitter Iulian Stana
Date Dec. 2, 2013, 7:10 p.m.
Message ID <f0bd82679df1b156a0f2.1386011414@doppler>
Download mbox | patch
Permalink /patch/3201/
State Superseded
Headers show

Comments

Iulian Stana - Dec. 2, 2013, 7:10 p.m.
# HG changeset patch
# User Iulian Stana <julian.stana@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.
Giovanni Gherdovich - Dec. 7, 2013, 11:55 a.m.
:::: 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.

replace the above two lines with

"To run this executable, give as argument the path where you want
the repository to be initialized."

also:

"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, wich is likely to be the path to c-hglib sources
(it's the case if only `make install` 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 --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;
:::: +}

Patch

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