contrib/hgsh: make to work with remote clone over ssh.
authorVadim Gelfer <vadim.gelfer@gmail.com>
Wed, 12 Jul 2006 15:44:51 -0700
changeset 2602 9cbeef33eaa3
parent 2601 00fc88b0b256
child 2608 ed344f948bd4
contrib/hgsh: make to work with remote clone over ssh.
contrib/hgsh/hgsh.c
--- a/contrib/hgsh/hgsh.c	Wed Jul 12 08:59:20 2006 -0700
+++ b/contrib/hgsh/hgsh.c	Wed Jul 12 15:44:51 2006 -0700
@@ -244,13 +244,20 @@
     exit(EX_OSFILE);
 }
 
+enum cmdline {
+    hg_init,
+    hg_serve,
+};
+
+    
 /*
  * paranoid wrapper, runs hg executable in server mode.
  */
 static void serve_data(int argc, char **argv)
 {
     char *hg_root = HG_ROOT;
-    char *repo, *abspath;
+    char *repo, *repo_root;
+    enum cmdline cmd;
     char *nargv[6];
     struct stat st;
     size_t repolen;
@@ -275,7 +282,12 @@
         goto badargs;
     }
 
-    if (sscanf(argv[2], "hg -R %as serve --stdio", &repo) != 1) {
+    if (sscanf(argv[2], "hg init %as", &repo) == 1) {
+	cmd = hg_init;
+    }
+    else if (sscanf(argv[2], "hg -R %as serve --stdio", &repo) == 1) {
+	cmd = hg_serve;
+    } else {
         goto badargs;
     }
 
@@ -286,7 +298,7 @@
     }
 
     if (hg_root) {
-        if (asprintf(&abspath, "%s/%s/.hg/data", hg_root, repo) == -1) {
+        if (asprintf(&repo_root, "%s/%s/", hg_root, repo) == -1) {
             goto badargs;
         }
 
@@ -296,16 +308,26 @@
          * symlink that looks safe, but really breaks us out of tree.
          */
 
-        if (strstr(abspath, "/../") != NULL) {
+        if (strstr(repo_root, "/../") != NULL) {
             goto badargs;
         }
 
-        /* verify that we really are looking at valid repo. */
+	/* only hg init expects no repo. */
 
-        if (stat(abspath, &st) == -1) {
-            perror(repo);
-            exit(EX_DATAERR);
-        }
+	if (cmd != hg_init) {
+	    char *abs_path;
+	    
+	    if (asprintf(&abs_path, "%s.hg/data", repo_root) == -1) {
+		goto badargs;
+	    }
+
+	    /* verify that we really are looking at valid repo. */
+
+	    if (stat(abs_path, &st) == -1) {
+		perror(repo);
+		exit(EX_DATAERR);
+	    }
+	}
 
         if (chdir(hg_root) == -1) {
             perror(hg_root);
@@ -314,11 +336,22 @@
     }
 
     i = 0;
-    nargv[i++] = HG;
-    nargv[i++] = "-R";
-    nargv[i++] = repo;
-    nargv[i++] = "serve";
-    nargv[i++] = "--stdio";
+
+    switch (cmd) {
+    case hg_serve:
+	nargv[i++] = HG;
+	nargv[i++] = "-R";
+	nargv[i++] = repo;
+	nargv[i++] = "serve";
+	nargv[i++] = "--stdio";
+	break;
+    case hg_init:
+	nargv[i++] = HG;
+	nargv[i++] = "init";
+	nargv[i++] = repo;
+	break;
+    }
+    
     nargv[i] = NULL;
 
     if (debug) {