[PATCH 2/5] refs: split log_ref_write logic into log_ref_setup

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Separation of the logic for testing and preparing the reflogs from
function log_ref_write to a new non static new function: log_ref_setup.

This allows to be performed from outside the first all reasonable checks
and procedures for writing reflogs.
---
 refs.c |   57 ++++++++++++++++++++++++++++++++++++---------------------
 refs.h |    3 +++
 2 files changed, 39 insertions(+), 21 deletions(-)

diff --git a/refs.c b/refs.c
index d3db15a..1161c2d 100644
--- a/refs.c
+++ b/refs.c
@@ -1258,52 +1258,67 @@ static int copy_msg(char *buf, const char *msg)
 	return cp - buf;
 }
 
-static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
-			 const unsigned char *new_sha1, const char *msg)
+int log_ref_setup(const char *ref_name, char **log_file)
 {
-	int logfd, written, oflags = O_APPEND | O_WRONLY;
-	unsigned maxlen, len;
-	int msglen;
-	char log_file[PATH_MAX];
-	char *logrec;
-	const char *committer;
-
-	if (log_all_ref_updates < 0)
-		log_all_ref_updates = !is_bare_repository();
-
-	git_snpath(log_file, sizeof(log_file), "logs/%s", ref_name);
+	int logfd, oflags = O_APPEND | O_WRONLY;
+	char logfile[PATH_MAX];
 
+	git_snpath(logfile, sizeof(logfile), "logs/%s", ref_name);
+	*log_file = logfile;
 	if (log_all_ref_updates &&
 	    (!prefixcmp(ref_name, "refs/heads/") ||
 	     !prefixcmp(ref_name, "refs/remotes/") ||
 	     !prefixcmp(ref_name, "refs/notes/") ||
 	     !strcmp(ref_name, "HEAD"))) {
-		if (safe_create_leading_directories(log_file) < 0)
+		if (safe_create_leading_directories(*log_file) < 0)
 			return error("unable to create directory for %s",
-				     log_file);
+				     *log_file);
 		oflags |= O_CREAT;
 	}
 
-	logfd = open(log_file, oflags, 0666);
+	logfd = open(*log_file, oflags, 0666);
 	if (logfd < 0) {
 		if (!(oflags & O_CREAT) && errno == ENOENT)
 			return 0;
 
 		if ((oflags & O_CREAT) && errno == EISDIR) {
-			if (remove_empty_directories(log_file)) {
+			if (remove_empty_directories(*log_file)) {
 				return error("There are still logs under '%s'",
-					     log_file);
+					     *log_file);
 			}
-			logfd = open(log_file, oflags, 0666);
+			logfd = open(*log_file, oflags, 0666);
 		}
 
 		if (logfd < 0)
 			return error("Unable to append to %s: %s",
-				     log_file, strerror(errno));
+				     *log_file, strerror(errno));
 	}
 
-	adjust_shared_perm(log_file);
+	adjust_shared_perm(*log_file);
+	close(logfd);
+	return 0;
+}
 
+static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
+			 const unsigned char *new_sha1, const char *msg)
+{
+	int logfd, result, written, oflags = O_APPEND | O_WRONLY;
+	unsigned maxlen, len;
+	int msglen;
+	char *log_file;
+	char *logrec;
+	const char *committer;
+
+	if (log_all_ref_updates < 0)
+		log_all_ref_updates = !is_bare_repository();
+
+	result = log_ref_setup(ref_name, &log_file);
+	if (result)
+		return result;
+
+	logfd = open(log_file, oflags);
+	if (logfd < 0)
+		return 0;
 	msglen = msg ? strlen(msg) : 0;
 	committer = git_committer_info(0);
 	maxlen = strlen(committer) + msglen + 100;
diff --git a/refs.h b/refs.h
index 4a18b08..594c9d9 100644
--- a/refs.h
+++ b/refs.h
@@ -68,6 +68,9 @@ extern void unlock_ref(struct ref_lock *lock);
 /** Writes sha1 into the ref specified by the lock. **/
 extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
 
+/** Setup reflog before using. **/
+int log_ref_setup(const char *ref_name, char **log_file);
+
 /** Reads log for the value of ref during at_time. **/
 extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1, char **msg, unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
 
-- 
1.7.1.231.g0687c.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]