From: Michal Sojka Date: Wed, 1 Oct 2008 09:48:58 +0000 (+0200) Subject: New directories are created recursively on target X-Git-Url: http://rtime.felk.cvut.cz/gitweb/git-ftp-sync.git/commitdiff_plain/66e64a301b1c4912f30bfd2ad6f990abe25433a9 New directories are created recursively on target This is necessary if you add a file deeply in the repo structure (e.g. dir/dir2/file). Then first dir has to be created, then dir/dir2 and finally the file can be uploaded. --- diff --git a/git-ftp-sync b/git-ftp-sync index d3d3e26..412dd3b 100755 --- a/git-ftp-sync +++ b/git-ftp-sync @@ -73,9 +73,21 @@ class Syncer: self.mkd(change.dest_path) elif change.type[0] in ["A", "C", "M"]: # Check whether the target directory exists - retcode = call("git ls-tree %s %s|grep -q ." % (change.oldrev, dirname(change.repo_path)), shell=True) - if (retcode != 0 and normpath(dirname(change.dest_path)) != normpath(dest_root)): - self.mkd(dirname(change.dest_path)) + if (normpath(dirname(change.dest_path)) != normpath(dest_root)): + retcode = 1 + if (change.oldrev): # If there is previous revision, check for it + retcode = call("git ls-tree %s %s|grep -q ." % (change.oldrev, dirname(change.repo_path)), shell=True) + if (retcode != 0): + dirs = normpath(dirname(change.dest_path)).split("/") + #print "YYYYYYYYYYYYY",dirname(change.dest_path) + destdir = "" + for i in dirs: + if (i==""): i="/" + destdir = os.path.join(destdir, i); + #print "XXXXXXXXXXXXX",destdir + #self.mkd(dirname(change.dest_path)) + self.mkd(destdir) + # Upload the file print "%s: UPLOAD"%self.__class__.__name__, change.dest_path pipe = Popen("git cat-file blob %s:%s" % (change.newrev, change.repo_path), @@ -189,8 +201,10 @@ def selftest(): mkdir www # Commit something to www directory in repo without the activated hook - echo 'Bye' > www/first.html - git add www/first.html + mkdir www/dir + mkdir www/dir/dir2 + echo 'Bye' > www/dir/dir2/first.html + git add www/dir/dir2/first.html commit_push 'Added first.html' # Activate the hook and commit a non-mirrored file @@ -202,7 +216,7 @@ def selftest(): # Check that the first commit appears in www even if the hook was # activated later (only for local sync) - grep -q PASSWORD $REPO/hooks/post-receive || test -f ../www/first.html + grep -q PASSWORD $REPO/hooks/post-receive || test -f ../www/dir/dir2/first.html cd www echo 'Hello' > index.html @@ -226,7 +240,7 @@ def selftest(): cd .. git rm www/index2.html commit_push 'Removed index2' - git rm www/first.html + git rm www/dir/dir2/first.html commit_push 'Removed first.html' cd $REPO/.. rm -rf working repo.git www @@ -258,7 +272,7 @@ def update_ref(hash): os.makedirs("refs/remotes/"+remote) file("refs/remotes/"+remote+"/"+branch, "w").write(newrev+"\n") -def add_to_change_list(changes, git_command): +def add_to_change_list(changes, git_command, oldrev, newrev): # Read changes gitdiff = Popen(git_command, stdout=PIPE, stderr=PIPE, close_fds=True, shell=True) @@ -284,18 +298,27 @@ if options.repodir: options.repodir+="/" changes = list() # Read the changes -for line in sys.stdin: - (oldrev, newrev, refname) = line.split() - if refname == "refs/heads/master": - try: - oldrev=file("refs/remotes/ftp/master").readline().strip(); - git_command = "/usr/bin/git diff --name-status %s %s"%(oldrev, newrev) - except IOError: - # We are run for the first time, so (A)dd all files in the repo. - git_command = r"git ls-tree -r --name-only %s | sed -e 's/\(.*\)/A \1/'"%(newrev); - - add_to_change_list(changes, git_command) - +if 'GIT_DIR' in os.environ: + # Invocation from hook + for line in sys.stdin: + (oldrev, newrev, refname) = line.split() + if refname == "refs/heads/master": + try: + oldrev=file("refs/remotes/ftp/master").readline().strip(); + git_command = "/usr/bin/git diff --name-status %s %s"%(oldrev, newrev) + except IOError: + # We are run for the first time, so (A)dd all files in the repo. + oldrev = None; + git_command = r"git ls-tree -r --name-only %s | sed -e 's/\(.*\)/A \1/'"%(newrev); + + add_to_change_list(changes, git_command, oldrev, newrev) +else: + # Manual invocation + newrev = "HEAD" + oldrev = None; + git_command = r"git ls-tree -r --name-only HEAD | sed -e 's/\(.*\)/A \1/'"; + add_to_change_list(changes, git_command, oldrev, newrev) + if not changes: perror("No changes to sync") sys.exit(0)