2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
14 static int cmd_log_wc(int argc
, const char **argv
, char **envp
,
17 struct commit
*commit
;
19 rev
->abbrev
= DEFAULT_ABBREV
;
20 rev
->commit_format
= CMIT_FMT_DEFAULT
;
21 rev
->verbose_header
= 1;
22 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
25 die("unrecognized argument: %s", argv
[1]);
27 prepare_revision_walk(rev
);
29 while ((commit
= get_revision(rev
)) != NULL
) {
30 log_tree_commit(rev
, commit
);
32 commit
->buffer
= NULL
;
37 int cmd_whatchanged(int argc
, const char **argv
, char **envp
)
43 rev
.diffopt
.recursive
= 1;
44 return cmd_log_wc(argc
, argv
, envp
, &rev
);
47 int cmd_show(int argc
, const char **argv
, char **envp
)
53 rev
.diffopt
.recursive
= 1;
54 rev
.combine_merges
= 1;
55 rev
.dense_combined_merges
= 1;
56 rev
.always_show_header
= 1;
57 rev
.ignore_merges
= 0;
59 return cmd_log_wc(argc
, argv
, envp
, &rev
);
62 int cmd_log(int argc
, const char **argv
, char **envp
)
67 rev
.always_show_header
= 1;
68 rev
.diffopt
.recursive
= 1;
69 return cmd_log_wc(argc
, argv
, envp
, &rev
);
72 static int istitlechar(char c
)
74 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
75 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
78 static FILE *realstdout
= NULL
;
80 static void reopen_stdout(struct commit
*commit
, int nr
)
87 sprintf(filename
, "%04d", nr
);
88 len
= strlen(filename
);
90 sol
= strstr(commit
->buffer
, "\n\n");
95 /* strip [PATCH] or [PATCH blabla] */
96 if (!strncmp(sol
, "[PATCH", 6)) {
97 char *eos
= strchr(sol
+ 6, ']');
105 for (j
= 0; len
< 1024 - 6 && sol
[j
] && sol
[j
] != '\n'; j
++) {
106 if (istitlechar(sol
[j
])) {
108 filename
[len
++] = '-';
111 filename
[len
++] = sol
[j
];
113 while (sol
[j
+ 1] == '.')
118 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
121 strcpy(filename
+ len
, ".txt");
122 fprintf(realstdout
, "%s\n", filename
);
123 freopen(filename
, "w", stdout
);
126 int cmd_format_patch(int argc
, const char **argv
, char **envp
)
128 struct commit
*commit
;
129 struct commit
**list
= NULL
;
134 init_revisions(&rev
);
135 rev
.commit_format
= CMIT_FMT_EMAIL
;
136 rev
.verbose_header
= 1;
138 rev
.diffopt
.with_raw
= 0;
139 rev
.diffopt
.with_stat
= 1;
140 rev
.combine_merges
= 0;
141 rev
.ignore_merges
= 1;
142 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
143 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
146 if (!strcmp(argv
[1], "--stdout"))
149 die ("unrecognized argument: %s", argv
[1]);
155 realstdout
= fdopen(dup(1), "w");
157 prepare_revision_walk(&rev
);
158 while ((commit
= get_revision(&rev
)) != NULL
) {
160 if (commit
->parents
&& commit
->parents
->next
)
163 list
= realloc(list
, nr
* sizeof(list
[0]));
164 list
[nr
- 1] = commit
;
171 reopen_stdout(commit
, total
- nr
);
172 shown
= log_tree_commit(&rev
, commit
);
173 free(commit
->buffer
);
174 commit
->buffer
= NULL
;
176 printf("-- \n%s\n\n", git_version_string
);