rev-tree.con commit [PATCH] Simplify date handling and make it more reliable (27de946)
   1#define _XOPEN_SOURCE /* glibc2 needs this */
   2#define _BSD_SOURCE /* for tm.tm_gmtoff */
   3#include <time.h>
   4#include <ctype.h>
   5
   6#include "cache.h"
   7#include "revision.h"
   8
   9/*
  10 * revision.h leaves the low 16 bits of the "flags" field of the
  11 * revision data structure unused. We use it for a "reachable from
  12 * this commit <N>" bitmask.
  13 */
  14#define MAX_COMMITS 16
  15
  16static int show_edges = 0;
  17static int basemask = 0;
  18
  19static unsigned long parse_time(const char *buf)
  20{
  21        char c, *p;
  22        char buffer[100];
  23        struct tm tm;
  24        const char *formats[] = {
  25                "%s",
  26                "%c",
  27                "%a %b %d %T %y",
  28                NULL
  29        };
  30        const char **fmt = formats;
  31
  32        p = buffer;
  33        while (isspace(c = *buf))
  34                buf++;
  35        while ((c = *buf++) != '\n' && c)
  36                *p++ = c;
  37        *p++ = 0;
  38        buf = buffer;
  39        memset(&tm, 0, sizeof(tm));
  40        do {
  41                const char *next = strptime(buf, *fmt, &tm);
  42                fmt++;
  43                if (next) {
  44                        if (!*next)
  45                                return mktime(&tm);
  46                        buf = next;
  47                }
  48        } while (*buf && *fmt);
  49        return mktime(&tm);
  50}
  51                
  52
  53static unsigned long parse_commit_date(const char *buf)
  54{
  55        unsigned long time;
  56
  57        if (memcmp(buf, "author", 6))
  58                return 0;
  59        while (*buf++ != '\n')
  60                /* nada */;
  61        if (memcmp(buf, "committer", 9))
  62                return 0;
  63        while (*buf++ != '>')
  64                /* nada */;
  65
  66        time = strtoul(buf, NULL, 10);
  67        if (!time)
  68                time = parse_time(buf);
  69        return time;
  70}
  71
  72static int parse_commit(unsigned char *sha1)
  73{
  74        struct revision *rev = lookup_rev(sha1);
  75
  76        if (!(rev->flags & SEEN)) {
  77                void *buffer, *bufptr;
  78                unsigned long size;
  79                char type[20];
  80                unsigned char parent[20];
  81
  82                rev->flags |= SEEN;
  83                buffer = bufptr = read_sha1_file(sha1, type, &size);
  84                if (!buffer || strcmp(type, "commit"))
  85                        return -1;
  86                bufptr += 46; /* "tree " + "hex sha1" + "\n" */
  87                while (!memcmp(bufptr, "parent ", 7) && !get_sha1_hex(bufptr+7, parent)) {
  88                        add_relationship(rev, parent);
  89                        parse_commit(parent);
  90                        bufptr += 48;   /* "parent " + "hex sha1" + "\n" */
  91                }
  92                rev->date = parse_commit_date(bufptr);
  93                free(buffer);
  94        }
  95        return 0;       
  96}
  97
  98static void read_cache_file(const char *path)
  99{
 100        FILE *file = fopen(path, "r");
 101        char line[500];
 102
 103        if (!file)
 104                die("bad revtree cache file (%s)", path);
 105
 106        while (fgets(line, sizeof(line), file)) {
 107                unsigned long date;
 108                unsigned char sha1[20];
 109                struct revision *rev;
 110                const char *buf;
 111
 112                if (sscanf(line, "%lu", &date) != 1)
 113                        break;
 114                buf = strchr(line, ' ');
 115                if (!buf)
 116                        break;
 117                if (get_sha1_hex(buf+1, sha1))
 118                        break;
 119                rev = lookup_rev(sha1);
 120                rev->flags |= SEEN;
 121                rev->date = date;
 122
 123                /* parents? */
 124                while ((buf = strchr(buf+1, ' ')) != NULL) {
 125                        unsigned char parent[20];
 126                        if (get_sha1_hex(buf + 1, parent))
 127                                break;
 128                        add_relationship(rev, parent);
 129                }
 130        }
 131        fclose(file);
 132}
 133
 134static void mark_sha1_path(struct revision *rev, unsigned int mask)
 135{
 136        struct parent *p;
 137
 138        if (rev->flags & mask)
 139                return;
 140
 141        rev->flags |= mask;
 142        p = rev->parent;
 143        while (p) {
 144                mark_sha1_path(p->parent, mask);
 145                p = p->next;
 146        }
 147}
 148
 149/*
 150 * Some revisions are less interesting than others.
 151 *
 152 * For example, if we use a cache-file, that one may contain
 153 * revisions that were never used. They are never interesting.
 154 *
 155 * And sometimes we're only interested in "edge" commits, ie
 156 * places where the marking changes between parent and child.
 157 */
 158static int interesting(struct revision *rev)
 159{
 160        unsigned mask = marked(rev);
 161
 162        if (!mask)
 163                return 0;
 164        if (show_edges) {
 165                struct parent *p = rev->parent;
 166                while (p) {
 167                        if (mask != marked(p->parent))
 168                                return 1;
 169                        p = p->next;
 170                }
 171                return 0;
 172        }
 173        if (mask & basemask)
 174                return 0;
 175
 176        return 1;
 177}
 178
 179/*
 180 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
 181 *
 182 * The cache-file can be quite important for big trees. This is an
 183 * expensive operation if you have to walk the whole chain of
 184 * parents in a tree with a long revision history.
 185 */
 186int main(int argc, char **argv)
 187{
 188        int i;
 189        int nr = 0;
 190        unsigned char sha1[MAX_COMMITS][20];
 191
 192        /*
 193         * First - pick up all the revisions we can (both from
 194         * caches and from commit file chains).
 195         */
 196        for (i = 1; i < argc ; i++) {
 197                char *arg = argv[i];
 198
 199                if (!strcmp(arg, "--cache")) {
 200                        read_cache_file(argv[2]);
 201                        i++;
 202                        continue;
 203                }
 204
 205                if (!strcmp(arg, "--edges")) {
 206                        show_edges = 1;
 207                        continue;
 208                }
 209
 210                if (arg[0] == '^') {
 211                        arg++;
 212                        basemask |= 1<<nr;
 213                }
 214                if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
 215                        usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
 216                parse_commit(sha1[nr]);
 217                nr++;
 218        }
 219
 220        /*
 221         * Now we have the maximal tree. Walk the different sha files back to the root.
 222         */
 223        for (i = 0; i < nr; i++)
 224                mark_sha1_path(lookup_rev(sha1[i]), 1 << i);
 225
 226        /*
 227         * Now print out the results..
 228         */
 229        for (i = 0; i < nr_revs; i++) {
 230                struct revision *rev = revs[i];
 231                struct parent *p;
 232
 233                if (!interesting(rev))
 234                        continue;
 235
 236                printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
 237                p = rev->parent;
 238                while (p) {
 239                        printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));
 240                        p = p->next;
 241                }
 242                printf("\n");
 243        }
 244        return 0;
 245}