[PATCH 01/12] Enumerate dive computers when saving them in the git repository

Linus Torvalds torvalds at linux-foundation.org
Sun Mar 9 18:41:22 PDT 2014



From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Sun, 9 Mar 2014 13:33:28 -0700
Subject: [PATCH 01/12] Enumerate dive computers when saving them in the git repository

We want to make sure that we load them in the same order we save them,
and while using the hash made the divecomputer names unique, it didn't
sort them.  You couldn't tell with just one or two dive computers, but
if you have three or more dive computers on a dive, the order of any but
the first ended up depending on the ordering of the unique hash
extensions.

So just append a numeric index instead of relying on the hash to make
the names unique.  But skip the index if there is just one dive
computer.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---
 save-git.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/save-git.c b/save-git.c
index 0f9dea95e3db..7b121a19b7e6 100644
--- a/save-git.c
+++ b/save-git.c
@@ -469,13 +469,13 @@ static int blob_insert(git_repository *repo, struct dir *tree, struct membuffer
 	return ret;
 }
 
-static int save_one_divecomputer(git_repository *repo, struct dir *tree, struct dive *dive, struct divecomputer *dc)
+static int save_one_divecomputer(git_repository *repo, struct dir *tree, struct dive *dive, struct divecomputer *dc, int idx)
 {
 	int ret;
 	struct membuffer buf = { 0 };
 
 	save_dc(&buf, dive, dc);
-	ret = blob_insert(repo, tree, &buf, "Divecomputer");
+	ret = blob_insert(repo, tree, &buf, "Divecomputer%c%03u", idx ? '-' : 0, idx);
 	if (ret)
 		report_error("divecomputer tree insert failed");
 	return ret;
@@ -501,10 +501,15 @@ static int save_one_dive(git_repository *repo, struct dir *tree, struct dive *di
 	if (ret)
 		return report_error("dive save-file tree insert failed");
 
-	/* Save the dive computer data */
+	/*
+	 * Save the dive computer data. If there is only one dive
+	 * computer, use index 0 for that (which disables the index
+	 * generation when naming it).
+	 */
 	dc = &dive->dc;
+	nr = dc->next ? 1 : 0;
 	do {
-		save_one_divecomputer(repo, subdir, dive, dc);
+		save_one_divecomputer(repo, subdir, dive, dc, nr++);
 		dc = dc->next;
 	} while (dc);
 
-- 
1.8.5.3



More information about the subsurface mailing list