aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-12-16 15:15:34 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-12-16 15:15:34 -0500
commitd664bef4e570647f85d6000170dba7e63807facc (patch)
tree8b847980830527df2a03613556aaf7faab258865
parentc29147a267542750c90ea8fc4905e6a7708b3b55 (diff)
downloadstasis-d664bef4e570647f85d6000170dba7e63807facc.tar.gz
git_clone: rework the error handler to return the proper exit codefix-git-clone-return
* Use goto to drop out for different failure conditions
-rw-r--r--src/lib/core/utils.c37
-rw-r--r--tests/test_recipe.c2
2 files changed, 31 insertions, 8 deletions
diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c
index 96c89bb..621831c 100644
--- a/src/lib/core/utils.c
+++ b/src/lib/core/utils.c
@@ -307,39 +307,64 @@ int touch(const char *filename) {
}
int git_clone(struct Process *proc, char *url, char *destdir, char *gitref) {
- int result = -1;
+ int result = 0;
char *chdir_to = NULL;
char *program = find_program("git");
if (!program) {
- return result;
+ result = -1;
+ goto die_quick;
}
- static char command[PATH_MAX];
+ static char command[PATH_MAX] = {0};
sprintf(command, "%s clone -c advice.detachedHead=false --recursive %s", program, url);
+
if (destdir && access(destdir, F_OK) < 0) {
+ // Destination directory does not exist
sprintf(command + strlen(command), " %s", destdir);
+ // Clone the repo
result = shell(proc, command);
+ if (result) {
+ goto die_quick;
+ }
}
if (destdir) {
chdir_to = destdir;
} else {
+ // Assume the name of the directory to be the basename of the URL
+ // like it is when executed in a shell session
chdir_to = path_basename(url);
}
- pushd(chdir_to);
- {
+ if (!pushd(chdir_to)) {
memset(command, 0, sizeof(command));
sprintf(command, "%s fetch --all", program);
result = shell(proc, command);
+ if (result) {
+ goto die_pop;
+ }
if (gitref != NULL) {
memset(command, 0, sizeof(command));
sprintf(command, "%s checkout %s", program, gitref);
- result += shell(proc, command);
+
+ result = shell(proc, command);
+ if (result) {
+ goto die_pop;
+ }
}
popd();
+ } else {
+ result = -1;
+ goto die_quick;
}
+ return 0;
+
+ die_pop:
+ // close out last pushd() call
+ popd();
+
+ die_quick:
return result;
}
diff --git a/tests/test_recipe.c b/tests/test_recipe.c
index 7c55cd5..531b3b4 100644
--- a/tests/test_recipe.c
+++ b/tests/test_recipe.c
@@ -94,8 +94,6 @@ int main(int argc, char *argv[]) {
STASIS_TEST_FUNC *tests[] = {
test_recipe_clone,
};
- mkdir("workspace", 0755);
- pushd("workspace");
STASIS_TEST_RUN(tests);
popd();
STASIS_TEST_END_MAIN();