git-rev-parse: Allow a "zeroth" parent of a commit - the commit itself.
[git.git] / export.c
1 #include "cache.h"
2 #include "commit.h"
3
4 /*
5  * Show one commit
6  */
7 static void show_commit(struct commit *commit)
8 {
9         char cmdline[400];
10         char hex[100];
11
12         strcpy(hex, sha1_to_hex(commit->object.sha1));
13         printf("Id: %s\n", hex);
14         fflush(NULL);
15         sprintf(cmdline, "git-cat-file commit %s", hex);
16         system(cmdline);
17         if (commit->parents) {
18                 char *against = sha1_to_hex(commit->parents->item->object.sha1);
19                 printf("\n\n======== diff against %s ========\n", against);
20                 fflush(NULL);
21                 sprintf(cmdline, "git-diff-tree -p %s %s", against, hex);
22                 system(cmdline);
23         }
24         printf("======== end ========\n\n");
25 }
26
27 /*
28  * Show all unseen commits, depth-first
29  */
30 static void show_unseen(struct commit *top)
31 {
32         struct commit_list *parents;
33
34         if (top->object.flags & 2)
35                 return;
36         top->object.flags |= 2;
37         parents = top->parents;
38         while (parents) {
39                 show_unseen(parents->item);
40                 parents = parents->next;
41         }
42         show_commit(top);
43 }
44
45 static void export(struct commit *top, struct commit *base)
46 {
47         mark_reachable(&top->object, 1);
48         if (base)
49                 mark_reachable(&base->object, 2);
50         show_unseen(top);
51 }
52
53 static struct commit *get_commit(unsigned char *sha1)
54 {
55         struct commit *commit = lookup_commit(sha1);
56         if (!commit->object.parsed) {
57                 struct commit_list *parents;
58
59                 if (parse_commit(commit) < 0)
60                         die("unable to parse commit %s", sha1_to_hex(sha1));
61                 parents = commit->parents;
62                 while (parents) {
63                         get_commit(parents->item->object.sha1);
64                         parents = parents->next;
65                 }
66         }
67         return commit;
68 }
69
70 int main(int argc, char **argv)
71 {
72         unsigned char base_sha1[20];
73         unsigned char top_sha1[20];
74
75         if (argc < 2 || argc > 4 ||
76             get_sha1(argv[1], top_sha1) ||
77             (argc == 3 && get_sha1(argv[2], base_sha1)))
78                 usage("git-export top [base]");
79         export(get_commit(top_sha1), argc==3 ? get_commit(base_sha1) : NULL);
80         return 0;
81 }