From: Linus Torvalds Date: Tue, 12 Jul 2005 18:49:27 +0000 (-0700) Subject: Abstract out the "name date" handling of commit-tree.c X-Git-Tag: v0.99.1~49 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=6aa33f4035d547ac2bd629b1cfd9c46262809d73;p=git.git Abstract out the "name date" handling of commit-tree.c We'll want to use it for the tagging too. --- diff --git a/Makefile b/Makefile index 160579ee..779fbbbf 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,7 @@ install: $(PROG) $(SCRIPTS) LIB_OBJS=read-cache.o sha1_file.o usage.o object.o commit.o tree.o blob.o \ tag.o date.o index.o diff-delta.o patch-delta.o entry.o path.o \ - epoch.o refs.o csum-file.o pack-check.o pkt-line.o connect.o + epoch.o refs.o csum-file.o pack-check.o pkt-line.o connect.o ident.o LIB_FILE=libgit.a LIB_H=cache.h object.h blob.h tree.h commit.h tag.h delta.h epoch.h csum-file.h \ pack.h pkt-line.h refs.h diff --git a/cache.h b/cache.h index f0eecb3d..1376700f 100644 --- a/cache.h +++ b/cache.h @@ -211,6 +211,9 @@ const char *show_date(unsigned long time, int timezone); void parse_date(const char *date, char *buf, int bufsize); void datestamp(char *buf, int bufsize); +extern int setup_ident(void); +extern char *get_ident(const char *name, const char *email, const char *date_str); + static inline void *xmalloc(size_t size) { void *ret = malloc(size); diff --git a/commit-tree.c b/commit-tree.c index 0d504e7e..8f3b96c2 100644 --- a/commit-tree.c +++ b/commit-tree.c @@ -45,39 +45,6 @@ static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...) memcpy(buf + size, one_line, len); } -static void remove_special(char *p) -{ - char c; - char *dst = p, *src = p; - - for (;;) { - c = *src; - src++; - switch(c) { - case '\n': case '<': case '>': - continue; - } - *dst++ = c; - if (!c) - break; - } - - /* - * Go back, and remove crud from the end: some people - * have commas etc in their gecos field - */ - dst--; - while (--dst >= p) { - unsigned char c = *dst; - switch (c) { - case ',': case ';': case '.': - *dst = 0; - continue; - } - break; - } -} - static void check_valid(unsigned char *sha1, const char *expect) { void *buf; @@ -112,18 +79,23 @@ static int new_parent(int idx) return 1; } +static char *git_author_info(void) +{ + return get_ident(gitenv("GIT_AUTHOR_NAME"), gitenv("GIT_AUTHOR_EMAIL"), gitenv("GIT_AUTHOR_DATE")); +} + +static char *git_committer_info(void) +{ + return get_ident(gitenv("GIT_COMMITTER_NAME"), gitenv("GIT_COMMITTER_EMAIL"), gitenv("GIT_COMMITTER_DATE")); +} + int main(int argc, char **argv) { - int i, len; + int i; int parents = 0; unsigned char tree_sha1[20]; unsigned char commit_sha1[20]; - char *gecos, *realgecos, *commitgecos; - char *email, *commitemail, realemail[1000]; - char date[50], realdate[50]; - char *audate, *cmdate; char comment[1000]; - struct passwd *pw; char *buffer; unsigned int size; @@ -142,35 +114,7 @@ int main(int argc, char **argv) } if (!parents) fprintf(stderr, "Committing initial tree %s\n", argv[1]); - pw = getpwuid(getuid()); - if (!pw) - die("You don't exist. Go away!"); - realgecos = pw->pw_gecos; - len = strlen(pw->pw_name); - memcpy(realemail, pw->pw_name, len); - realemail[len] = '@'; - gethostname(realemail+len+1, sizeof(realemail)-len-1); - if (!strchr(realemail+len+1, '.')) { - strcat(realemail, "."); - getdomainname(realemail+strlen(realemail), sizeof(realemail)-strlen(realemail)-1); - } - - datestamp(realdate, sizeof(realdate)); - strcpy(date, realdate); - - commitgecos = gitenv("GIT_COMMITTER_NAME") ? : realgecos; - commitemail = gitenv("GIT_COMMITTER_EMAIL") ? : realemail; - gecos = gitenv("GIT_AUTHOR_NAME") ? : realgecos; - email = gitenv("GIT_AUTHOR_EMAIL") ? : realemail; - audate = gitenv("GIT_AUTHOR_DATE"); - if (audate) - parse_date(audate, date, sizeof(date)); - cmdate = gitenv("GIT_COMMITTER_DATE"); - if (cmdate) - parse_date(cmdate, realdate, sizeof(realdate)); - - remove_special(gecos); remove_special(realgecos); remove_special(commitgecos); - remove_special(email); remove_special(realemail); remove_special(commitemail); + setup_ident(); init_buffer(&buffer, &size); add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1)); @@ -184,8 +128,8 @@ int main(int argc, char **argv) add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i])); /* Person/date information */ - add_buffer(&buffer, &size, "author %s <%s> %s\n", gecos, email, date); - add_buffer(&buffer, &size, "committer %s <%s> %s\n\n", commitgecos, commitemail, realdate); + add_buffer(&buffer, &size, "author %s\n", git_author_info()); + add_buffer(&buffer, &size, "committer %s\n\n", git_committer_info()); /* And add the comment */ while (fgets(comment, sizeof(comment), stdin) != NULL) diff --git a/ident.c b/ident.c new file mode 100644 index 00000000..faff4417 --- /dev/null +++ b/ident.c @@ -0,0 +1,138 @@ +/* + * ident.c + * + * create git identifier lines of the form "name date" + * + * Copyright (C) 2005 Linus Torvalds + */ +#include "cache.h" + +#include +#include +#include + +static char real_email[1000]; +static char real_name[1000]; +static char real_date[50]; + +int setup_ident(void) +{ + int len; + struct passwd *pw = getpwuid(getuid()); + + if (!pw) + die("You don't exist. Go away!"); + + /* Get the name ("gecos") */ + len = strlen(pw->pw_gecos); + if (len >= sizeof(real_name)) + die("Your parents must have hated you"); + memcpy(real_name, pw->pw_gecos, len+1); + + /* Make up a fake email address (name + '@' + hostname [+ '.' + domainname]) */ + len = strlen(pw->pw_name); + if (len > sizeof(real_email)/2) + die("Your parents must have hated you"); + memcpy(real_email, pw->pw_name, len); + real_email[len++] = '@'; + gethostname(real_email + len, sizeof(real_email) - len); + if (!strchr(real_email+len, '.')) { + len = strlen(real_email); + real_email[len++] = '.'; + getdomainname(real_email+len, sizeof(real_email)-len); + } + + /* And set the default date */ + datestamp(real_date, sizeof(real_date)); + return 0; +} + +static int add_raw(char *buf, int size, int offset, const char *str) +{ + int len = strlen(str); + if (offset + len > size) + return size; + memcpy(buf + offset, str, len); + return offset + len; +} + +static int crud(unsigned char c) +{ + static const char crud_array[256] = { + [0 ... 31] = 1, + [' '] = 1, + ['.'] = 1, [','] = 1, + [':'] = 1, [';'] = 1, + ['<'] = 1, ['>'] = 1, + ['"'] = 1, ['\''] = 1, + }; + return crud_array[c]; +} + +/* + * Copy over a string to the destination, but avoid special + * characters ('\n', '<' and '>') and remove crud at the end + */ +static int copy(char *buf, int size, int offset, const char *src) +{ + int i, len; + unsigned char c; + + /* Remove crud from the beginning.. */ + while ((c = *src) != 0) { + if (!crud(c)) + break; + src++; + } + + /* Remove crud from the end.. */ + len = strlen(src); + while (len > 0) { + c = src[len-1]; + if (!crud(c)) + break; + --len; + } + + /* + * Copy the rest to the buffer, but avoid the special + * characters '\n' '<' and '>' that act as delimeters on + * a identification line + */ + for (i = 0; i < len; i++) { + c = *src++; + switch (c) { + case '\n': case '<': case '>': + continue; + } + if (offset >= size) + return size; + buf[offset++] = c; + } + return offset; +} + +char *get_ident(const char *name, const char *email, const char *date_str) +{ + static char buffer[1000]; + char date[50]; + int i; + + if (!name) + name = real_name; + if (!email) + email = real_email; + strcpy(date, real_date); + if (date_str) + parse_date(date_str, date, sizeof(date)); + + i = copy(buffer, sizeof(buffer), 0, name); + i = add_raw(buffer, sizeof(buffer), i, " <"); + i = copy(buffer, sizeof(buffer), i, email); + i = add_raw(buffer, sizeof(buffer), i, "> "); + i = copy(buffer, sizeof(buffer), i, date); + if (i >= sizeof(buffer)) + die("Impossibly long personal identifier"); + buffer[i] = 0; + return buffer; +}