diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2017-10-30T18·57+0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2017-10-30T18·59+0100 |
commit | 72cd52c3cdd1fc465fade6d553b3823aca9f8b6e (patch) | |
tree | 403a3d512f405a98cf304e1568dadb13f4c79e0b | |
parent | 197922ea4e76ec9439d487e2d16411495a71df4e (diff) |
builtins.fetchgit: Support importing a working tree
For example, you can write src = fetchgit ./.; and if ./. refers to an unclean working tree, that tree will be copied to the Nix store. This removes the need for "cleanSource".
-rw-r--r-- | src/libexpr/parser.y | 2 | ||||
-rw-r--r-- | src/libexpr/primops/fetchgit.cc | 49 | ||||
-rw-r--r-- | src/libexpr/primops/fetchgit.hh | 3 | ||||
-rw-r--r-- | src/libutil/archive.cc | 2 | ||||
-rw-r--r-- | src/libutil/archive.hh | 7 | ||||
-rw-r--r-- | src/libutil/hash.hh | 2 | ||||
-rw-r--r-- | src/libutil/util.hh | 6 |
7 files changed, 54 insertions, 17 deletions
diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index eee31522830f..7e63dc89f608 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -667,7 +667,7 @@ std::pair<bool, std::string> EvalState::resolveSearchPathElem(const SearchPathEl try { if (hasPrefix(elem.second, "git://") || hasSuffix(elem.second, ".git")) // FIXME: support specifying revision/branch - res = { true, exportGit(store, elem.second, "master").storePath }; + res = { true, exportGit(store, elem.second).storePath }; else res = { true, getDownloader()->downloadCached(store, elem.second, true) }; } catch (DownloadError & e) { diff --git a/src/libexpr/primops/fetchgit.cc b/src/libexpr/primops/fetchgit.cc index 4af5301247bc..dc526485fa31 100644 --- a/src/libexpr/primops/fetchgit.cc +++ b/src/libexpr/primops/fetchgit.cc @@ -16,9 +16,48 @@ using namespace std::string_literals; namespace nix { GitInfo exportGit(ref<Store> store, const std::string & uri, - const std::string & ref, const std::string & rev, + std::experimental::optional<std::string> ref, const std::string & rev, const std::string & name) { + if (!ref && rev == "" && hasPrefix(uri, "/") && pathExists(uri + "/.git")) { + + bool clean = true; + + try { + runProgram("git", true, { "-C", uri, "diff-index", "--quiet", "HEAD", "--" }); + } catch (ExecError e) { + if (!WIFEXITED(e.status) || WEXITSTATUS(e.status) != 1) throw; + clean = false; + } + + if (!clean) { + + /* This is an unclean working tree. So copy all tracked + files. */ + + GitInfo gitInfo; + gitInfo.rev = "0000000000000000000000000000000000000000"; + gitInfo.shortRev = std::string(gitInfo.rev, 0, 7); + + auto files = tokenizeString<std::set<std::string>>( + runProgram("git", true, { "-C", uri, "ls-files", "-z" }), "\0"s); + + PathFilter filter = [&](const Path & p) -> bool { + assert(hasPrefix(p, uri)); + auto st = lstat(p); + if (S_ISDIR(st.st_mode)) return true; + std::string file(p, uri.size() + 1); + return files.count(file); + }; + + gitInfo.storePath = store->addToStore("source", uri, true, htSHA256, filter); + + return gitInfo; + } + } + + if (!ref) ref = "master"; + if (rev != "") { std::regex revRegex("^[0-9a-fA-F]{40}$"); if (!std::regex_match(rev, revRegex)) @@ -32,7 +71,7 @@ GitInfo exportGit(ref<Store> store, const std::string & uri, runProgram("git", true, { "init", "--bare", cacheDir }); } - std::string localRef = hashString(htSHA256, fmt("%s-%s", uri, ref)).to_string(Base32, false); + std::string localRef = hashString(htSHA256, fmt("%s-%s", uri, *ref)).to_string(Base32, false); Path localRefFile = cacheDir + "/refs/heads/" + localRef; @@ -47,7 +86,7 @@ GitInfo exportGit(ref<Store> store, const std::string & uri, // FIXME: git stderr messes up our progress indicator, so // we're using --quiet for now. Should process its stderr. - runProgram("git", true, { "-C", cacheDir, "fetch", "--quiet", "--force", "--", uri, ref + ":" + localRef }); + runProgram("git", true, { "-C", cacheDir, "fetch", "--quiet", "--force", "--", uri, *ref + ":" + localRef }); struct timeval times[2]; times[0].tv_sec = now; @@ -114,7 +153,7 @@ GitInfo exportGit(ref<Store> store, const std::string & uri, static void prim_fetchGit(EvalState & state, const Pos & pos, Value * * args, Value & v) { std::string url; - std::string ref = "master"; + std::experimental::optional<std::string> ref; std::string rev; std::string name = "source"; PathSet context; @@ -145,7 +184,7 @@ static void prim_fetchGit(EvalState & state, const Pos & pos, Value * * args, Va } else url = state.coerceToString(pos, *args[0], context, false, false); - if (hasPrefix(url, "/")) url = "file://" + url; + if (!isUri(url)) url = absPath(url); // FIXME: git externals probably can be used to bypass the URI // whitelist. Ah well. diff --git a/src/libexpr/primops/fetchgit.hh b/src/libexpr/primops/fetchgit.hh index 056b6fcbe78d..818ab7102e97 100644 --- a/src/libexpr/primops/fetchgit.hh +++ b/src/libexpr/primops/fetchgit.hh @@ -17,7 +17,8 @@ struct GitInfo }; GitInfo exportGit(ref<Store> store, const std::string & uri, - const std::string & ref, const std::string & rev = "", + std::experimental::optional<std::string> ref = {}, + const std::string & rev = "", const std::string & name = ""); } diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc index ea1deb924e67..f71229d8fdd6 100644 --- a/src/libutil/archive.cc +++ b/src/libutil/archive.cc @@ -29,7 +29,7 @@ const std::string narVersionMagic1 = "nix-archive-1"; static string caseHackSuffix = "~nix~case~hack~"; -PathFilter defaultPathFilter; +PathFilter defaultPathFilter = [](const Path &) { return true; }; static void dumpContents(const Path & path, size_t size, diff --git a/src/libutil/archive.hh b/src/libutil/archive.hh index 607ebf8b28f9..8a15e849c7b8 100644 --- a/src/libutil/archive.hh +++ b/src/libutil/archive.hh @@ -44,13 +44,6 @@ namespace nix { `+' denotes string concatenation. */ -struct PathFilter -{ - virtual ~PathFilter() { } - virtual bool operator () (const Path & path) { return true; } -}; - -extern PathFilter defaultPathFilter; void dumpPath(const Path & path, Sink & sink, PathFilter & filter = defaultPathFilter); diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh index d83049b02368..fd7a61df8e46 100644 --- a/src/libutil/hash.hh +++ b/src/libutil/hash.hh @@ -93,8 +93,6 @@ Hash hashFile(HashType ht, const Path & path); /* Compute the hash of the given path. The hash is defined as (essentially) hashString(ht, dumpPath(path)). */ -struct PathFilter; -extern PathFilter defaultPathFilter; typedef std::pair<Hash, unsigned long long> HashResult; HashResult hashPath(HashType ht, const Path & path, PathFilter & filter = defaultPathFilter); diff --git a/src/libutil/util.hh b/src/libutil/util.hh index fccf5d854800..63a93f2ca6a7 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -481,4 +481,10 @@ struct MaintainCount std::pair<unsigned short, unsigned short> getWindowSize(); +/* Used in various places. */ +typedef std::function<bool(const Path & path)> PathFilter; + +extern PathFilter defaultPathFilter; + + } |