preserve the log message when an error occurs during import, as intended
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
diff --git a/got/got.c b/got/got.c
index 75e9f8a..4e4e359 100644
--- a/got/got.c
+++ b/got/got.c
@@ -461,10 +461,10 @@ done:
}
static const struct got_error *
-collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
- const char *branch_name)
+collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
+ const char *path_dir, const char *branch_name)
{
- char *initial_content = NULL, *logmsg_path = NULL;
+ char *initial_content = NULL;
const struct got_error *err = NULL;
int fd;
@@ -473,17 +473,16 @@ collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
branch_name) == -1)
return got_error_from_errno("asprintf");
- err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
+ err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
if (err)
goto done;
dprintf(fd, initial_content);
close(fd);
- err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
+ err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
done:
free(initial_content);
- free(logmsg_path);
return err;
}
@@ -576,13 +575,14 @@ cmd_import(int argc, char *argv[])
char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
const char *branch_name = "master";
- char *refname = NULL, *id_str = NULL;
+ char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
struct got_repository *repo = NULL;
struct got_reference *branch_ref = NULL, *head_ref = NULL;
struct got_object_id *new_commit_id = NULL;
int ch;
struct got_pathlist_head ignores;
struct got_pathlist_entry *pe;
+ int preserve_logmsg = 0;
TAILQ_INIT(&ignores);
@@ -679,54 +679,93 @@ cmd_import(int argc, char *argv[])
if (error)
goto done;
free(logmsg);
- error = collect_import_msg(&logmsg, editor, path_dir, refname);
- if (error)
+ error = collect_import_msg(&logmsg, &logmsg_path, editor,
+ path_dir, refname);
+ if (error) {
+ if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
+ logmsg_path != NULL)
+ preserve_logmsg = 1;
goto done;
+ }
}
- if (unveil(path_dir, "r") != 0)
- return got_error_from_errno2("unveil", path_dir);
+ if (unveil(path_dir, "r") != 0) {
+ error = got_error_from_errno2("unveil", path_dir);
+ if (logmsg_path)
+ preserve_logmsg = 1;
+ goto done;
+ }
error = apply_unveil(got_repo_get_path(repo), 0, NULL);
- if (error)
+ if (error) {
+ if (logmsg_path)
+ preserve_logmsg = 1;
goto done;
+ }
error = got_repo_import(&new_commit_id, path_dir, logmsg,
author, &ignores, repo, import_progress, NULL);
- if (error)
+ if (error) {
+ if (logmsg_path)
+ preserve_logmsg = 1;
goto done;
+ }
error = got_ref_alloc(&branch_ref, refname, new_commit_id);
- if (error)
+ if (error) {
+ if (logmsg_path)
+ preserve_logmsg = 1;
goto done;
+ }
error = got_ref_write(branch_ref, repo);
- if (error)
+ if (error) {
+ if (logmsg_path)
+ preserve_logmsg = 1;
goto done;
+ }
error = got_object_id_str(&id_str, new_commit_id);
- if (error)
+ if (error) {
+ if (logmsg_path)
+ preserve_logmsg = 1;
goto done;
+ }
error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
if (error) {
- if (error->code != GOT_ERR_NOT_REF)
+ if (error->code != GOT_ERR_NOT_REF) {
+ if (logmsg_path)
+ preserve_logmsg = 1;
goto done;
+ }
error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
branch_ref);
- if (error)
+ if (error) {
+ if (logmsg_path)
+ preserve_logmsg = 1;
goto done;
+ }
error = got_ref_write(head_ref, repo);
- if (error)
+ if (error) {
+ if (logmsg_path)
+ preserve_logmsg = 1;
goto done;
+ }
}
printf("Created branch %s with commit %s\n",
got_ref_get_name(branch_ref), id_str);
done:
+ if (preserve_logmsg) {
+ fprintf(stderr, "%s: log message preserved in %s\n",
+ getprogname(), logmsg_path);
+ } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
+ error = got_error_from_errno2("unlink", logmsg_path);
free(logmsg);
+ free(logmsg_path);
free(repo_path);
free(editor);
free(refname);