From b43f8a1e4019da8f1f2d5d46c06d5be936e2826c Mon Sep 17 00:00:00 2001 From: Matija Nalis Date: Sun, 3 Sep 2017 19:55:50 +0200 Subject: [PATCH 1/5] add "--mirror" option to clone to bare mirrored repository --- README.md | 1 + github-clone | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9bf65db..2f2c3f8 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 modified to clone to bare repository with "--mirror" ## Examples diff --git a/github-clone b/github-clone index ae5adab..7757012 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 @@ -28,7 +29,11 @@ class GithubClone if dry_run feedback "Would clone #{name}: #{clone_url}" else - execute_cmd "git clone #{clone_url}" + if use_mirror + execute_cmd "git clone --mirror #{clone_url}" + else + execute_cmd "git clone #{clone_url}" + end end end end @@ -143,7 +148,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 +159,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 --dry-run Only fetch a list of repositories and print it, but do not actually clone them. EOS exit @@ -169,6 +175,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 From 17d2ac686b3f9b6321d6cccab453c008f2bab45b Mon Sep 17 00:00:00 2001 From: Matija Nalis Date: Sun, 3 Sep 2017 20:26:03 +0200 Subject: [PATCH 2/5] detect duplicate on --mirror directories too --- github-clone | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/github-clone b/github-clone index 7757012..8cff30a 100755 --- a/github-clone +++ b/github-clone @@ -22,9 +22,16 @@ class GithubClone def clone_new_repositories determine_github_config clone_repositories.each do |name, clone_url| - if File.exist?(name) - feedback "Already exists: #{name} for #{clone_url}" - next + if use_mirror + if File.exist?(name + ".git") + feedback "Already exists: #{name}.git for #{clone_url}" + next + end + else + if File.exist?(name) + feedback "Already exists: #{name} for #{clone_url}" + next + end end if dry_run feedback "Would clone #{name}: #{clone_url}" From 6ef20afcd6d4c3615d90ef4935b13de5e0be5b16 Mon Sep 17 00:00:00 2001 From: Matija Nalis Date: Mon, 4 Sep 2017 00:55:27 +0200 Subject: [PATCH 3/5] also do "git fetch" updating if cloning bare "--mirror" repositories --- LICENSE | 1 + README.md | 5 +++-- github-clone | 27 ++++++++++++++------------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/LICENSE b/LICENSE index da69927..a4184a6 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,5 @@ Copyright (c) 2008 Wes Oldenbeuving +Copyright (c) 2017 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 2f2c3f8..56f2f55 100644 --- a/README.md +++ b/README.md @@ -3,7 +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 modified to clone to bare repository with "--mirror" +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 @@ -42,4 +42,5 @@ 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 8cff30a..69d6810 100755 --- a/github-clone +++ b/github-clone @@ -22,25 +22,26 @@ class GithubClone def clone_new_repositories determine_github_config clone_repositories.each do |name, clone_url| + cmd = "git clone"; if use_mirror - if File.exist?(name + ".git") - feedback "Already exists: #{name}.git for #{clone_url}" - next - end - else - if File.exist?(name) + name = name + ".git" + cmd = cmd + " --mirror" + end + cmd = "#{cmd} #{clone_url}" + + if File.exist?(name) + 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 - if use_mirror - execute_cmd "git clone --mirror #{clone_url}" - else - execute_cmd "git clone #{clone_url}" - end + execute_cmd cmd end end end @@ -166,7 +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 + --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 From 6a542d357cc57a24163da06b4d9bc00d7a1ceaa3 Mon Sep 17 00:00:00 2001 From: Matija Nalis Date: Mon, 4 Sep 2017 01:06:12 +0200 Subject: [PATCH 4/5] whitespace --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 56f2f55..8af99d7 100644 --- a/README.md +++ b/README.md @@ -43,4 +43,5 @@ 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. + Copyright (c) 2017 - Matija Nalis, released under the MIT license. From 95c60e4c473b9328ae510b6ba82f15f10497f350 Mon Sep 17 00:00:00 2001 From: Matija Nalis Date: Sun, 23 Oct 2022 13:34:29 +0200 Subject: [PATCH 5/5] fix warning github-clone:118: warning: calling URI.open via Kernel#open is deprecated, call URI.open directly or use URI#open --- LICENSE | 2 +- github-clone | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index a4184a6..91a7ee8 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ Copyright (c) 2008 Wes Oldenbeuving -Copyright (c) 2017 Matija Nalis +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/github-clone b/github-clone index 69d6810..da7729a 100755 --- a/github-clone +++ b/github-clone @@ -115,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