@matheusbsbs / matheus-bagliano / commits / 6de08ae688
Fork 0

Log ref updates to logs/refs/<ref>

If config parameter core.logAllRefUpdates is true or the log file already exists then append a line to ".git/logs/refs/<ref>" whenever git-update-ref <ref> is executed. Each log line contains the following information: oldsha1 <SP> newsha1 <SP> committer <LF> where committer is the current user, date, time and timezone in the standard GIT ident format. If the caller is unable to append to the log file then git-update-ref will fail without updating <ref>. An optional message may be included in the log line with the -m flag. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn Pearce committed May 17, 2006 at 05:55 UTC 6de08ae688b9f2426410add155079e04baff33bd
8 files changed +210
Documentation/config.txt
+8
@@ -70,6 +70,14 @@ core.preferSymlinkRefs::
70 This is sometimes needed to work with old scripts that
71 expect HEAD to be a symbolic link.
72
73 +core.logAllRefUpdates::
74 + If true, `git-update-ref` will append a line to
75 + "$GIT_DIR/logs/<ref>" listing the new SHA1 and the date/time
76 + of the update. If the file does not exist it will be
77 + created automatically. This information can be used to
78 + determine what commit was the tip of a branch "2 days ago".
79 + This value is false by default (no logging).
80 +
81 core.repositoryFormatVersion::
82 Internal variable identifying the repository format and layout
83 version.
Documentation/git-update-ref.txt
+26
@@ -49,6 +49,32 @@ for reading but not for writing (so we'll never write through a
49 ref symlink to some other tree, if you have copied a whole
50 archive by creating a symlink tree).
51
52 +Logging Updates
53 +---------------
54 +If config parameter "core.logAllRefUpdates" is true or the file
55 +"$GIT_DIR/logs/<ref>" exists then `git-update-ref` will append
56 +a line to the log file "$GIT_DIR/logs/<ref>" (dereferencing all
57 +symbolic refs before creating the log name) describing the change
58 +in ref value. Log lines are formatted as:
59 +
60 + . oldsha1 SP newsha1 SP committer LF
61 ++
62 +Where "oldsha1" is the 40 character hexadecimal value previously
63 +stored in <ref>, "newsha1" is the 40 character hexadecimal value of
64 +<newvalue> and "committer" is the committer's name, email address
65 +and date in the standard GIT committer ident format.
66 +
67 +Optionally with -m:
68 +
69 + . oldsha1 SP newsha1 SP committer TAB message LF
70 ++
71 +Where all fields are as described above and "message" is the
72 +value supplied to the -m option.
73 +
74 +An update will fail (without changing <ref>) if the current user is
75 +unable to create a new log file, append to the existing log file
76 +or does not have committer information available.
77 +
78 Author
79 ------
80 Written by Linus Torvalds <torvalds@osdl.org>.
cache.h
+1
@@ -170,6 +170,7 @@ extern void rollback_index_file(struct cache_file *);
170 extern int trust_executable_bit;
171 extern int assume_unchanged;
172 extern int prefer_symlink_refs;
173 +extern int log_all_ref_updates;
174 extern int warn_ambiguous_refs;
175 extern int diff_rename_limit_default;
176 extern int shared_repository;
config.c
+5
@@ -269,6 +269,11 @@ int git_default_config(const char *var, const char *value)
269 return 0;
270 }
271
272 + if (!strcmp(var, "core.logallrefupdates")) {
273 + log_all_ref_updates = git_config_bool(var, value);
274 + return 0;
275 + }
276 +
277 if (!strcmp(var, "core.warnambiguousrefs")) {
278 warn_ambiguous_refs = git_config_bool(var, value);
279 return 0;
environment.c
+1
@@ -14,6 +14,7 @@ char git_default_name[MAX_GITNAME];
14 int trust_executable_bit = 1;
15 int assume_unchanged = 0;
16 int prefer_symlink_refs = 0;
17 +int log_all_ref_updates = 0;
18 int warn_ambiguous_refs = 1;
19 int repository_format_version = 0;
20 char git_commit_encoding[MAX_ENCODING_LENGTH] = "utf-8";
refs.c
+56
@@ -302,6 +302,7 @@ static struct ref_lock* lock_ref_sha1_basic(const char *path,
302
303 lock->ref_file = strdup(path);
304 lock->lock_file = strdup(mkpath("%s.lock", lock->ref_file));
305 + lock->log_file = strdup(git_path("logs/%s", lock->ref_file + plen));
306
307 if (safe_create_leading_directories(lock->lock_file))
308 die("unable to create directory for %s", lock->lock_file);
@@ -343,9 +344,60 @@ void unlock_ref (struct ref_lock *lock)
344 free(lock->ref_file);
345 if (lock->lock_file)
346 free(lock->lock_file);
347 + if (lock->log_file)
348 + free(lock->log_file);
349 free(lock);
350 }
351
352 +static int log_ref_write(struct ref_lock *lock,
353 + const unsigned char *sha1, const char *msg)
354 +{
355 + int logfd, written, oflags = O_APPEND | O_WRONLY;
356 + unsigned maxlen, len;
357 + char *logrec;
358 + const char *comitter;
359 +
360 + if (log_all_ref_updates) {
361 + if (safe_create_leading_directories(lock->log_file) < 0)
362 + return error("unable to create directory for %s",
363 + lock->log_file);
364 + oflags |= O_CREAT;
365 + }
366 +
367 + logfd = open(lock->log_file, oflags, 0666);
368 + if (logfd < 0) {
369 + if (!log_all_ref_updates && errno == ENOENT)
370 + return 0;
371 + return error("Unable to append to %s: %s",
372 + lock->log_file, strerror(errno));
373 + }
374 +
375 + setup_ident();
376 + comitter = git_committer_info(1);
377 + if (msg) {
378 + maxlen = strlen(comitter) + strlen(msg) + 2*40 + 5;
379 + logrec = xmalloc(maxlen);
380 + len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
381 + sha1_to_hex(lock->old_sha1),
382 + sha1_to_hex(sha1),
383 + comitter,
384 + msg);
385 + } else {
386 + maxlen = strlen(comitter) + 2*40 + 4;
387 + logrec = xmalloc(maxlen);
388 + len = snprintf(logrec, maxlen, "%s %s %s\n",
389 + sha1_to_hex(lock->old_sha1),
390 + sha1_to_hex(sha1),
391 + comitter);
392 + }
393 + written = len <= maxlen ? write(logfd, logrec, len) : -1;
394 + free(logrec);
395 + close(logfd);
396 + if (written != len)
397 + return error("Unable to append to %s", lock->log_file);
398 + return 0;
399 +}
400 +
401 int write_ref_sha1(struct ref_lock *lock,
402 const unsigned char *sha1, const char *logmsg)
403 {
@@ -364,6 +416,10 @@ int write_ref_sha1(struct ref_lock *lock,
416 unlock_ref(lock);
417 return -1;
418 }
419 + if (log_ref_write(lock, sha1, logmsg) < 0) {
420 + unlock_ref(lock);
421 + return -1;
422 + }
423 if (rename(lock->lock_file, lock->ref_file) < 0) {
424 error("Couldn't set %s", lock->ref_file);
425 unlock_ref(lock);
refs.h
+1
@@ -4,6 +4,7 @@
4 struct ref_lock {
5 char *ref_file;
6 char *lock_file;
7 + char *log_file;
8 unsigned char old_sha1[20];
9 int lock_fd;
10 };
t/t1400-update-ref.sh new
+112
@@ -0,0 +1,112 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2006 Shawn Pearce
4 +#
5 +
6 +test_description='Test git-update-ref and basic ref logging'
7 +. ./test-lib.sh
8 +
9 +Z=0000000000000000000000000000000000000000
10 +A=1111111111111111111111111111111111111111
11 +B=2222222222222222222222222222222222222222
12 +m=refs/heads/master
13 +
14 +test_expect_success \
15 + "create $m" \
16 + 'git-update-ref $m $A &&
17 + test $A = $(cat .git/$m)'
18 +test_expect_success \
19 + "create $m" \
20 + 'git-update-ref $m $B $A &&
21 + test $B = $(cat .git/$m)'
22 +rm -f .git/$m
23 +
24 +test_expect_success \
25 + "create $m (by HEAD)" \
26 + 'git-update-ref HEAD $A &&
27 + test $A = $(cat .git/$m)'
28 +test_expect_success \
29 + "create $m (by HEAD)" \
30 + 'git-update-ref HEAD $B $A &&
31 + test $B = $(cat .git/$m)'
32 +rm -f .git/$m
33 +
34 +test_expect_failure \
35 + '(not) create HEAD with old sha1' \
36 + 'git-update-ref HEAD $A $B'
37 +test_expect_failure \
38 + "(not) prior created .git/$m" \
39 + 'test -f .git/$m'
40 +rm -f .git/$m
41 +
42 +test_expect_success \
43 + "create HEAD" \
44 + 'git-update-ref HEAD $A'
45 +test_expect_failure \
46 + '(not) change HEAD with wrong SHA1' \
47 + 'git-update-ref HEAD $B $Z'
48 +test_expect_failure \
49 + "(not) changed .git/$m" \
50 + 'test $B = $(cat .git/$m)'
51 +rm -f .git/$m
52 +
53 +mkdir -p .git/logs/refs/heads
54 +touch .git/logs/refs/heads/master
55 +test_expect_success \
56 + "create $m (logged by touch)" \
57 + 'GIT_COMMITTER_DATE="2005-05-26 23:30" \
58 + git-update-ref HEAD $A -m "Initial Creation" &&
59 + test $A = $(cat .git/$m)'
60 +test_expect_success \
61 + "update $m (logged by touch)" \
62 + 'GIT_COMMITTER_DATE="2005-05-26 23:31" \
63 + git-update-ref HEAD $B $A -m "Switch" &&
64 + test $B = $(cat .git/$m)'
65 +test_expect_success \
66 + "set $m (logged by touch)" \
67 + 'GIT_COMMITTER_DATE="2005-05-26 23:41" \
68 + git-update-ref HEAD $A &&
69 + test $A = $(cat .git/$m)'
70 +
71 +cat >expect <<EOF
72 +$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creation
73 +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch
74 +$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
75 +EOF
76 +test_expect_success \
77 + "verifying $m's log" \
78 + 'diff expect .git/logs/$m'
79 +rm -rf .git/$m .git/logs expect
80 +
81 +test_expect_success \
82 + 'enable core.logAllRefUpdates' \
83 + 'git-repo-config core.logAllRefUpdates true &&
84 + test true = $(git-repo-config --bool --get core.logAllRefUpdates)'
85 +
86 +test_expect_success \
87 + "create $m (logged by config)" \
88 + 'GIT_COMMITTER_DATE="2005-05-26 23:32" \
89 + git-update-ref HEAD $A -m "Initial Creation" &&
90 + test $A = $(cat .git/$m)'
91 +test_expect_success \
92 + "update $m (logged by config)" \
93 + 'GIT_COMMITTER_DATE="2005-05-26 23:33" \
94 + git-update-ref HEAD $B $A -m "Switch" &&
95 + test $B = $(cat .git/$m)'
96 +test_expect_success \
97 + "set $m (logged by config)" \
98 + 'GIT_COMMITTER_DATE="2005-05-26 23:43" \
99 + git-update-ref HEAD $A &&
100 + test $A = $(cat .git/$m)'
101 +
102 +cat >expect <<EOF
103 +$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000 Initial Creation
104 +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000 Switch
105 +$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
106 +EOF
107 +test_expect_success \
108 + "verifying $m's log" \
109 + 'diff expect .git/logs/$m'
110 +rm -f .git/$m .git/logs/$m expect
111 +
112 +test_done