fixup.cc5711424b7ae36276a40c06ede5d95f87ca20f0
[git/dscho.git] / builtin-fmt.c
blob04b8b2ac9840887626f65ba39e862140c36fb291
1 /*
2 * GIT - The information manager from hell
3 */
5 #include "cache.h"
6 #include "refs.h"
7 #include "builtin.h"
8 #include "utf8.h"
10 static const char *fmt_usage = "git-fmt [--width <columns>] [<files>...]";
12 static int fmt_from_fd(int fd, int width)
14 int indent = 0;
15 char buffer[1024];
16 for (;;) {
17 int ret = xread(fd, buffer, sizeof(buffer) - 1);
18 if (ret <= 0) {
19 putchar('\n');
20 return ret;
22 buffer[ret] = '\0';
23 indent = print_wrapped_text(buffer, -indent, 0, width);
27 int cmd_fmt(int argc, const char **argv, const char *prefix)
29 int width = 76;
31 if (argc > 2 && (!strcmp(argv[1], "--width")
32 || !strcmp(argv[1], "-w"))) {
33 width = atoi(argv[2]);
34 argc -= 2;
35 argv += 2;
38 if (argc == 1)
39 return fmt_from_fd(0, width);
40 else {
41 int i, ret = 0;
42 if (!strcmp(argv[1], "-h"))
43 usage(fmt_usage);
44 for (i = 1; i < argc && ret; i++) {
45 int fd = open(argv[i], O_RDONLY);
46 if (fd < 0) {
47 warning ("Error reading %s: %s", argv[i],
48 strerror(errno));
49 continue;
51 ret |= fmt_from_fd(fd, width);
52 close(fd);
54 return ret;