[PATCH 3/5] Start splitting out git repo helper routines

Linus Torvalds torvalds at linux-foundation.org
Sun Feb 8 19:14:54 PST 2015


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Fri, 23 Jan 2015 15:40:01 +1200
Subject: [PATCH 3/5] Start splitting out git repo helper routines

This doesn't actually change any code, but it moves the 'is_git_repo()'
function that is used by both loading and saving into a new git-access.c
file.

This is where I'll start doing remote repo syncing too. Knock wood.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---
 CMakeLists.txt |  1 +
 git-access.c   | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 save-git.c     | 67 ------------------------------------------------
 subsurface.pro |  1 +
 4 files changed, 82 insertions(+), 67 deletions(-)
 create mode 100644 git-access.c

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4a260e729505..16b8b9f52406 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -103,6 +103,7 @@ SET(SUBSURFACE_CORE_LIB_SRCS
 	divelist.c
 	equipment.c
 	file.c
+	git-access.c
 	libdivecomputer.c
 	liquivision.c
 	load-git.c
diff --git a/git-access.c b/git-access.c
new file mode 100644
index 000000000000..3b01623b9897
--- /dev/null
+++ b/git-access.c
@@ -0,0 +1,80 @@
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <git2.h>
+
+#include "dive.h"
+
+/*
+ * If it's not a git repo, return NULL. Be very conservative.
+ */
+struct git_repository *is_git_repository(const char *filename, const char **branchp)
+{
+	int flen, blen, ret;
+	struct stat st;
+	git_repository *repo;
+	char *loc, *branch;
+
+	flen = strlen(filename);
+	if (!flen || filename[--flen] != ']')
+		return NULL;
+
+	/* Find the matching '[' */
+	blen = 0;
+	while (flen && filename[--flen] != '[')
+		blen++;
+
+	if (!flen)
+		return NULL;
+
+	/*
+	 * This is the "point of no return": the name matches
+	 * the git repository name rules, and we will no longer
+	 * return NULL.
+	 *
+	 * We will either return "dummy_git_repository" and the
+	 * branch pointer will have the _whole_ filename in it,
+	 * or we will return a real git repository with the
+	 * branch pointer being filled in with just the branch
+	 * name.
+	 *
+	 * The actual git reading/writing routines can use this
+	 * to generate proper error messages.
+	 */
+	*branchp = filename;
+	loc = malloc(flen+1);
+	if (!loc)
+		return dummy_git_repository;
+	memcpy(loc, filename, flen);
+	loc[flen] = 0;
+
+	branch = malloc(blen+1);
+	if (!branch) {
+		free(loc);
+		return dummy_git_repository;
+	}
+	memcpy(branch, filename+flen+1, blen);
+	branch[blen] = 0;
+
+	if (stat(loc, &st) < 0 || !S_ISDIR(st.st_mode)) {
+		free(loc);
+		free(branch);
+		return dummy_git_repository;
+	}
+
+	ret = git_repository_open(&repo, loc);
+	free(loc);
+	if (ret < 0) {
+		free(branch);
+		return dummy_git_repository;
+	}
+	*branchp = branch;
+	return repo;
+}
diff --git a/save-git.c b/save-git.c
index 6c2587bf8643..4a4d06655da1 100644
--- a/save-git.c
+++ b/save-git.c
@@ -1094,73 +1094,6 @@ static int do_git_save(git_repository *repo, const char *branch, bool select_onl
 	return create_new_commit(repo, branch, &id);
 }
 
-/*
- * If it's not a git repo, return NULL. Be very conservative.
- */
-struct git_repository *is_git_repository(const char *filename, const char **branchp)
-{
-	int flen, blen, ret;
-	struct stat st;
-	git_repository *repo;
-	char *loc, *branch;
-
-	flen = strlen(filename);
-	if (!flen || filename[--flen] != ']')
-		return NULL;
-
-	/* Find the matching '[' */
-	blen = 0;
-	while (flen && filename[--flen] != '[')
-		blen++;
-
-	if (!flen)
-		return NULL;
-
-	/*
-	 * This is the "point of no return": the name matches
-	 * the git repository name rules, and we will no longer
-	 * return NULL.
-	 *
-	 * We will either return "dummy_git_repository" and the
-	 * branch pointer will have the _whole_ filename in it,
-	 * or we will return a real git repository with the
-	 * branch pointer being filled in with just the branch
-	 * name.
-	 *
-	 * The actual git reading/writing routines can use this
-	 * to generate proper error messages.
-	 */
-	*branchp = filename;
-	loc = malloc(flen+1);
-	if (!loc)
-		return dummy_git_repository;
-	memcpy(loc, filename, flen);
-	loc[flen] = 0;
-
-	branch = malloc(blen+1);
-	if (!branch) {
-		free(loc);
-		return dummy_git_repository;
-	}
-	memcpy(branch, filename+flen+1, blen);
-	branch[blen] = 0;
-
-	if (stat(loc, &st) < 0 || !S_ISDIR(st.st_mode)) {
-		free(loc);
-		free(branch);
-		return dummy_git_repository;
-	}
-
-	ret = git_repository_open(&repo, loc);
-	free(loc);
-	if (ret < 0) {
-		free(branch);
-		return dummy_git_repository;
-	}
-	*branchp = branch;
-	return repo;
-}
-
 int git_save_dives(struct git_repository *repo, const char *branch, bool select_only)
 {
 	int ret;
diff --git a/subsurface.pro b/subsurface.pro
index 2cc6b0982e23..c20fa614370d 100644
--- a/subsurface.pro
+++ b/subsurface.pro
@@ -121,6 +121,7 @@ SOURCES =  \
 	equipment.c \
 	file.c \
 	gettextfromc.cpp \
+	git-access.c \
 	libdivecomputer.c \
 	liquivision.c \
 	load-git.c \
-- 
2.3.0.rc2.2.g0d1c285



More information about the subsurface mailing list