write commit objects
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
diff --git a/got/got.c b/got/got.c
index f48a524..a39387a 100644
--- a/got/got.c
+++ b/got/got.c
@@ -2132,7 +2132,8 @@ cmd_commit(int argc, char *argv[])
if (error)
goto done;
- error = got_worktree_commit(&id, worktree, path, logmsg, repo);
+ error = got_worktree_commit(&id, worktree, path,
+ "Stefan Sperling <stsp@stsp.name>", NULL, logmsg, repo);
if (error)
goto done;
diff --git a/include/got_worktree.h b/include/got_worktree.h
index debcb30..27d9c4b 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -171,10 +171,10 @@ const struct got_error *got_worktree_revert(struct got_worktree *,
* The worktree's base commit will be set to this new commit.
* Files unaffected by this commit operation will retain their
* current base commit.
- * A non-empty log message must be specified.
+ * An author and a non-empty log message must be specified.
+ * The name of the committer is optional (may be NULL).
* If an on-disk path is specified, only commit changes at or within this path.
*/
-const struct got_error *
-got_worktree_commit(struct got_object_id **,
- struct got_worktree *, const char *, const char *,
- struct got_repository *);
+const struct got_error *got_worktree_commit(struct got_object_id **,
+ struct got_worktree *, const char *, const char *, const char *,
+ const char *, struct got_repository *);
diff --git a/lib/got_lib_object_create.h b/lib/got_lib_object_create.h
index 4d00479..011e79e 100644
--- a/lib/got_lib_object_create.h
+++ b/lib/got_lib_object_create.h
@@ -18,3 +18,7 @@ const struct got_error *got_object_blob_create(struct got_object_id **,
const char *, struct got_repository *);
const struct got_error *got_object_tree_create(struct got_object_id **,
struct got_tree_entries *, struct got_repository *);
+const struct got_error *got_object_commit_create(struct got_object_id **,
+ struct got_object_id *, struct got_object_id_queue *, int,
+ const char *, time_t, const char *, time_t, const char *,
+ struct got_repository *);
diff --git a/lib/object_create.c b/lib/object_create.c
index 8c94148..6e0d389 100644
--- a/lib/object_create.c
+++ b/lib/object_create.c
@@ -304,3 +304,165 @@ done:
}
return err;
}
+
+const struct got_error *
+got_object_commit_create(struct got_object_id **id,
+ struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
+ int nparents, const char *author, time_t author_time,
+ const char *committer, time_t committer_time,
+ const char *logmsg, struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ SHA1_CTX sha1_ctx;
+ char *header = NULL, *tree_str = NULL;
+ char *author_str = NULL, *committer_str = NULL;
+ char *id_str = NULL;
+ size_t headerlen, len = 0, n;
+ FILE *commitfile = NULL;
+ struct got_object_qid *qid;
+
+ *id = NULL;
+
+ SHA1Init(&sha1_ctx);
+
+ if (asprintf(&author_str, "%s%s %lld +0000\n",
+ GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
+ return got_error_from_errno();
+
+ if (asprintf(&committer_str, "%s%s %lld +0000\n",
+ GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
+ committer ? committer_time : author_time)
+ == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
+ nparents *
+ (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
+ + strlen(author_str) + strlen(committer_str) + 2 + strlen(logmsg);
+
+ if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ headerlen = strlen(header) + 1;
+ SHA1Update(&sha1_ctx, header, headerlen);
+
+ commitfile = got_opentemp();
+ if (commitfile == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ n = fwrite(header, 1, headerlen, commitfile);
+ if (n != headerlen) {
+ err = got_ferror(commitfile, GOT_ERR_IO);
+ goto done;
+ }
+
+ err = got_object_id_str(&id_str, tree_id);
+ if (err)
+ goto done;
+ if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
+ == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ len = strlen(tree_str);
+ SHA1Update(&sha1_ctx, tree_str, len);
+ n = fwrite(tree_str, 1, len, commitfile);
+ if (n != len) {
+ err = got_ferror(commitfile, GOT_ERR_IO);
+ goto done;
+ }
+
+ SIMPLEQ_FOREACH(qid, parent_ids, entry) {
+ char *parent_str = NULL;
+
+ free(id_str);
+
+ err = got_object_id_str(&id_str, qid->id);
+ if (err)
+ goto done;
+ if (asprintf(&parent_str, "%s%s\n", GOT_COMMIT_LABEL_PARENT,
+ id_str) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ len = strlen(parent_str);
+ SHA1Update(&sha1_ctx, parent_str, len);
+ n = fwrite(parent_str, 1, len, commitfile);
+ if (n != len) {
+ err = got_ferror(commitfile, GOT_ERR_IO);
+ free(parent_str);
+ goto done;
+ }
+ free(parent_str);
+ }
+
+ len = strlen(author_str);
+ SHA1Update(&sha1_ctx, author_str, len);
+ n = fwrite(author_str, 1, len, commitfile);
+ if (n != len) {
+ err = got_ferror(commitfile, GOT_ERR_IO);
+ goto done;
+ }
+
+ len = strlen(committer_str);
+ SHA1Update(&sha1_ctx, committer_str, len);
+ n = fwrite(committer_str, 1, len, commitfile);
+ if (n != len) {
+ err = got_ferror(commitfile, GOT_ERR_IO);
+ goto done;
+ }
+
+ SHA1Update(&sha1_ctx, "\n", 1);
+ n = fwrite("\n", 1, 1, commitfile);
+ if (n != 1) {
+ err = got_ferror(commitfile, GOT_ERR_IO);
+ goto done;
+ }
+
+ len = strlen(logmsg);
+ SHA1Update(&sha1_ctx, logmsg, len);
+ n = fwrite(logmsg, 1, len, commitfile);
+ if (n != len) {
+ err = got_ferror(commitfile, GOT_ERR_IO);
+ goto done;
+ }
+
+ SHA1Update(&sha1_ctx, "\n", 1);
+ n = fwrite("\n", 1, 1, commitfile);
+ if (n != 1) {
+ err = got_ferror(commitfile, GOT_ERR_IO);
+ goto done;
+ }
+
+ *id = malloc(sizeof(**id));
+ if (*id == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ SHA1Final((*id)->sha1, &sha1_ctx);
+
+ if (fflush(commitfile) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ rewind(commitfile);
+
+ err = create_object_file(*id, commitfile, repo);
+done:
+ free(header);
+ free(tree_str);
+ free(author_str);
+ free(committer_str);
+ if (commitfile && fclose(commitfile) != 0 && err == NULL)
+ err = got_error_from_errno();
+ if (err) {
+ free(*id);
+ *id = NULL;
+ }
+ return err;
+}
diff --git a/lib/worktree.c b/lib/worktree.c
index 90a9746..1bd890d 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2598,6 +2598,7 @@ done:
const struct got_error *
got_worktree_commit(struct got_object_id **new_commit_id,
struct got_worktree *worktree, const char *ondisk_path,
+ const char *author, const char *committer,
const char *logmsg, struct got_repository *repo)
{
const struct got_error *err = NULL, *unlockerr = NULL;
@@ -2608,10 +2609,13 @@ got_worktree_commit(struct got_object_id **new_commit_id,
struct got_commit_object *base_commit = NULL;
struct got_tree_object *base_tree = NULL;
struct got_object_id *new_tree_id = NULL;
+ struct got_object_id_queue parent_ids;
+ struct got_object_qid *pid = NULL;
*new_commit_id = NULL;
TAILQ_INIT(&commitable_paths);
+ SIMPLEQ_INIT(&parent_ids);
if (ondisk_path) {
err = got_path_skip_common_ancestor(&relpath,
@@ -2632,6 +2636,8 @@ got_worktree_commit(struct got_object_id **new_commit_id,
if (err)
goto done;
+ /* TODO: out-of-dateness check */
+
/* TODO: collect commit message if not specified */
/* Create blobs from added and modified files and record their IDs. */
@@ -2667,8 +2673,13 @@ got_worktree_commit(struct got_object_id **new_commit_id,
if (err)
goto done;
- /* TODO: Write new commit. */
-
+ err = got_object_qid_alloc(&pid, worktree->base_commit_id);
+ if (err)
+ goto done;
+ SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
+ err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
+ 1, author, time(NULL), committer, time(NULL), logmsg, repo);
+ got_object_qid_free(pid);
done:
unlockerr = lock_worktree(worktree, LOCK_SH);
if (unlockerr && err == NULL)