Commit fc76cabb0c70b61d89701fce09ff96e538615379

Stefan Sperling 2018-12-25T15:24:16

store base commit of each blob in file index

diff --git a/lib/fileindex.c b/lib/fileindex.c
index 86250c1..a6bdde1 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -29,7 +29,8 @@
 
 const struct got_error *
 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
-    const char *ondisk_path, const char *relpath, uint8_t *blob_sha1)
+    const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
+    uint8_t *commit_sha1)
 {
 	struct stat sb;
 	size_t len;
@@ -63,6 +64,7 @@ got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
 	(*entry)->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
 	    GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
 	memcpy((*entry)->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
+	memcpy((*entry)->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
 	len = strlen(relpath);
 	if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
 		len = GOT_INDEX_ENTRY_F_PATH_LEN;
@@ -213,6 +215,11 @@ write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
 	if (n != SHA1_DIGEST_LENGTH)
 		return got_ferror(outfile, GOT_ERR_IO);
 
+	SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
+	n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
+	if (n != SHA1_DIGEST_LENGTH)
+		return got_ferror(outfile, GOT_ERR_IO);
+
 	err = write_fileindex_val32(ctx, entry->flags, outfile);
 	if (err)
 		return err;
@@ -383,6 +390,13 @@ read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
 	}
 	SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
 
+	n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
+	if (n != SHA1_DIGEST_LENGTH) {
+		err = got_ferror(infile, GOT_ERR_IO);
+		goto done;
+	}
+	SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
+
 	err = read_fileindex_val32(&entry->flags, ctx, infile);
 	if (err)
 		goto done;
diff --git a/lib/got_lib_fileindex.h b/lib/got_lib_fileindex.h
index d0855be..e8716e8 100644
--- a/lib/got_lib_fileindex.h
+++ b/lib/got_lib_fileindex.h
@@ -45,6 +45,9 @@ struct got_fileindex_entry {
 	/* SHA1 of corresponding blob in repository. */
 	uint8_t blob_sha1[SHA1_DIGEST_LENGTH];
 
+	/* SHA1 of corresponding base commit in repository. */
+	uint8_t commit_sha1[SHA1_DIGEST_LENGTH];
+
 	uint32_t flags;
 #define GOT_INDEX_ENTRY_F_PATH_LEN	0x00000fff
 #define GOT_INDEX_ENTRY_F_STAGE		0x00003000
@@ -83,7 +86,7 @@ struct got_fileindex_hdr {
 };
 
 const struct got_error *got_fileindex_entry_alloc(struct got_fileindex_entry **,
-    const char *, const char *, uint8_t *);
+    const char *, const char *, uint8_t *, uint8_t *);
 void got_fileindex_entry_free(struct got_fileindex_entry *);
 struct got_fileindex *got_fileindex_alloc(void);
 void got_fileindex_free(struct got_fileindex *);
diff --git a/lib/worktree.c b/lib/worktree.c
index 4656e2e..f8d6e1c 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -461,7 +461,8 @@ add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
 	fsync(fd);
 
 	err = got_fileindex_entry_alloc(&entry, ondisk_path,
-	    apply_path_prefix(worktree, path), blob->id.sha1);
+	    apply_path_prefix(worktree, path), blob->id.sha1,
+	    worktree->base_commit_id->sha1);
 	if (err)
 		goto done;