diff --git a/LICENSE b/LICENSE index da69927..91a7ee8 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,5 @@ Copyright (c) 2008 Wes Oldenbeuving +Copyright (c) 2017,2021 Matija Nalis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/README.md b/README.md index 9bf65db..8af99d7 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ ## Description Github-clone makes it easy to clone all github projects of a user, using either the public or private clone urls for the projects. +This version adds option to clone to bare repository with "git clone --mirror", and would also update it at later time with "git fetch" it it exists ## Examples @@ -41,4 +42,6 @@ You can find and/or setup your access tokens here. You only need the `repo` scop ## Author -Copyright (c) 2008-2017 - Wes Oldenbeuving, released under the MIT license. \ No newline at end of file +Copyright (c) 2008-2017 - Wes Oldenbeuving, released under the MIT license. + +Copyright (c) 2017 - Matija Nalis, released under the MIT license. diff --git a/github-clone b/github-clone index ae5adab..da7729a 100755 --- a/github-clone +++ b/github-clone @@ -4,13 +4,14 @@ require 'json' # Clone all of a user's GitHub repositories. class GithubClone - attr_accessor :quiet, :use_private_clone_url, :dry_run + attr_accessor :quiet, :use_private_clone_url, :use_mirror, :dry_run attr_reader :username def initialize(username) @username = username @quiet = false @use_private_clone_url = false + @use_mirror = false @github_user = nil @github_token = nil @use_basic_auth = false @@ -21,14 +22,26 @@ class GithubClone def clone_new_repositories determine_github_config clone_repositories.each do |name, clone_url| + cmd = "git clone"; + if use_mirror + name = name + ".git" + cmd = cmd + " --mirror" + end + cmd = "#{cmd} #{clone_url}" + if File.exist?(name) - feedback "Already exists: #{name} for #{clone_url}" - next + if use_mirror + cmd = "git --git-dir=#{name} fetch --tags" + else + feedback "Already exists: #{name} for #{clone_url}" + next + end end + if dry_run - feedback "Would clone #{name}: #{clone_url}" + feedback "Would do #{cmd}" else - execute_cmd "git clone #{clone_url}" + execute_cmd cmd end end end @@ -102,7 +115,7 @@ github.user was not defined, so only fetching public repositories. For more info if @use_basic_auth headers[:http_basic_authentication] = [@github_user, @github_token] end - json = open(url, headers).read + json = URI.open(url, headers).read JSON.parse(json) end @@ -143,7 +156,7 @@ username = ARGV.shift if %w[-h --help help].include? username or username.to_s == '' puts <<-EOS Syntax: - github-clone [ -q ] [ --public | --private ] + github-clone [ -q ] [ --dry-run ] [ --public | --private ] [ --mirror ] This will clone all repositories of into separate directories inside the current directory. When run we check if the git configuration github.user is set. If it is the same as , @@ -154,6 +167,7 @@ Parameters: is your github username. -q Run github-clone in quiet mode, suppressing all output. --public, --private Use public or private clone URL. Defaults to use public. + --mirror Clone with --mirror to bare repository. Will also update with "git fetch" if already exists. --dry-run Only fetch a list of repositories and print it, but do not actually clone them. EOS exit @@ -169,6 +183,8 @@ while option = ARGV.shift gh.use_private_clone_url = false when '--private' gh.use_private_clone_url = true + when '--mirror' + gh.use_mirror = true when '--dry-run' gh.dry_run = true gh.quiet = false