First (untested) attempt at migrating p4-git-sync into the final git-p4 script
[gitweb.git] / contrib / fast-import / p4-git-sync.py
index a4d126fd463c311d256991aa9dad7fcebebfec66..5a3bf90485ddecc28fd9b19b393969a65d35c455 100755 (executable)
@@ -33,9 +33,18 @@ def p4Cmd(cmd):
         result.update(entry)
     return result;
 
+def die(msg):
+    sys.stderr.write(msg + "\n")
+    sys.exit(1)
+
+def tryGitDir(path):
+    if os.path.exists(path + "/HEAD") and os.path.exists(path + "/refs") and os.path.exists(path + "/objects"):
+        return True;
+    return False
+
 try:
     opts, args = getopt.getopt(sys.argv[1:], "", [ "continue", "git-dir=", "origin=", "reset", "master=",
-                                                   "submit-log-subst=", "log-substitutions=", "interactive",
+                                                   "submit-log-subst=", "log-substitutions=", "noninteractive",
                                                    "dry-run" ])
 except getopt.GetoptError:
     print "fixme, syntax error"
@@ -46,10 +55,10 @@ def p4Cmd(cmd):
 logSubstitutions["\tDetails:"] = "\tDetails:  %log%"
 gitdir = os.environ.get("GIT_DIR", "")
 origin = "origin"
-master = "master"
+master = ""
 firstTime = True
 reset = False
-interactive = False
+interactive = True
 dryRun = False
 
 for o, a in opts:
@@ -72,8 +81,8 @@ def p4Cmd(cmd):
         for line in open(a, "r").readlines():
             tokens = line[:-1].split("=")
             logSubstitutions[tokens[0]] = tokens[1]
-    elif o == "--interactive":
-        interactive = True
+    elif o == "--noninteractive":
+        interactive = False
     elif o == "--dry-run":
         dryRun = True
 
@@ -82,15 +91,26 @@ def p4Cmd(cmd):
 else:
     os.environ["GIT_DIR"] = gitdir
 
+if not tryGitDir(gitdir):
+    if tryGitDir(gitdir + "/.git"):
+        gitdir += "/.git"
+        os.environ["GIT_DIR"] = gitdir
+    else:
+        die("fatal: %s seems not to be a git repository." % gitdir)
+
+
 configFile = gitdir + "/p4-git-sync.cfg"
 
 origin = "origin"
 if len(args) == 1:
     origin = args[0]
 
-def die(msg):
-    sys.stderr.write(msg + "\n")
-    sys.exit(1)
+if len(master) == 0:
+    sys.stdout.write("Auto-detecting current branch: ")
+    master = os.popen("git-name-rev HEAD").read().split(" ")[1][:-1]
+    if len(master) == 0 or not os.path.exists("%s/refs/heads/%s" % (gitdir, master)):
+        die("\nFailed to detect current branch! Aborting!");
+    sys.stdout.write("%s\n" % master)
 
 def system(cmd):
     if os.system(cmd) != 0:
@@ -269,8 +289,8 @@ def apply(id):
         print "Deleting temporary p4-sync branch and going back to %s" % master
         system("git checkout %s" % master)
         system("git branch -D p4-sync")
-        print "Cleaning out your perforce checkout by doing p4 edit ... ; p4 revert -a ..."
-        system("p4 edit ...")
-        system("p4 revert -a ...")
+        print "Cleaning out your perforce checkout by doing p4 edit ... ; p4 revert ..."
+        system("p4 edit ... >/dev/null")
+        system("p4 revert ... >/dev/null")
     os.remove(configFile)