]> Dogcows Code - chaz/githead/blob - githead.c
initial commit
[chaz/githead] / githead.c
1
2 /*
3 * Get the name of the current git branch (pointed to by HEAD) and print it
4 * out, color-coded to show if any working files are dirty. This would
5 * ideally be implemented as a shell script, but I wrote it in C because it
6 * needs to be fast! And we all know C is fast, amiright?
7 *
8 * Also reads in regular expressions (one per line) matching paths that
9 * should be ignored. Each regex is matched against the top-level
10 * directory path of the repository. If it matches, nothing is printed.
11 * It is a good idea to ignore repositories on slow (remote) disks.
12 *
13 * Example usage (bash):
14 * PS1='\u@\h \w$(githead <~/.nogitprompt) $ '
15 *
16 * Written 04 June 2012 by Charles McGarvey.
17 */
18
19 const char* ansi_codes = "0;36";
20 const char* ansi_codes_dirty = "0;31";
21
22 #include <errno.h>
23 #include <regex.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include <sys/types.h>
30 #include <sys/wait.h>
31
32 int main(int argc, char* argv[])
33 {
34 char buf[4096];
35
36 char* branch = NULL;
37 int dirty = 0;
38
39 fclose(stderr); // usually do not want any extra junk printed
40
41 int p[2];
42 if (pipe(p) == -1) {
43 fprintf(stderr, "pipe failed: %s\n", strerror(errno));
44 return 1;
45 }
46
47 pid_t pid1 = fork();
48 if (pid1 == -1) {
49 fprintf(stderr, "fork failed: %s\n", strerror(errno));
50 return 1;
51 }
52 if (pid1 == 0) {
53 char* args[] = {"git", "rev-parse", "--show-toplevel", NULL};
54 if (dup2(p[1], 1) == -1) {
55 fprintf(stderr, "dup2 failed: %s\n", strerror(errno));
56 }
57 execvp(args[0], args);
58 return 0;
59 }
60
61 close(p[1]);
62
63 int status;
64 while (waitpid(pid1, &status, 0) < 0) {
65 if (errno != EINTR) {
66 fprintf(stderr, "waitpid failed: %s\n", strerror(errno));
67 return 1;
68 }
69 }
70 if (WEXITSTATUS(status) == 0) {
71 ssize_t bytes = read(p[0], buf, sizeof(buf));
72 if (bytes < 0) {
73 fprintf(stderr, "read failed: %s\n", strerror(errno));
74 return 1;
75 }
76 buf[bytes-1] = '\0';
77
78 char regbuf[4096];
79 while (gets(regbuf)) {
80 char errbuf[4096];
81 regex_t re;
82 int err = regcomp(&re, regbuf, REG_EXTENDED | REG_ICASE | REG_NOSUB);
83 if (err != 0) {
84 regerror(err, &re, errbuf, sizeof(errbuf));
85 fprintf(stderr, "regcomp failed: %s\n", errbuf);
86 continue;
87 }
88 if (regexec(&re, buf, 0, NULL, 0) == 0) {
89 return 0;
90 }
91 regfree(&re);
92 }
93 }
94 else {
95 fprintf(stderr, "git failed; bailing out...\n");
96 return 1;
97 }
98
99 pid_t pid2 = fork();
100 if (pid2 == -1) {
101 fprintf(stderr, "fork failed: %s\n", strerror(errno));
102 return 1;
103 }
104 if (pid2 == 0) {
105 char* args[] = {"git", "diff", "--no-ext-diff", "--quiet", "--exit-code", NULL};
106 execvp(args[0], args);
107 return 0;
108 }
109
110 if (pipe(p) == -1) {
111 fprintf(stderr, "pipe failed: %s\n", strerror(errno));
112 return 1;
113 }
114
115 pid_t pid3 = fork();
116 if (pid3 == -1) {
117 fprintf(stderr, "fork failed: %s\n", strerror(errno));
118 return 1;
119 }
120 if (pid3 == 0) {
121 char* args[] = {"git", "symbolic-ref", "HEAD", NULL};
122 if (dup2(p[1], 1) == -1) {
123 fprintf(stderr, "dup2 failed: %s\n", strerror(errno));
124 }
125 execvp(args[0], args);
126 return 1;
127 }
128
129 close(p[1]);
130
131 while (waitpid(pid3, &status, 0) < 0) {
132 if (errno != EINTR) {
133 fprintf(stderr, "waitpid failed: %s\n", strerror(errno));
134 return 1;
135 }
136 }
137 if (WEXITSTATUS(status) == 0) {
138 ssize_t bytes = read(p[0], buf, sizeof(buf));
139 if (bytes < 0) {
140 fprintf(stderr, "read failed: %s\n", strerror(errno));
141 return 1;
142 }
143 buf[bytes-1] = '\0';
144 branch = buf + 11;
145 }
146
147 while (waitpid(pid2, &status, 0) < 0) {
148 if (errno != EINTR) {
149 fprintf(stderr, "waitpid failed: %s\n", strerror(errno));
150 return 1;
151 }
152 }
153 dirty = (WEXITSTATUS(status) != 0);
154
155 if (branch) {
156 if (dirty) {
157 printf("\033[%sm[%s]\033[0m", ansi_codes_dirty, branch);
158 }
159 else {
160 printf("\033[%sm[%s]\033[0m", ansi_codes, branch);
161 }
162 }
163
164 return 0;
165 }
166
This page took 0.041672 seconds and 4 git commands to generate.