Skip to content

Commit

Permalink
Add option isInteractive
Browse files Browse the repository at this point in the history
  • Loading branch information
KasyanDiGris committed May 18, 2023
1 parent 25434df commit 4c4ae88
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
21 changes: 7 additions & 14 deletions src/libfetchers/git.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ namespace nix::fetchers {

namespace {

template<typename... Args>
auto runProgramWithCredentialsInput(Args... args)
{
logger->pause();
Finally defer([]{ logger->resume(); });
return runProgram(std::forward<Args>(args)...);
}

// Explicit initial branch of our bare repo to suppress warnings from new version of git.
// The value itself does not matter, since we always fetch a specific revision or branch.
// It is set with `-c init.defaultBranch=` instead of `--initial-branch=` to stay compatible with
Expand Down Expand Up @@ -67,10 +59,11 @@ Path getCachePath(std::string_view key)
// ...
std::optional<std::string> readHead(const Path & path)
{
auto [status, output] = runProgramWithCredentialsInput(RunOptions {
auto [status, output] = runProgram(RunOptions {
.program = "git",
// FIXME: use 'HEAD' to avoid returning all refs
.args = {"ls-remote", "--symref", path},
.isInteractive = true,
});
if (status != 0) return std::nullopt;

Expand Down Expand Up @@ -359,7 +352,7 @@ struct GitInputScheme : InputScheme

args.push_back(destDir);

runProgramWithCredentialsInput("git", true, args);
runProgram("git", true, args, {}, true);
}

std::optional<Path> getSourcePath(const Input & input) override
Expand Down Expand Up @@ -564,7 +557,7 @@ struct GitInputScheme : InputScheme
: ref == "HEAD"
? *ref
: "refs/heads/" + *ref;
runProgramWithCredentialsInput("git", true, Strings { "-C", repoDir, "--git-dir", gitDir, "fetch", "--quiet", "--force", "--", actualUrl, fmt("%s:%s", fetchRef, fetchRef) });
runProgram("git", true, Strings { "-C", repoDir, "--git-dir", gitDir, "fetch", "--quiet", "--force", "--", actualUrl, fmt("%s:%s", fetchRef, fetchRef) }, {}, true);
} catch (Error & e) {
if (!pathExists(localRefFile)) throw;
warn("could not update local clone of Git repository '%s'; continuing with the most recent version", actualUrl);
Expand Down Expand Up @@ -630,8 +623,8 @@ struct GitInputScheme : InputScheme
// exists, see FIXME above) so use a big hammer and fetch
// everything to ensure we get the rev.
Activity act(*logger, lvlTalkative, actUnknown, fmt("making temporary clone of '%s'", repoDir));
runProgramWithCredentialsInput("git", true, Strings { "-C", tmpDir, "fetch", "--quiet", "--force",
"--update-head-ok", "--", repoDir, "refs/*:refs/*" });
runProgram("git", true, Strings { "-C", tmpDir, "fetch", "--quiet", "--force",
"--update-head-ok", "--", repoDir, "refs/*:refs/*" }, {}, true);
}

runProgram("git", true, { "-C", tmpDir, "checkout", "--quiet", input.getRev()->gitRev() });
Expand All @@ -658,7 +651,7 @@ struct GitInputScheme : InputScheme

{
Activity act(*logger, lvlTalkative, actUnknown, fmt("fetching submodules of '%s'", actualUrl));
runProgramWithCredentialsInput("git", true, Strings{ "-C", tmpDir, "submodule", "--quiet", "update", "--init", "--recursive" });
runProgram("git", true, Strings{ "-C", tmpDir, "submodule", "--quiet", "update", "--init", "--recursive" }, {}, true);
}

filter = isNotDotGitDirectory;
Expand Down
14 changes: 12 additions & 2 deletions src/libutil/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1141,9 +1141,9 @@ std::vector<char *> stringsToCharPtrs(const Strings & ss)
}

std::string runProgram(Path program, bool searchPath, const Strings & args,
const std::optional<std::string> & input)
const std::optional<std::string> & input, bool isInteractive)
{
auto res = runProgram(RunOptions {.program = program, .searchPath = searchPath, .args = args, .input = input});
auto res = runProgram(RunOptions {.program = program, .searchPath = searchPath, .args = args, .input = input, .isInteractive = isInteractive});

if (!statusOk(res.first))
throw ExecError(res.first, "program '%1%' %2%", program, statusToString(res.first));
Expand Down Expand Up @@ -1193,6 +1193,16 @@ void runProgram2(const RunOptions & options)
// case), so we can't use it if we alter the environment
processOptions.allowVfork = !options.environment;

std::optional<Finally<std::function<void()>>> resumeLoggerDefer;
if (options.isInteractive) {
logger->pause();
resumeLoggerDefer.emplace(
[]() {
logger->resume();
}
);
}

/* Fork. */
Pid pid = startProcess([&]() {
if (options.environment)
Expand Down
3 changes: 2 additions & 1 deletion src/libutil/util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions & options = P
*/
std::string runProgram(Path program, bool searchPath = false,
const Strings & args = Strings(),
const std::optional<std::string> & input = {});
const std::optional<std::string> & input = {}, bool isInteractive = false);

struct RunOptions
{
Expand All @@ -430,6 +430,7 @@ struct RunOptions
Source * standardIn = nullptr;
Sink * standardOut = nullptr;
bool mergeStderrToStdout = false;
bool isInteractive = false;
};

std::pair<int, std::string> runProgram(RunOptions && options);
Expand Down

0 comments on commit 4c4ae88

Please sign in to comment.