aboutsummaryrefslogtreecommitdiff
path: root/src/libfetchers/git.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/libfetchers/git.cc')
-rw-r--r--src/libfetchers/git.cc22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/libfetchers/git.cc b/src/libfetchers/git.cc
index 48e1bd6e3..671779f84 100644
--- a/src/libfetchers/git.cc
+++ b/src/libfetchers/git.cc
@@ -363,7 +363,7 @@ struct GitInputScheme : InputScheme
runProgram("git", true, args, {}, true);
}
- std::optional<Path> getSourcePath(const Input & input) override
+ std::optional<Path> getSourcePath(const Input & input) const override
{
auto url = parseURL(getStrAttr(input.attrs, "url"));
if (url.scheme == "file" && !input.getRef() && !input.getRev())
@@ -371,22 +371,30 @@ struct GitInputScheme : InputScheme
return {};
}
- void markChangedFile(const Input & input, std::string_view file, std::optional<std::string> commitMsg) override
+ void putFile(
+ const Input & input,
+ const CanonPath & path,
+ std::string_view contents,
+ std::optional<std::string> commitMsg) const override
{
- auto sourcePath = getSourcePath(input);
- assert(sourcePath);
+ auto root = getSourcePath(input);
+ if (!root)
+ throw Error("cannot commit '%s' to Git repository '%s' because it's not a working tree", path, input.to_string());
+
+ writeFile((CanonPath(*root) + path).abs(), contents);
+
auto gitDir = ".git";
auto result = runProgram(RunOptions {
.program = "git",
- .args = {"-C", *sourcePath, "--git-dir", gitDir, "check-ignore", "--quiet", std::string(file)},
+ .args = {"-C", *root, "--git-dir", gitDir, "check-ignore", "--quiet", std::string(path.rel())},
});
auto exitCode = WEXITSTATUS(result.first);
if (exitCode != 0) {
// The path is not `.gitignore`d, we can add the file.
runProgram("git", true,
- { "-C", *sourcePath, "--git-dir", gitDir, "add", "--intent-to-add", "--", std::string(file) });
+ { "-C", *root, "--git-dir", gitDir, "add", "--intent-to-add", "--", std::string(path.rel()) });
if (commitMsg) {
@@ -394,7 +402,7 @@ struct GitInputScheme : InputScheme
logger->pause();
Finally restoreLogger([]() { logger->resume(); });
runProgram("git", true,
- { "-C", *sourcePath, "--git-dir", gitDir, "commit", std::string(file), "-m", *commitMsg });
+ { "-C", *root, "--git-dir", gitDir, "commit", std::string(path.rel()), "-m", *commitMsg });
}
}
}