]>
Dogcows Code - chaz/githead/blob - githead.c
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?
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.
13 * Example usage (bash):
14 * PS1='\u@\h \w$(githead <~/.nogitprompt) $ '
16 * Written 04 June 2012 by Charles McGarvey.
19 const char* format
= "\ 1\e[%sm\ 2 %s\ 1\e[0m\ 2";
20 const char* ansi_codes
= "0;36";
21 const char* ansi_codes_dirty
= "0;31";
23 const int short_hash_length
= 7;
32 #include <sys/types.h>
35 int main(int argc
, char* argv
[])
41 fclose(stderr
); // usually do not want any extra junk printed
45 fprintf(stderr
, "pipe failed: %s\n", strerror(errno
));
51 fprintf(stderr
, "fork failed: %s\n", strerror(errno
));
55 char* args
[] = {"git", "rev-parse", "--show-toplevel", NULL
};
56 if (dup2(p
[1], 1) == -1) {
57 fprintf(stderr
, "dup2 failed: %s\n", strerror(errno
));
59 execvp(args
[0], args
);
66 while (waitpid(pid1
, &status
, 0) < 0) {
68 fprintf(stderr
, "waitpid failed: %s\n", strerror(errno
));
72 if (WEXITSTATUS(status
) == 0) {
73 ssize_t bytes
= read(p
[0], buf
, sizeof(buf
));
75 fprintf(stderr
, "read failed: %s\n", strerror(errno
));
81 while (gets(regbuf
)) {
84 int err
= regcomp(&re
, regbuf
, REG_EXTENDED
| REG_ICASE
| REG_NOSUB
);
86 regerror(err
, &re
, errbuf
, sizeof(errbuf
));
87 fprintf(stderr
, "regcomp failed: %s\n", errbuf
);
90 if (regexec(&re
, buf
, 0, NULL
, 0) == 0) {
97 fprintf(stderr
, "git failed; bailing out...\n");
103 fprintf(stderr
, "fork failed: %s\n", strerror(errno
));
107 char* args
[] = {"git", "diff", "--no-ext-diff", "--quiet", "--exit-code", NULL
};
108 execvp(args
[0], args
);
113 fprintf(stderr
, "pipe failed: %s\n", strerror(errno
));
119 fprintf(stderr
, "fork failed: %s\n", strerror(errno
));
123 char* args
[] = {"git", "symbolic-ref", "HEAD", NULL
};
124 if (dup2(p
[1], 1) == -1) {
125 fprintf(stderr
, "dup2 failed: %s\n", strerror(errno
));
127 execvp(args
[0], args
);
133 while (waitpid(pid3
, &status
, 0) < 0) {
134 if (errno
!= EINTR
) {
135 fprintf(stderr
, "waitpid failed: %s\n", strerror(errno
));
139 if (WEXITSTATUS(status
) == 0) {
140 ssize_t bytes
= read(p
[0], buf
, sizeof(buf
));
142 fprintf(stderr
, "read failed: %s\n", strerror(errno
));
149 fprintf(stderr
, "pipe failed: %s\n", strerror(errno
));
155 fprintf(stderr
, "fork failed: %s\n", strerror(errno
));
159 char* args
[] = {"git", "rev-parse", "HEAD", NULL
};
160 if (dup2(p
[1], 1) == -1) {
161 fprintf(stderr
, "dup2 failed: %s\n", strerror(errno
));
163 execvp(args
[0], args
);
169 while (waitpid(pid4
, &status
, 0) < 0) {
170 if (errno
!= EINTR
) {
171 fprintf(stderr
, "waitpid failed: %s\n", strerror(errno
));
175 if (WEXITSTATUS(status
) == 0) {
176 ssize_t bytes
= read(p
[0], buf
, sizeof(buf
));
178 fprintf(stderr
, "read failed: %s\n", strerror(errno
));
181 buf
[short_hash_length
] = '\0';
186 while (waitpid(pid2
, &status
, 0) < 0) {
187 if (errno
!= EINTR
) {
188 fprintf(stderr
, "waitpid failed: %s\n", strerror(errno
));
192 dirty
= (WEXITSTATUS(status
) != 0);
196 printf(format
, ansi_codes_dirty
, ref
);
199 printf(format
, ansi_codes
, ref
);
This page took 0.044367 seconds and 4 git commands to generate.