2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
14 /* this is in builtin-diff.c */
15 void add_head(struct rev_info
*revs
);
17 static int cmd_log_wc(int argc
, const char **argv
, char **envp
,
20 struct commit
*commit
;
22 rev
->abbrev
= DEFAULT_ABBREV
;
23 rev
->commit_format
= CMIT_FMT_DEFAULT
;
24 rev
->verbose_header
= 1;
25 argc
= setup_revisions(argc
, argv
, rev
, "HEAD");
28 die("unrecognized argument: %s", argv
[1]);
30 prepare_revision_walk(rev
);
32 while ((commit
= get_revision(rev
)) != NULL
) {
33 log_tree_commit(rev
, commit
);
35 commit
->buffer
= NULL
;
40 int cmd_whatchanged(int argc
, const char **argv
, char **envp
)
46 rev
.diffopt
.recursive
= 1;
47 return cmd_log_wc(argc
, argv
, envp
, &rev
);
50 int cmd_show(int argc
, const char **argv
, char **envp
)
56 rev
.diffopt
.recursive
= 1;
57 rev
.combine_merges
= 1;
58 rev
.dense_combined_merges
= 1;
59 rev
.always_show_header
= 1;
60 rev
.ignore_merges
= 0;
62 return cmd_log_wc(argc
, argv
, envp
, &rev
);
65 int cmd_log(int argc
, const char **argv
, char **envp
)
70 rev
.always_show_header
= 1;
71 rev
.diffopt
.recursive
= 1;
72 return cmd_log_wc(argc
, argv
, envp
, &rev
);
75 static int istitlechar(char c
)
77 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
78 (c
>= '0' && c
<= '9') || c
== '.' || c
== '_';
81 static FILE *realstdout
= NULL
;
82 static char *output_directory
= NULL
;
84 static void reopen_stdout(struct commit
*commit
, int nr
, int keep_subject
)
90 if (output_directory
) {
91 strncpy(filename
, output_directory
, 1010);
92 len
= strlen(filename
);
93 if (filename
[len
- 1] != '/')
94 filename
[len
++] = '/';
97 sprintf(filename
+ len
, "%04d", nr
);
98 len
= strlen(filename
);
100 sol
= strstr(commit
->buffer
, "\n\n");
105 /* strip [PATCH] or [PATCH blabla] */
106 if (!keep_subject
&& !strncmp(sol
, "[PATCH", 6)) {
107 char *eos
= strchr(sol
+ 6, ']');
109 while (isspace(*eos
))
115 for (j
= 0; len
< 1024 - 6 && sol
[j
] && sol
[j
] != '\n'; j
++) {
116 if (istitlechar(sol
[j
])) {
118 filename
[len
++] = '-';
121 filename
[len
++] = sol
[j
];
123 while (sol
[j
+ 1] == '.')
128 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
131 strcpy(filename
+ len
, ".txt");
132 fprintf(realstdout
, "%s\n", filename
);
133 freopen(filename
, "w", stdout
);
136 int cmd_format_patch(int argc
, const char **argv
, char **envp
)
138 struct commit
*commit
;
139 struct commit
**list
= NULL
;
141 int nr
= 0, total
, i
, j
;
144 int keep_subject
= 0;
146 init_revisions(&rev
);
147 rev
.commit_format
= CMIT_FMT_EMAIL
;
148 rev
.verbose_header
= 1;
150 rev
.diffopt
.with_raw
= 0;
151 rev
.diffopt
.with_stat
= 1;
152 rev
.combine_merges
= 0;
153 rev
.ignore_merges
= 1;
154 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
157 * Parse the arguments before setup_revisions(), or something
158 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
159 * possibly a valid SHA1.
161 for (i
= 1, j
= 1; i
< argc
; i
++) {
162 if (!strcmp(argv
[i
], "--stdout"))
164 else if (!strcmp(argv
[i
], "-n") ||
165 !strcmp(argv
[i
], "--numbered"))
167 else if (!strcmp(argv
[i
], "-k") ||
168 !strcmp(argv
[i
], "--keep-subject")) {
171 } else if (!strcmp(argv
[i
], "-o")) {
173 die ("Which directory?");
174 if (mkdir(argv
[i
+ 1], 0777) < 0 && errno
!= EEXIST
)
175 die("Could not create directory %s",
177 output_directory
= strdup(argv
[i
+ 1]);
184 if (numbered
&& keep_subject
< 0)
185 die ("-n and -k are mutually exclusive.");
187 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
189 die ("unrecognized argument: %s", argv
[1]);
191 if (rev
.pending_objects
&& rev
.pending_objects
->next
== NULL
) {
192 rev
.pending_objects
->item
->flags
|= UNINTERESTING
;
197 realstdout
= fdopen(dup(1), "w");
199 prepare_revision_walk(&rev
);
200 while ((commit
= get_revision(&rev
)) != NULL
) {
202 if (commit
->parents
&& commit
->parents
->next
)
205 list
= realloc(list
, nr
* sizeof(list
[0]));
206 list
[nr
- 1] = commit
;
216 reopen_stdout(commit
, rev
.nr
, keep_subject
);
217 shown
= log_tree_commit(&rev
, commit
);
218 free(commit
->buffer
);
219 commit
->buffer
= NULL
;
221 printf("-- \n%s\n\n", git_version_string
);
225 if (output_directory
)
226 free(output_directory
);