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"commit.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 voidread_cache_file(const char*path) 20{ 21die("no revtree cache file yet"); 22} 23 24/* 25 * Some revisions are less interesting than others. 26 * 27 * For example, if we use a cache-file, that one may contain 28 * revisions that were never used. They are never interesting. 29 * 30 * And sometimes we're only interested in "edge" commits, ie 31 * places where the marking changes between parent and child. 32 */ 33static intinteresting(struct commit *rev) 34{ 35unsigned mask = rev->object.flags; 36 37if(!mask) 38return0; 39if(show_edges) { 40struct commit_list *p = rev->parents; 41while(p) { 42if(mask != p->item->object.flags) 43return1; 44 p = p->next; 45} 46return0; 47} 48if(mask & basemask) 49return0; 50 51return1; 52} 53 54voidprocess_commit(unsigned char*sha1) 55{ 56struct commit_list *parents; 57struct commit *obj =lookup_commit(sha1); 58 59if(obj->object.parsed) 60return; 61 62parse_commit(obj); 63 64 parents = obj->parents; 65while(parents) { 66process_commit(parents->item->object.sha1); 67 parents = parents->next; 68} 69} 70 71/* 72 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>] 73 * 74 * The cache-file can be quite important for big trees. This is an 75 * expensive operation if you have to walk the whole chain of 76 * parents in a tree with a long revision history. 77 */ 78intmain(int argc,char**argv) 79{ 80int i; 81int nr =0; 82unsigned char sha1[MAX_COMMITS][20]; 83 84/* 85 * First - pick up all the revisions we can (both from 86 * caches and from commit file chains). 87 */ 88for(i =1; i < argc ; i++) { 89char*arg = argv[i]; 90 91if(!strcmp(arg,"--cache")) { 92read_cache_file(argv[2]); 93 i++; 94continue; 95} 96 97if(!strcmp(arg,"--edges")) { 98 show_edges =1; 99continue; 100} 101 102if(arg[0] =='^') { 103 arg++; 104 basemask |=1<<nr; 105} 106if(nr >= MAX_COMMITS ||get_sha1_hex(arg, sha1[nr])) 107usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]"); 108process_commit(sha1[nr]); 109 nr++; 110} 111 112/* 113 * Now we have the maximal tree. Walk the different sha files back to the root. 114 */ 115for(i =0; i < nr; i++) 116mark_reachable(&lookup_commit(sha1[i])->object,1<< i); 117 118/* 119 * Now print out the results.. 120 */ 121for(i =0; i < nr_objs; i++) { 122struct object *obj = objs[i]; 123struct commit *commit; 124struct commit_list *p; 125 126if(obj->type != commit_type) 127continue; 128 129 commit = (struct commit *) obj; 130 131if(!interesting(commit)) 132continue; 133 134printf("%lu%s:%d", commit->date,sha1_to_hex(obj->sha1), 135 obj->flags); 136 p = commit->parents; 137while(p) { 138printf("%s:%d",sha1_to_hex(p->item->object.sha1), 139 p->item->object.flags); 140 p = p->next; 141} 142printf("\n"); 143} 144return0; 145}