9

This is, how I download various master branches from GitHub, and I aim to have a prettier script (and maybe more reliable?).

wget -P ~/ https://github.com/user/repository/archive/master.zip
unzip ~/master.zip
mv ~/*-master ~/dir-name

Can this be shorten to one line somehow, maybe with tar and pipe?

Please address issues of downloading directly to the home directory ~/ and having a certain name for the directory (mv really needed?).

  • 21
    Why not clone the repository? – Kusalananda Feb 04 '18 at 13:14
  • Mostly from the answer below but: wget -c https://github.com/user/repository/archive/master.zip -O ~/master.zip && unzip "$_" && mv ~/*-master ~/dir-name – jesse_b Feb 04 '18 at 13:24
  • 1
    Cloning the repository will download git information not needed by an end-user, even with the depth=1 option. Without the depth= option, the user could end up with a needlessly huge download. – user1404316 Feb 04 '18 at 13:28
  • 5
    @user1404316 yeah, because "end users" never need to run "git pull" because they're never interested in compiling the latest version, or running git show or git log to see what's changed between versions. – cas Feb 04 '18 at 14:11
  • 2
    seems to be a popular question lately: one and two – Jeff Schaller Feb 04 '18 at 14:24
  • 2
    @JeffSchaller probably all one person. the other two questions definitely were. – cas Feb 04 '18 at 14:25
  • @cas End users also don't always need that information. Downloading just the zip file is not an unreasonable request. – chepner Feb 04 '18 at 14:32
  • i never said it was unreasonable. I was objecting to the statement "Cloning the repository will download git information not needed by an end-user". Some end-users won't need it. Some will. The overhead of cloning a git repo is pretty small (roughly proportional to the size of the project and the number & frequency & size of commits). For small to medium sized projects, the overhead is probably less than the size of the .zip file plus the extracted source - but offers a lot more functionality. – cas Feb 04 '18 at 14:49
  • 1
    unzip should take an argument for a directory to extract into, so you could create the parent dir first – Will Crawford Feb 04 '18 at 15:04
  • 1
    @cas: Consider that the person to whom you're making this recommendation is someone who needs an answer to a very basic command line question. Is your expectation that that type of "end user" would be one to be messing with git? – user1404316 Feb 04 '18 at 16:09
  • 1
    given that they're almost certainly going to be compiling whatever they download (or at least installing it into the path if it's a simple script), then yes - they are the sort of end user who will or should be "messing with git". having at least a basic familiarity with the tools of the trade is always a good idea. – cas Feb 04 '18 at 16:12
  • @WillCrawford that's a good approach but it will put my directory inside that directory you mention and it's not what I want... – Arcticooling Feb 04 '18 at 18:04
  • @shorter sorry, I had failed to pick up on you actually wanting to unpack in the home directory. – Will Crawford Feb 05 '18 at 07:04

4 Answers4

18

The shortest way that seems to be what you want would be git clone https://github.com/user/repository --depth 1 --branch=master ~/dir-name. This will only copy the master branch, it will copy as little extra information as possible, and it will store it in ~/dir-name.

tjespe
  • 323
  • Thanks! Small question. I noticed that besides downloading the repo, it also created a dir named .git with many files in it, is there a necessity to create this dir? Is there a way to avoid creating it? – Arcticooling Feb 04 '18 at 18:50
  • 2
    @shorter, there shouldn't be too many files in .git, and no, you can't do without it. What you can do though is remove all of the other files after compiling, then reconstitute them later if needed with git checkout. As an added bonus, when you want to get the latest version in the future, a git pull will only need to download a small amount of data to represent what has changed, instead of downloading the whole thing again. – psusi Feb 04 '18 at 19:09
  • @shorter As long as you are using git, I don't know of any way to avoid creating the .git directory, but you can safely delete it after the command has completed. I can't think of any reason creating it would be a problem, though. Anyway, if you do this you lose the ability to easily update to a future revision. – David Z Feb 05 '18 at 06:25
  • @shorter For a custom .git/ folder location, see: https://stackoverflow.com/questions/5337025/setting-up-git-folder-in-a-custom-location – Luc Feb 05 '18 at 13:53
4

This will clone the files into new directory it creates:

git clone git@github.com:whatever NonExistentNewFolderName
3

Let's start with the bash function I had handy for my own personal use:

wget_github_zip() {
  if [[ $1 =~ ^-+h(elp)?$ ]] ; then
    printf "%s\n" "Downloads a github snapshot of a master branch.\nSupports input URLs of the forms */repo_name, *.git, and */master.zip"
    return
    fi
  if [[ ${1} =~ /archive/master.zip$ ]] ; then
    download=${1}
    out_file=${1/\/archive\/master.zip}
    out_file=${out_file##*/}.zip
  elif [[ ${1} =~ .git$ ]] ; then
    out_file=${1/%.git}
    download=${out_file}/archive/master.zip
    out_file=${out_file##*/}.zip
  else
    out_file=${1/%\/} # remove trailing '/'
    download=${out_file}/archive/master.zip
    out_file=${out_file##*/}.zip
    fi
  wget -c ${download} -O ${out_file}
  }

You want the file to always be called master.zip and always be downloaded into your home directory, so:

wget_github_zip() {
  if [[ $1 =~ ^-+h(elp)?$ ]] ; then
    printf "%s\n" "Downloads a github snapshot of a master branch.\nSupports input URLs of the forms */repo_name, *.git, and */master.zip"
    return
    fi
  if [[ ${1} =~ /archive/master.zip$ ]] ; then
    download=${1}
  elif [[ ${1} =~ .git$ ]] ; then
    out_file=${1/%.git}
    download=${out_file}/archive/master.zip
  else
    out_file=${1/%\/} # remove trailing '/'
    download=${out_file}/archive/master.zip
    fi
  wget -c ${download} -O ~/master.zip && unzip ~/master.zip && mv ~/master.zip ~/myAddons
  }

However, there are a few things for you to consider:

1) My original script will give you a unique download zip file name for each download, based upon the name of the github repository, which is generally what most people really want instead of everything being called master and having to manually rename them afterwards for uniqueness. In that version of the script, you can use the value of $out_file to uniquely name the root directory unzipped tree.

2) If you really do want all downloaded zip files to be named ~/master.zip, do you want to delete each after they have been unzipped?

3) Since you seem to always want everything put in directory ~/myAddons, why not perform all the operations there, and save yourself the need to move the unzipped directory?

user1404316
  • 3,078
1

Here're few ways to download + unzip in oneliner command:

# With WGET and JAR:
wget -O - https://github.com/user/repo/archive/master.zip | jar xv

# With WGET and TAR:
wget -O - https://github.com/user/repo/archive/master.tar.gz | tar xz

# With CURL and JAR:
curl -L https://github.com/user/repo/archive/master.zip | jar xv

# With CURL and TAR:
curl -L https://github.com/user/repo/archive/master.tar.gz | tar xz
Noam Manos
  • 1,031