Skip to content

Commit 11a7f8c

Browse files
committedDec 5, 2017
Merge branch 'fetchGit-fast-revision-update'
2 parents 7f2c324 + eedbc4e commit 11a7f8c

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed
 

‎src/libexpr/primops/fetchGit.cc

+35-24
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,43 @@ GitInfo exportGit(ref<Store> store, const std::string & uri,
8989

9090
Path localRefFile = cacheDir + "/refs/heads/" + localRef;
9191

92-
/* If the local ref is older than ‘tarball-ttl’ seconds, do a git
93-
fetch to update the local ref to the remote ref. */
92+
bool doFetch;
9493
time_t now = time(0);
95-
struct stat st;
96-
if (stat(localRefFile.c_str(), &st) != 0 ||
97-
st.st_mtime <= now - settings.tarballTtl)
98-
{
99-
if (rev == "" ||
100-
chomp(runProgram(
101-
RunOptions("git", { "-C", cacheDir, "cat-file", "-t", rev })
102-
.killStderr(true)).second) != "commit")
103-
{
104-
Activity act(*logger, lvlTalkative, actUnknown, fmt("fetching Git repository '%s'", uri));
105-
106-
// FIXME: git stderr messes up our progress indicator, so
107-
// we're using --quiet for now. Should process its stderr.
108-
runProgram("git", true, { "-C", cacheDir, "fetch", "--quiet", "--force", "--", uri, *ref + ":" + localRef });
109-
110-
struct timeval times[2];
111-
times[0].tv_sec = now;
112-
times[0].tv_usec = 0;
113-
times[1].tv_sec = now;
114-
times[1].tv_usec = 0;
115-
116-
utimes(localRefFile.c_str(), times);
94+
/* If a rev was specified, we need to fetch if it's not in the
95+
repo. */
96+
if (rev != "") {
97+
try {
98+
runProgram("git", true, { "-C", cacheDir, "cat-file", "-e", rev });
99+
doFetch = false;
100+
} catch (ExecError & e) {
101+
if (WIFEXITED(e.status)) {
102+
doFetch = true;
103+
} else {
104+
throw;
105+
}
117106
}
107+
} else {
108+
/* If the local ref is older than ‘tarball-ttl’ seconds, do a
109+
git fetch to update the local ref to the remote ref. */
110+
struct stat st;
111+
doFetch = stat(localRefFile.c_str(), &st) != 0 ||
112+
st.st_mtime <= now - settings.tarballTtl;
113+
}
114+
if (doFetch)
115+
{
116+
Activity act(*logger, lvlTalkative, actUnknown, fmt("fetching Git repository '%s'", uri));
117+
118+
// FIXME: git stderr messes up our progress indicator, so
119+
// we're using --quiet for now. Should process its stderr.
120+
runProgram("git", true, { "-C", cacheDir, "fetch", "--quiet", "--force", "--", uri, *ref + ":" + localRef });
121+
122+
struct timeval times[2];
123+
times[0].tv_sec = now;
124+
times[0].tv_usec = 0;
125+
times[1].tv_sec = now;
126+
times[1].tv_usec = 0;
127+
128+
utimes(localRefFile.c_str(), times);
118129
}
119130

120131
// FIXME: check whether rev is an ancestor of ref.

‎tests/common.sh.in

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export NIX_REMOTE=$NIX_REMOTE_
2121
unset NIX_PATH
2222
export TEST_HOME=$TEST_ROOT/test-home
2323
export HOME=$TEST_HOME
24+
unset XDG_CACHE_HOME
2425
mkdir -p $TEST_HOME
2526

2627
export PATH=@bindir@:$PATH

‎tests/fetchGit.sh

+7
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,10 @@ git -C $repo commit -m 'Bla3' -a
8686

8787
path4=$(nix eval --tarball-ttl 0 --raw "(builtins.fetchGit file://$repo).outPath")
8888
[[ $path2 = $path4 ]]
89+
90+
# tarball-ttl should be ignored if we specify a rev
91+
echo delft > $repo/hello
92+
git -C $repo add hello
93+
git -C $repo commit -m 'Bla4'
94+
rev3=$(git -C $repo rev-parse HEAD)
95+
nix eval --tarball-ttl 3600 "(builtins.fetchGit { url = $repo; rev = \"$rev3\"; })" >/dev/null

0 commit comments

Comments
 (0)
Please sign in to comment.