Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

after git push, system appears that still erasing files #4

Closed
wants to merge 9 commits into from
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ optional arguments:
Directory to keep the cached data to avoid re-
downloading all SVN and Git history each time. This is
optional, but highly recommended
--git-dir GIT_DIR
Directory to keep the cached data to avoid re-
downloading all SVN and Git history each time. This is
optional, but highly recommended
the difference for cache-dir is that is not compress and avoid decompress, copy all to an temporary directory and compress again.


====

Expand All @@ -79,3 +85,13 @@ optional arguments:
-h, --help show this help message and exit
```

## Examples and migration from cache-dir to git-dir

### 1 - Before the existence of --git-dir option
./svn2github.py --cache-dir /home/sergio/rpmfusion/new/VirtualBox/svn2github/virtualbox update sergiomb2/virtualbox

### 2 - Migration from --cache-dir to --git-dir option (just run one time)
./svn2github.py --git-dir /home/sergio/rpmfusion/new/VirtualBox/svn2github/virtualbox-repo/ --cache-dir /home/sergio/rpmfusion/new/VirtualBox/svn2github/virtualbox update sergiomb2/virtualbox

#### 3 - Now we may delete cache_dir and run option just with --git-dir option
./svn2github.py --git-dir /home/sergio/rpmfusion/new/VirtualBox/svn2github/virtualbox-repo/ update sergiomb2/virtualbox
36 changes: 26 additions & 10 deletions svn2github.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def get_last_revision_from_svn(svn_url):
if m:
return int(m.group(1))

return Svn2GithubException("svn info {} output did not specify the current revision".format(svn_url))
raise Svn2GithubException("svn info {} output did not specify the current revision".format(svn_url))


def run_git_cmd(args, git_dir):
return proc.run(["git"] + args, check=True, cwd=git_dir, stderr=DEVNULL, stdin=DEVNULL, stdout=PIPE)
return proc.run(["git"] + args, check=True, cwd=git_dir, stdout=PIPE)


def is_repo_empty(git_dir):
Expand All @@ -50,7 +50,8 @@ def get_svn_info_from_git(git_dir):
if m:
return GitSvnInfo(svn_url=m.group(1), svn_revision=int(m.group(2)), svn_uuid=m.group(3))

return Svn2GithubException("git log -1 HEAD --pretty=%b output did not specify the current revision")
print(result)
raise Svn2GithubException("git log -1 HEAD --pretty=%b output did not specify the current revision")


def git_svn_init(git_svn_info, git_dir):
Expand Down Expand Up @@ -98,25 +99,36 @@ def unpack_cache(cache_path, git_dir):

def save_cache(cache_path, tmp_path, git_dir):
dot_git_dir = os.path.join(git_dir, ".git")
proc.run(["tar", "-cf", tmp_path, "."], check=True, cwd=dot_git_dir, stderr=DEVNULL, stdin=DEVNULL, stdout=DEVNULL)
proc.run(["sleep", "60"], check=True, cwd=dot_git_dir)
proc.run(["tar", "-cJf", tmp_path, "."], check=True, cwd=dot_git_dir)
shutil.copyfile(tmp_path, cache_path)


def sync_github_mirror(github_repo, cache_dir, new_svn_url=None):
def sync_github_mirror(github_repo, cache_dir, new_svn_url=None, new_git_dir=None):
if cache_dir:
os.makedirs(cache_dir, exist_ok=True)
cache_path = os.path.join(cache_dir, "cache." + github_repo.replace("/", ".") + ".tar")
cache_old_path = os.path.join(cache_dir, "cache." + github_repo.replace("/", ".") + ".tar")
cache_path = os.path.join(cache_dir, "cache." + github_repo.replace("/", ".") + ".tar.xz")
if os.path.exists(cache_old_path):
shutil.move(cache_old_path, cache_path)
cached = os.path.exists(cache_path)
else:
cached = False

github_url = "[email protected]:" + github_repo + ".git"

with tempfile.TemporaryDirectory(prefix="svn2github-") as tmp_dir:
if not new_git_dir:
tmp_dir = tempfile.TemporaryDirectory(prefix="svn2github-").name
else:
tmp_dir = new_git_dir

if tmp_dir:
git_dir = os.path.join(tmp_dir, "repo")
if cached and not new_svn_url:
print("Using cached Git repository from " + cache_path)
unpack_cache(cache_path, git_dir)
elif new_git_dir and os.path.exists(new_git_dir):
pass
else:
print("Cloning " + github_url)
git_clone(github_url, git_dir)
Expand All @@ -127,6 +139,8 @@ def sync_github_mirror(github_repo, cache_dir, new_svn_url=None):
git_svn_info = GitSvnInfo(svn_url=new_svn_url, svn_revision=0, svn_uuid=None)
else:
git_svn_info = get_svn_info_from_git(git_dir)
#print(git_svn_info)
print("svn_url = %s, svn_revison = %s" % (git_svn_info.svn_url, git_svn_info.svn_revision))

print("Checking for SVN updates")
upstream_revision = get_last_revision_from_svn(git_svn_info.svn_url)
Expand All @@ -138,7 +152,7 @@ def sync_github_mirror(github_repo, cache_dir, new_svn_url=None):
return

print("Fetching from SVN", end="")
if not cached or new_svn_url:
if (not cached or new_svn_url) and not new_git_dir:
git_svn_init(git_svn_info, git_dir)

for rev in git_svn_fetch(git_dir):
Expand All @@ -153,12 +167,13 @@ def sync_github_mirror(github_repo, cache_dir, new_svn_url=None):

if cache_dir:
print("Saving Git directory to cache")
save_cache(cache_path, os.path.join(tmp_dir, "cache.tar"), git_dir)
save_cache(cache_path, os.path.join(tmp_dir, "cache.tar.xz"), git_dir)


def main():
parser = argparse.ArgumentParser(description="Mirror SVN repositories to GitHub")
parser.add_argument("--cache-dir", help="Directory to keep the cached data to avoid re-downloading all SVN and Git history each time. This is optional, but highly recommended")
parser.add_argument("--git-dir", help="git local dir without be compressed")
subparsers = parser.add_subparsers()

subparser_import = subparsers.add_parser("import", help="Import SVN repository to the GitHub repo")
Expand All @@ -170,7 +185,8 @@ def main():
args = parser.parse_args(sys.argv[1:] or ["--help"])

new_svn_url = args.svn_url if "svn_url" in args else None
sync_github_mirror(args.github_repo, args.cache_dir, new_svn_url=new_svn_url)
new_git_dir = args.git_dir if "git_dir" in args else None
sync_github_mirror(args.github_repo, args.cache_dir, new_svn_url=new_svn_url, new_git_dir=new_git_dir)



Expand Down