]> asedeno.scripts.mit.edu Git - git.git/blobdiff - refs.c
Replace mmap with xmmap, better handling MAP_FAILED.
[git.git] / refs.c
diff --git a/refs.c b/refs.c
index d911b9e86009aa3d81c6c4d310a26bf057920e8a..121774cb328a4d8a773a6a870c642a030bf3752d 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -1,10 +1,8 @@
-#include "refs.h"
 #include "cache.h"
+#include "refs.h"
 #include "object.h"
 #include "tag.h"
 
-#include <errno.h>
-
 /* ISSYMREF=01 and ISPACKED=02 are public interfaces */
 #define REF_KNOWS_PEELED 04
 
@@ -927,7 +925,8 @@ static int log_ref_write(struct ref_lock *lock,
        const char *committer;
 
        if (log_all_ref_updates &&
-           !strncmp(lock->ref_name, "refs/heads/", 11)) {
+           (!strncmp(lock->ref_name, "refs/heads/", 11) ||
+            !strncmp(lock->ref_name, "refs/remotes/", 13))) {
                if (safe_create_leading_directories(lock->log_file) < 0)
                        return error("unable to create directory for %s",
                                lock->log_file);
@@ -1015,7 +1014,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
 {
        const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
        char *tz_c;
-       int logfd, tz;
+       int logfd, tz, reccnt = 0;
        struct stat st;
        unsigned long date;
        unsigned char logged_sha1[20];
@@ -1027,12 +1026,13 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
        fstat(logfd, &st);
        if (!st.st_size)
                die("Log %s is empty.", logfile);
-       logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
+       logdata = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
        close(logfd);
 
        lastrec = NULL;
        rec = logend = logdata + st.st_size;
        while (logdata < rec) {
+               reccnt++;
                if (logdata < rec && *(rec-1) == '\n')
                        rec--;
                lastgt = NULL;
@@ -1089,7 +1089,37 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
        if (get_sha1_hex(logdata, sha1))
                die("Log %s is corrupt.", logfile);
        munmap((void*)logdata, st.st_size);
-       fprintf(stderr, "warning: Log %s only goes back to %s.\n",
-               logfile, show_rfc2822_date(date, tz));
+       if (at_time)
+               fprintf(stderr, "warning: Log %s only goes back to %s.\n",
+                       logfile, show_rfc2822_date(date, tz));
+       else
+               fprintf(stderr, "warning: Log %s only has %d entries.\n",
+                       logfile, reccnt);
        return 0;
 }
+
+void for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
+{
+       const char *logfile;
+       FILE *logfp;
+       char buf[1024];
+
+       logfile = git_path("logs/%s", ref);
+       logfp = fopen(logfile, "r");
+       if (!logfp)
+               return;
+       while (fgets(buf, sizeof(buf), logfp)) {
+               unsigned char osha1[20], nsha1[20];
+               int len;
+
+               /* old SP new SP name <email> SP time TAB msg LF */
+               len = strlen(buf);
+               if (len < 83 || buf[len-1] != '\n' ||
+                   get_sha1_hex(buf, osha1) || buf[40] != ' ' ||
+                   get_sha1_hex(buf + 41, nsha1) || buf[81] != ' ')
+                       continue; /* corrupt? */
+               fn(osha1, nsha1, buf+82, cb_data);
+       }
+       fclose(logfp);
+}
+