refs: split log_ref_write logic into log_ref_setup
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. Signed-off-by: Erick Mattos <erick.mattos@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Erick Mattos committed
May 21, 2010 at 21:28 UTC
859c30175f7d8f64f61968cb3ece7aca9218b314
2 files changed
+39
-21
refs.c
+36
-21
@@ -1258,52 +1258,67 @@ static int copy_msg(char *buf, const char *msg)
1258
return cp - buf;
1259
}
1260
1261
-static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
1262
- const unsigned char *new_sha1, const char *msg)
1261
+int log_ref_setup(const char *ref_name, char **log_file)
1262
{
1264
- int logfd, written, oflags = O_APPEND | O_WRONLY;
1265
- unsigned maxlen, len;
1266
- int msglen;
1267
- char log_file[PATH_MAX];
1268
- char *logrec;
1269
- const char *committer;
1270
-
1271
- if (log_all_ref_updates < 0)
1272
- log_all_ref_updates = !is_bare_repository();
1273
-
1274
- git_snpath(log_file, sizeof(log_file), "logs/%s", ref_name);
1263
+ int logfd, oflags = O_APPEND | O_WRONLY;
1264
+ char logfile[PATH_MAX];
1265
1266
+ git_snpath(logfile, sizeof(logfile), "logs/%s", ref_name);
1267
+ *log_file = logfile;
1268
if (log_all_ref_updates &&
1269
(!prefixcmp(ref_name, "refs/heads/") ||
1270
!prefixcmp(ref_name, "refs/remotes/") ||
1271
!prefixcmp(ref_name, "refs/notes/") ||
1272
!strcmp(ref_name, "HEAD"))) {
1281
- if (safe_create_leading_directories(log_file) < 0)
1273
+ if (safe_create_leading_directories(*log_file) < 0)
1274
return error("unable to create directory for %s",
1283
- log_file);
1275
+ *log_file);
1276
oflags |= O_CREAT;
1277
}
1278
1287
- logfd = open(log_file, oflags, 0666);
1279
+ logfd = open(*log_file, oflags, 0666);
1280
if (logfd < 0) {
1281
if (!(oflags & O_CREAT) && errno == ENOENT)
1282
return 0;
1283
1284
if ((oflags & O_CREAT) && errno == EISDIR) {
1293
- if (remove_empty_directories(log_file)) {
1285
+ if (remove_empty_directories(*log_file)) {
1286
return error("There are still logs under '%s'",
1295
- log_file);
1287
+ *log_file);
1288
}
1297
- logfd = open(log_file, oflags, 0666);
1289
+ logfd = open(*log_file, oflags, 0666);
1290
}
1291
1292
if (logfd < 0)
1293
return error("Unable to append to %s: %s",
1302
- log_file, strerror(errno));
1294
+ *log_file, strerror(errno));
1295
}
1296
1305
- adjust_shared_perm(log_file);
1297
+ adjust_shared_perm(*log_file);
1298
+ close(logfd);
1299
+ return 0;
1300
+}
1301
1302
+static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
1303
+ const unsigned char *new_sha1, const char *msg)
1304
+{
1305
+ int logfd, result, written, oflags = O_APPEND | O_WRONLY;
1306
+ unsigned maxlen, len;
1307
+ int msglen;
1308
+ char *log_file;
1309
+ char *logrec;
1310
+ const char *committer;
1311
+
1312
+ if (log_all_ref_updates < 0)
1313
+ log_all_ref_updates = !is_bare_repository();
1314
+
1315
+ result = log_ref_setup(ref_name, &log_file);
1316
+ if (result)
1317
+ return result;
1318
+
1319
+ logfd = open(log_file, oflags);
1320
+ if (logfd < 0)
1321
+ return 0;
1322
msglen = msg ? strlen(msg) : 0;
1323
committer = git_committer_info(0);
1324
maxlen = strlen(committer) + msglen + 100;
refs.h
+3
@@ -68,6 +68,9 @@ extern void unlock_ref(struct ref_lock *lock);
68
/** Writes sha1 into the ref specified by the lock. **/
69
extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
70
71
+/** Setup reflog before using. **/
72
+int log_ref_setup(const char *ref_name, char **log_file);
73
+
74
/** Reads log for the value of ref during at_time. **/
75
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);
76