]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin-log.c
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
;
79 static char *output_directory
= NULL
;
81 static void reopen_stdout(struct commit
*commit
, int nr
)
87 if (output_directory
) {
88 strncpy(filename
, output_directory
, 1010);
89 len
= strlen(filename
);
90 if (filename
[len
- 1] != '/')
91 filename
[len
++] = '/';
94 sprintf(filename
+ len
, "%04d", nr
);
95 len
= strlen(filename
);
97 sol
= strstr(commit
->buffer
, "\n\n");
102 /* strip [PATCH] or [PATCH blabla] */
103 if (!strncmp(sol
, "[PATCH", 6)) {
104 char *eos
= strchr(sol
+ 6, ']');
106 while (isspace(*eos
))
112 for (j
= 0; len
< 1024 - 6 && sol
[j
] && sol
[j
] != '\n'; j
++) {
113 if (istitlechar(sol
[j
])) {
115 filename
[len
++] = '-';
118 filename
[len
++] = sol
[j
];
120 while (sol
[j
+ 1] == '.')
125 while (filename
[len
- 1] == '.' || filename
[len
- 1] == '-')
128 strcpy(filename
+ len
, ".txt");
129 fprintf(realstdout
, "%s\n", filename
);
130 freopen(filename
, "w", stdout
);
133 int cmd_format_patch(int argc
, const char **argv
, char **envp
)
135 struct commit
*commit
;
136 struct commit
**list
= NULL
;
138 int nr
= 0, total
, i
, j
;
141 init_revisions(&rev
);
142 rev
.commit_format
= CMIT_FMT_EMAIL
;
143 rev
.verbose_header
= 1;
145 rev
.diffopt
.with_raw
= 0;
146 rev
.diffopt
.with_stat
= 1;
147 rev
.combine_merges
= 0;
148 rev
.ignore_merges
= 1;
149 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
152 * Parse the arguments before setup_revisions(), or something
153 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
154 * possibly a valid SHA1.
156 for (i
= 1, j
= 1; i
< argc
; i
++) {
157 if (!strcmp(argv
[i
], "--stdout"))
159 else if (!strcmp(argv
[i
], "-o")) {
161 die ("Which directory?");
162 if (mkdir(argv
[i
+ 1], 0777) < 0 && errno
!= EEXIST
)
163 die("Could not create directory %s",
165 output_directory
= strdup(argv
[i
+ 1]);
172 argc
= setup_revisions(argc
, argv
, &rev
, "HEAD");
174 die ("unrecognized argument: %s", argv
[1]);
177 realstdout
= fdopen(dup(1), "w");
179 prepare_revision_walk(&rev
);
180 while ((commit
= get_revision(&rev
)) != NULL
) {
182 if (commit
->parents
&& commit
->parents
->next
)
185 list
= realloc(list
, nr
* sizeof(list
[0]));
186 list
[nr
- 1] = commit
;
193 reopen_stdout(commit
, total
- nr
);
194 shown
= log_tree_commit(&rev
, commit
);
195 free(commit
->buffer
);
196 commit
->buffer
= NULL
;
198 printf("-- \n%s\n\n", git_version_string
);
202 if (output_directory
)
203 free(output_directory
);