├── .gitignore ├── .travis.yml ├── Cargo.lock ├── Cargo.toml ├── README.md ├── default.nix ├── make-tar.nix ├── prefetch-github-completion.bash ├── src └── main.rs └── test ├── Makefile └── default.nix /.gitignore: -------------------------------------------------------------------------------- 1 | prefetch-github 2 | prefetch-github.tar.gz 3 | /target 4 | **/*.rs.bk 5 | result 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | dist: trusty 3 | language: nix 4 | script: 5 | - glibcLocales=$(nix-build --no-out-link "" -A glibcLocales) 6 | - echo $glibcLocales 7 | - export LOCALE_ARCHIVE_2_27="${glibcLocales}/lib/locale/locale-archive" 8 | - cd test 9 | - make 10 | - cd .. 11 | - nix-shell --pure make-tar.nix --run 'echo make-tar complete.' 12 | deploy: 13 | provider: releases 14 | api_key: $API_KEY 15 | file: 16 | - prefetch-github.tar.gz 17 | skip_cleanup: true 18 | on: 19 | tags: true 20 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "prefetch-github" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "prefetch-github" 3 | version = "0.1.0" 4 | authors = ["justinwoo "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Prefetch-Github 2 | 3 | [![Build Status](https://travis-ci.org/justinwoo/prefetch-github.svg?branch=master)](https://travis-ci.org/justinwoo/prefetch-github) 4 | 5 | A helper to save me some keystrokes from `nix-prefetch-git`. 6 | 7 | ## Usage 8 | 9 | ```sh 10 | > prefetch-github -owner justinwoo -repo spacchetti 11 | { 12 | owner = "justinwoo"; 13 | repo = "spacchetti"; 14 | rev = "9c5661c7fa994c08932494600fb0fee1e0d6ce11"; 15 | sha256 = "1d3x15qr4iw1gsswx6qhmmh1lmfh12fwdfi94gkkxiihnwimzfdm"; 16 | } 17 | ``` 18 | 19 | ```sh 20 | > prefetch-github -h 21 | Usage of /home/justin/.nix-profile/bin/prefetch-github: 22 | -branch 23 | Treat the rev as a branch, where the commit reference should be used. 24 | -fetchgit 25 | Print the output in the fetchGit format. Default: fromFromGitHub 26 | -hash-only 27 | Print only the hash. 28 | -o string 29 | Alias for -owner 30 | -owner string 31 | The owner of the repository. e.g. justinwoo 32 | -r string 33 | Alias for -repo 34 | -repo string 35 | The repository name. e.g. easy-purescript-nix 36 | -rev string 37 | Optionally specify which revision should be fetched. 38 | -v string 39 | Alias for -rev 40 | ``` 41 | 42 | ## Why another Prefetch GitHub tool? What's wrong with... 43 | 44 | ### `nix-prefetch-git` 45 | 46 | Not that much, just that it's annoying to take its input and work with it personally. 47 | 48 | ### [`nix-prefetch-github`](https://github.com/seppeljordan/nix-prefetch-github) 49 | 50 | This tool regularly does not successfully install for me through neither nixpkgs nor pip. I also hate Python for being irreproducible in general. I also have not found that its command line arguments are read in correctly, but this may have been addressed in other releases that I have not been able to install. 51 | -------------------------------------------------------------------------------- /default.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import {} }: 2 | 3 | let 4 | binary = pkgs.rustPlatform.buildRustPackage rec { 5 | name = "prefetch-github-rs"; 6 | version = "0.1.0"; 7 | src = ./.; 8 | cargoSha256 = "0jacm96l1gw9nxwavqi1x4669cg6lzy9hr18zjpwlcyb3qkw9z7f"; 9 | }; 10 | 11 | in pkgs.stdenv.mkDerivation { 12 | name = "prefetch-github"; 13 | 14 | src = ./.; 15 | 16 | buildInputs = [ pkgs.makeWrapper ]; 17 | 18 | installPhase = '' 19 | mkdir -p $out/bin 20 | install -D -m555 -t $out/bin ${binary}/bin/prefetch-github 21 | 22 | wrapProgram $out/bin/prefetch-github \ 23 | --prefix PATH : ${pkgs.lib.makeBinPath [ 24 | pkgs.nix-prefetch-git 25 | ]} 26 | 27 | mkdir -p $out/etc/bash_completion.d/ 28 | cp $src/prefetch-github-completion.bash $out/etc/bash_completion.d/prefetch-github-completion.bash 29 | ''; 30 | } 31 | -------------------------------------------------------------------------------- /make-tar.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import {} }: 2 | 3 | pkgs.stdenv.mkDerivation rec { 4 | name = "make-tar"; 5 | buildInputs = [pkgs.cargo]; 6 | src = ./.; 7 | shellHook = '' 8 | cargo build --release 9 | mv target/release/prefetch-github . 10 | mkdir -p bin 11 | mv prefetch-github bin 12 | tar zcvf prefetch-github.tar.gz bin 13 | ''; 14 | } 15 | -------------------------------------------------------------------------------- /prefetch-github-completion.bash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | _prefetch-github() { 4 | local cur prev opts 5 | COMPREPLY=() 6 | cur="${COMP_WORDS[COMP_CWORD]}" 7 | prev="${COMP_WORDS[COMP_CWORD-1]}" 8 | opts="-branch -fetchgit -hash-only -owner -repo -rev" 9 | 10 | case $prev in 11 | "-owner") 12 | COMPREPLY=("justinwoo"); 13 | return 0;; 14 | "-repo") 15 | COMPREPLY=("prefetch-github"); 16 | return 0;; 17 | "-rev") 18 | COMPREPLY=("REVISION"); 19 | return 0;; 20 | *) 21 | # shellcheck disable=SC2207 22 | # shellcheck disable=SC2086 23 | COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ); 24 | return 0;; 25 | esac 26 | } 27 | 28 | complete -F _prefetch-github prefetch-github 29 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | #![warn(rust_2018_idioms, clippy::all)] 2 | 3 | use std::env; 4 | use std::process::exit; 5 | use std::process::Command; 6 | 7 | fn main() { 8 | let mut args: Vec = env::args().collect(); 9 | args.remove(0); 10 | 11 | let mut help: bool = false; 12 | let mut branch: bool = false; 13 | let mut fetchgit: bool = false; 14 | let mut hash_only: bool = false; 15 | let mut opt_owner: Option<&str> = None; 16 | let mut opt_repo: Option<&str> = None; 17 | let mut opt_rev: Option<&str> = None; 18 | 19 | let required_param = |tag: &str, i: usize| { 20 | let result: Option<&str>; 21 | match args.get(i + 1) { 22 | Some(x) => result = Some(x), 23 | None => { 24 | println!("Error: Expected argument for flag '{}'", tag); 25 | exit(1) 26 | } 27 | }; 28 | result 29 | }; 30 | 31 | for (i, x) in args.clone().iter().enumerate() { 32 | let word: &str = x; 33 | match word { 34 | "-owner" => opt_owner = required_param("-owner", i), 35 | "-repo" => opt_repo = required_param("-repo", i), 36 | "-rev" => opt_rev = required_param("-rev", i), 37 | 38 | "-branch" => branch = true, 39 | "-fetchgit" => fetchgit = true, 40 | "-hash-only" => hash_only = true, 41 | 42 | "help" => help = true, 43 | "-h" => help = true, 44 | "-help" => help = true, 45 | "--help" => help = true, 46 | _ => {} 47 | }; 48 | } 49 | 50 | if help || args.is_empty() { 51 | println!("{}", HELP_MESSAGE); 52 | exit(0); 53 | } 54 | 55 | if opt_owner.is_none() { 56 | println!("You must specify an owner via '-owner OWNER'. See help."); 57 | exit(1); 58 | } 59 | 60 | if opt_repo.is_none() { 61 | println!("You must specify a repo via '-repo REPO'. See help."); 62 | exit(1); 63 | } 64 | 65 | let owner = opt_owner.unwrap(); 66 | let repo = opt_repo.unwrap(); 67 | 68 | let results: NixPrefetchGitResults = get_nix_prefetch_git_results(owner, repo, opt_rev, branch); 69 | 70 | if hash_only { 71 | println!("{}", results.sha); 72 | exit(0); 73 | } 74 | 75 | if fetchgit { 76 | println!( 77 | r#"{{ 78 | url = "{}"; 79 | rev = "{}"; 80 | sha256 = "{}"; 81 | }}"#, 82 | results.url, results.rev, results.sha 83 | ); 84 | exit(0); 85 | } else { 86 | println!( 87 | r#"{{ 88 | owner = "{}"; 89 | repo = "{}"; 90 | rev = "{}"; 91 | sha256 = "{}"; 92 | }}"#, 93 | owner, repo, results.rev, results.sha 94 | ); 95 | exit(0); 96 | } 97 | } 98 | 99 | struct NixPrefetchGitResults { 100 | url: String, 101 | rev: String, 102 | sha: String, 103 | } 104 | 105 | fn get_nix_prefetch_git_results( 106 | owner_arg: &str, 107 | repo_arg: &str, 108 | opt_rev_arg: Option<&str>, 109 | as_branch: bool, 110 | ) -> NixPrefetchGitResults { 111 | let rev_arg = match (opt_rev_arg, as_branch) { 112 | (Some(r), true) => { 113 | let formatted = format!("{}{}", REFS_PREFIX, r); 114 | formatted 115 | } 116 | (Some(r), false) => r.to_owned(), 117 | (None, _) => "".to_owned(), 118 | }; 119 | 120 | let prefetch_result = Command::new("nix-prefetch-git") 121 | .arg("--quiet") 122 | .arg(format!("https://github.com/{}/{}.git/", owner_arg, repo_arg)) 123 | .arg("--rev") 124 | .arg(rev_arg) 125 | .env("GIT_TERMINAL_PROMPT", "0") 126 | .output() 127 | .expect("Failed to run nix-prefetch-git in bash") 128 | .stdout; 129 | 130 | let result: String = String::from_utf8(prefetch_result).unwrap(); 131 | 132 | // Parse out the result of running nix-prefetch-git 133 | // { 134 | // "url": "https://github.com/justinwoo/easy-purescript-nix", 135 | // "rev": "54266e45aeaebc78dd51a40da36e9840a8a300dd", 136 | // "date": "2019-02-08T01:59:41+02:00", 137 | // "sha256": "1swjii6975cpys49w5rgnhw5x6ms2cc9fs8ijjpk04pz3zp2vpzn", 138 | // "fetchSubmodules": false 139 | // } 140 | 141 | let mut url: Option = None; 142 | let mut rev: Option = None; 143 | let mut sha: Option = None; 144 | 145 | for line_ in result.lines() { 146 | let line = line_.replace(",", "").replace("\"", ""); 147 | let split = line.split_whitespace().collect::>(); 148 | if line.contains("url") { 149 | url = Some(split[1].to_string()); 150 | } 151 | if line.contains("rev") { 152 | rev = Some(split[1].to_string()); 153 | } 154 | if line.contains("sha256") { 155 | sha = Some(split[1].to_string()); 156 | } 157 | } 158 | 159 | let prefetch_results = NixPrefetchGitResults { 160 | url: url 161 | .unwrap_or_else(|| panic!("Could not parse url result")) 162 | .to_owned(), 163 | rev: rev 164 | .unwrap_or_else(|| panic!("Could not parse revision result")) 165 | .to_owned(), 166 | sha: sha 167 | .unwrap_or_else(|| panic!("Could not parse sha256 result")) 168 | .to_owned(), 169 | }; 170 | 171 | if prefetch_results.rev == FAILED_PREFETCH_REV { 172 | println!("nix-prefetch-git could not fetch the repo with the given params."); 173 | exit(1); 174 | } else { 175 | prefetch_results 176 | } 177 | } 178 | 179 | // prefix used to refer to a branch instead of tag 180 | const REFS_PREFIX: &str = "refs/heads/"; 181 | 182 | // we live in hell, so we must deal with this: 183 | // the prefix given when nix-prefetch-git --quiet fails to find a legitimate revision 184 | // the properties in the result will be as follows: 185 | // "rev": "refs/heads/fetchgit", 186 | // "date": "", 187 | // "sha256": "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5", 188 | // "fetchSubmodules": false 189 | // the sha is computed likely from an empty checkout, as this sha is consistent across empty revisions 190 | const FAILED_PREFETCH_REV: &str = "refs/heads/fetchgit"; 191 | 192 | const HELP_MESSAGE: &str = "Usage of prefetch-github: 193 | -branch 194 | Treat the rev as a branch, where the commit reference should be used. 195 | -fetchgit 196 | Print the output in the fetchGit format. Default: fromFromGitHub 197 | -hash-only 198 | Print only the hash. 199 | -owner string 200 | The owner of the repository. e.g. justinwoo 201 | -repo string 202 | The repository name. e.g. easy-purescript-nix 203 | -rev string 204 | Optionally specify which revision should be fetched."; 205 | -------------------------------------------------------------------------------- /test/Makefile: -------------------------------------------------------------------------------- 1 | test: 2 | nix-shell --run 'prefetch-github -owner justinwoo -repo prefetch-github' -j 10 3 | -------------------------------------------------------------------------------- /test/default.nix: -------------------------------------------------------------------------------- 1 | let 2 | pkgs = import {}; 3 | 4 | prefetch-github = import ../. { inherit pkgs; }; 5 | 6 | in pkgs.stdenv.mkDerivation { 7 | name = "test"; 8 | buildInputs = [ 9 | prefetch-github 10 | ]; 11 | } 12 | --------------------------------------------------------------------------------