GIT Package Repository

GIT Package Repository

README

Every day when I surf over Internet I download many resources like images, scripts and full packages of something that I don’t know yet, because you know, I want to learn many things; those packages are generally GIT Repositories composed for a large tree of folders and files. That’s why I decided to write a small script that I’ll use in my UNIX system like a single command, mainly because I always try to organize the files having all packaged/compressed and because the hard drive of my computer doesn’t have sufficient space left :D.

Code

The script that I’ll write will do the same thing that the command git clone (and really this command will be used inside the script code), but additionally the script will compress the repository cloned in BZip2 format asking before if already exists a file named equal to the output (this will provocate an override of the file or an input prompt to the user to provide a new filename to disable the override action).

The script code shown is in Base64 format because the CMS Wysiwyg doesn’t understand some codes as part of the post and escape it, just decode it with this simple command: echo "BASE64_CODE" | base64 -d > tar-gitclone. Additionally you’ll need to grant execution privileges to the files to let other users and the owner execute it: chmod 755 tar-gitclone; and finally to use this script throught all the paths in the system move it into the path user local binary path:

$ sudo mv -iv tar-gitclone /usr/local/bin/
$ sudo chown root:root /usr/local/bin/tar-gitclone
#!/usr/bin/env ruby
if !ARGV[0].nil? then
	git_path = ARGV[0]
	git_m = git_path.match(/(http|https):\/\/github\.com\/(.*)\/(.*).git/)
	git_m = git_path.match(/.*@(.*):(.*)\/(.*).git/) if git_m.nil?
	if !git_m.nil? then
		git_localname = git_m[3] + '-' + Time.now.to_s.split(' ')[0].gsub('-','')
		git_localfile = "#{git_localname}.tar.bz2"
		puts "\033[1;33m[+] Closing Git repository: #{git_m[2]}/#{git_m[3]} as #{git_localfile}\033[0m"
		if File.exists?(git_localfile) then
			print "\033[1;31m[?] The local file\033[0m \033[1;36m#{git_localfile}\033[0m \033[1;31malready exists, do you want to override it? (y/n) \033[0m"
			override_it = $stdin.gets.to_s.gsub("\n",'')
			if override_it=='y' or override_it=='' then
				File.delete(git_localfile)
			else
				print "    Fill the filename for the compressed version of the repository #{git_localname}: "
				git_localname = $stdin.gets.to_s.gsub("\n",'')
				git_localfile = "#{git_localname}.tar.bz2"
			end
		end
		system("git clone #{git_path} #{git_localname} && tar -c #{git_localname} | bzip2 > #{git_localname}.tar.bz2 && rm -rf #{git_localname}");
		puts "\033[1;33m[+] Download and Compression finished\033[0m"
		system("ls -lhas #{git_localname}.tar.bz2")
	end
else
	puts "Usage: tar-gitclone https://github.com/useraccount/project.git"
end
Do you have a project idea? Let's make it together!