make reflog filename independent from struct ref_lock
This allows for ref_log_write() to be used in a more flexible way, and is needed for future changes. This is only code reorg with no behavior change. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nicolas Pitre committed
Jan 26, 2007 at 17:26 UTC
9a13f0b71b887af42c3be854344f185c6dfa1d0d
3 files changed
+26
-25
builtin-reflog.c
+6
-4
@@ -242,7 +242,7 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
242
struct cmd_reflog_expire_cb *cmd = cb_data;
243
struct expire_reflog_cb cb;
244
struct ref_lock *lock;
245
- char *newlog_path = NULL;
245
+ char *log_file, *newlog_path = NULL;
246
int status = 0;
247
248
if (strncmp(ref, "refs/", 5))
@@ -255,7 +255,8 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
255
lock = lock_ref_sha1(ref + 5, sha1);
256
if (!lock)
257
return error("cannot lock ref '%s'", ref);
258
- if (!file_exists(lock->log_file))
258
+ log_file = xstrdup(git_path("logs/%s", ref));
259
+ if (!file_exists(log_file))
260
goto finish;
261
if (!cmd->dry_run) {
262
newlog_path = xstrdup(git_path("logs/%s.lock", ref));
@@ -271,13 +272,14 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
272
if (fclose(cb.newlog))
273
status |= error("%s: %s", strerror(errno),
274
newlog_path);
274
- if (rename(newlog_path, lock->log_file)) {
275
+ if (rename(newlog_path, log_file)) {
276
status |= error("cannot rename %s to %s",
276
- newlog_path, lock->log_file);
277
+ newlog_path, log_file);
278
unlink(newlog_path);
279
}
280
}
281
free(newlog_path);
282
+ free(log_file);
283
unlock_ref(lock);
284
return status;
285
}
refs.c
+20
-20
@@ -680,7 +680,6 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
680
lock->lk = xcalloc(1, sizeof(struct lock_file));
681
682
lock->ref_name = xstrdup(ref);
683
- lock->log_file = xstrdup(git_path("logs/%s", ref));
683
ref_file = git_path("%s", ref);
684
lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
685
@@ -776,10 +775,10 @@ int delete_ref(const char *refname, unsigned char *sha1)
775
*/
776
ret |= repack_without_ref(refname);
777
779
- err = unlink(lock->log_file);
778
+ err = unlink(git_path("logs/%s", lock->ref_name));
779
if (err && errno != ENOENT)
780
fprintf(stderr, "warning: unlink(%s) failed: %s",
782
- lock->log_file, strerror(errno));
781
+ git_path("logs/%s", lock->ref_name), strerror(errno));
782
invalidate_cached_refs();
783
unlock_ref(lock);
784
return ret;
@@ -920,47 +919,48 @@ void unlock_ref(struct ref_lock *lock)
919
rollback_lock_file(lock->lk);
920
}
921
free(lock->ref_name);
923
- free(lock->log_file);
922
free(lock);
923
}
924
927
-static int log_ref_write(struct ref_lock *lock,
928
- const unsigned char *sha1, const char *msg)
925
+static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
926
+ const unsigned char *new_sha1, const char *msg)
927
{
928
int logfd, written, oflags = O_APPEND | O_WRONLY;
929
unsigned maxlen, len;
930
int msglen;
933
- char *logrec;
931
+ char *log_file, *logrec;
932
const char *committer;
933
934
if (log_all_ref_updates < 0)
935
log_all_ref_updates = !is_bare_repository();
936
937
+ log_file = git_path("logs/%s", ref_name);
938
+
939
if (log_all_ref_updates &&
940
- (!strncmp(lock->ref_name, "refs/heads/", 11) ||
941
- !strncmp(lock->ref_name, "refs/remotes/", 13))) {
942
- if (safe_create_leading_directories(lock->log_file) < 0)
940
+ (!strncmp(ref_name, "refs/heads/", 11) ||
941
+ !strncmp(ref_name, "refs/remotes/", 13))) {
942
+ if (safe_create_leading_directories(log_file) < 0)
943
return error("unable to create directory for %s",
944
- lock->log_file);
944
+ log_file);
945
oflags |= O_CREAT;
946
}
947
948
- logfd = open(lock->log_file, oflags, 0666);
948
+ logfd = open(log_file, oflags, 0666);
949
if (logfd < 0) {
950
if (!(oflags & O_CREAT) && errno == ENOENT)
951
return 0;
952
953
if ((oflags & O_CREAT) && errno == EISDIR) {
954
- if (remove_empty_directories(lock->log_file)) {
954
+ if (remove_empty_directories(log_file)) {
955
return error("There are still logs under '%s'",
956
- lock->log_file);
956
+ log_file);
957
}
958
- logfd = open(lock->log_file, oflags, 0666);
958
+ logfd = open(log_file, oflags, 0666);
959
}
960
961
if (logfd < 0)
962
return error("Unable to append to %s: %s",
963
- lock->log_file, strerror(errno));
963
+ log_file, strerror(errno));
964
}
965
966
msglen = 0;
@@ -982,8 +982,8 @@ static int log_ref_write(struct ref_lock *lock,
982
maxlen = strlen(committer) + msglen + 100;
983
logrec = xmalloc(maxlen);
984
len = sprintf(logrec, "%s %s %s\n",
985
- sha1_to_hex(lock->old_sha1),
986
- sha1_to_hex(sha1),
985
+ sha1_to_hex(old_sha1),
986
+ sha1_to_hex(new_sha1),
987
committer);
988
if (msglen)
989
len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
@@ -991,7 +991,7 @@ static int log_ref_write(struct ref_lock *lock,
991
free(logrec);
992
close(logfd);
993
if (written != len)
994
- return error("Unable to append to %s", lock->log_file);
994
+ return error("Unable to append to %s", log_file);
995
return 0;
996
}
997
@@ -1014,7 +1014,7 @@ int write_ref_sha1(struct ref_lock *lock,
1014
return -1;
1015
}
1016
invalidate_cached_refs();
1017
- if (log_ref_write(lock, sha1, logmsg) < 0) {
1017
+ if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0) {
1018
unlock_ref(lock);
1019
return -1;
1020
}
refs.h
-1
@@ -3,7 +3,6 @@
3
4
struct ref_lock {
5
char *ref_name;
6
- char *log_file;
6
struct lock_file *lk;
7
unsigned char old_sha1[20];
8
int lock_fd;