Git fork

Clean up compatibility definitions.

This attempts to clean up the way various compatibility
functions are defined and used.

- A new header file, git-compat-util.h, is introduced. This
looks at various NO_XXX and does necessary function name
replacements, equivalent of -Dstrcasestr=gitstrcasestr in the
Makefile.

- Those function name replacements are removed from the Makefile.

- Common features such as usage(), die(), xmalloc() are moved
from cache.h to git-compat-util.h; cache.h includes
git-compat-util.h itself.

Signed-off-by: Junio C Hamano <junkio@cox.net>

+129 -118
+6 -6
Makefile
··· 162 162 LIB_H = \ 163 163 blob.h cache.h commit.h count-delta.h csum-file.h delta.h \ 164 164 diff.h epoch.h object.h pack.h pkt-line.h quote.h refs.h \ 165 - run-command.h strbuf.h tag.h tree.h 165 + run-command.h strbuf.h tag.h tree.h git-compat-util.h 166 166 167 167 DIFF_OBJS = \ 168 168 diff.o diffcore-break.o diffcore-order.o diffcore-pathspec.o \ ··· 320 320 SIMPLE_LIB += -lnsl 321 321 endif 322 322 ifdef NO_STRCASESTR 323 - COMPAT_CFLAGS += -Dstrcasestr=gitstrcasestr -DNO_STRCASESTR=1 323 + COMPAT_CFLAGS += -DNO_STRCASESTR 324 324 COMPAT_OBJS += compat/strcasestr.o 325 325 endif 326 326 ifdef NO_SETENV 327 - COMPAT_CFLAGS += -Dsetenv=gitsetenv -DNO_SETENV=1 327 + COMPAT_CFLAGS += -DNO_SETENV 328 328 COMPAT_OBJS += compat/setenv.o 329 329 endif 330 330 ifdef NO_MMAP 331 - COMPAT_CFLAGS += -Dmmap=gitfakemmap -Dmunmap=gitfakemunmap -DNO_MMAP 331 + COMPAT_CFLAGS += -DNO_MMAP 332 332 COMPAT_OBJS += compat/mmap.o 333 333 endif 334 334 ifdef NO_IPV6 ··· 363 363 all: 364 364 $(MAKE) -C templates 365 365 366 - git$(X): git.c $(COMPAT_OBJS) Makefile 366 + git$X: git.c $(LIB_FILE) Makefile 367 367 $(CC) -DGIT_EXEC_PATH='"$(bindir)"' -DGIT_VERSION='"$(GIT_VERSION)"' \ 368 - $(CFLAGS) $(COMPAT_CFLAGS) -o $@ $(filter %.c,$^) $(filter %.o,$^) 368 + $(CFLAGS) $(COMPAT_CFLAGS) -o $@ $(filter %.c,$^) $(LIB_FILE) 369 369 370 370 $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh 371 371 rm -f $@
+1 -95
cache.h
··· 1 1 #ifndef CACHE_H 2 2 #define CACHE_H 3 3 4 - #include <unistd.h> 5 - #include <stdio.h> 6 - #include <sys/stat.h> 7 - #include <fcntl.h> 8 - #include <stddef.h> 9 - #include <stdlib.h> 10 - #include <stdarg.h> 11 - #include <string.h> 12 - #include <errno.h> 13 - #include <limits.h> 14 - #ifndef NO_MMAP 15 - #include <sys/mman.h> 16 - #endif 17 - #include <sys/param.h> 18 - #include <netinet/in.h> 19 - #include <sys/types.h> 20 - #include <dirent.h> 4 + #include "git-compat-util.h" 21 5 22 6 #include SHA1_HEADER 23 7 #include <zlib.h> ··· 34 18 #define DT_REG 2 35 19 #define DT_LNK 3 36 20 #define DTYPE(de) DT_UNKNOWN 37 - #endif 38 - 39 - #ifdef __GNUC__ 40 - #define NORETURN __attribute__((__noreturn__)) 41 - #else 42 - #define NORETURN 43 - #ifndef __attribute__ 44 - #define __attribute__(x) 45 - #endif 46 21 #endif 47 22 48 23 /* ··· 250 225 extern int create_symref(const char *git_HEAD, const char *refs_heads_master); 251 226 extern int validate_symref(const char *git_HEAD); 252 227 253 - /* General helper functions */ 254 - extern void usage(const char *err) NORETURN; 255 - extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); 256 - extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); 257 - 258 228 extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2); 259 229 extern int cache_name_compare(const char *name1, int len1, const char *name2, int len2); 260 230 ··· 272 242 extern const char *git_author_info(void); 273 243 extern const char *git_committer_info(void); 274 244 275 - static inline void *xmalloc(size_t size) 276 - { 277 - void *ret = malloc(size); 278 - if (!ret) 279 - die("Out of memory, malloc failed"); 280 - return ret; 281 - } 282 - 283 - static inline void *xrealloc(void *ptr, size_t size) 284 - { 285 - void *ret = realloc(ptr, size); 286 - if (!ret) 287 - die("Out of memory, realloc failed"); 288 - return ret; 289 - } 290 - 291 - static inline void *xcalloc(size_t nmemb, size_t size) 292 - { 293 - void *ret = calloc(nmemb, size); 294 - if (!ret) 295 - die("Out of memory, calloc failed"); 296 - return ret; 297 - } 298 - 299 245 struct checkout { 300 246 const char *base_dir; 301 247 int base_dir_len; ··· 373 319 /* Dumb servers support */ 374 320 extern int update_server_info(int); 375 321 376 - #ifdef NO_MMAP 377 - 378 - #ifndef PROT_READ 379 - #define PROT_READ 1 380 - #define PROT_WRITE 2 381 - #define MAP_PRIVATE 1 382 - #define MAP_FAILED ((void*)-1) 383 - #endif 384 - 385 - extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset); 386 - extern int gitfakemunmap(void *start, size_t length); 387 - 388 - #endif 389 - 390 322 typedef int (*config_fn_t)(const char *, const char *); 391 323 extern int git_default_config(const char *, const char *); 392 324 extern int git_config_from_file(config_fn_t fn, const char *); ··· 403 335 404 336 #define MAX_ENCODING_LENGTH 64 405 337 extern char git_commit_encoding[MAX_ENCODING_LENGTH]; 406 - 407 - /* Sane ctype - no locale, and works with signed chars */ 408 - #undef isspace 409 - #undef isdigit 410 - #undef isalpha 411 - #undef isalnum 412 - #undef tolower 413 - #undef toupper 414 - extern unsigned char sane_ctype[256]; 415 - #define GIT_SPACE 0x01 416 - #define GIT_DIGIT 0x02 417 - #define GIT_ALPHA 0x04 418 - #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0) 419 - #define isspace(x) sane_istest(x,GIT_SPACE) 420 - #define isdigit(x) sane_istest(x,GIT_DIGIT) 421 - #define isalpha(x) sane_istest(x,GIT_ALPHA) 422 - #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT) 423 - #define tolower(x) sane_case((unsigned char)(x), 0x20) 424 - #define toupper(x) sane_case((unsigned char)(x), 0) 425 - 426 - static inline int sane_case(int x, int high) 427 - { 428 - if (sane_istest(x, GIT_ALPHA)) 429 - x = (x & ~0x20) | high; 430 - return x; 431 - } 432 338 433 339 extern int copy_fd(int ifd, int ofd); 434 340 #endif /* CACHE_H */
+1 -1
compat/mmap.c
··· 2 2 #include <stdlib.h> 3 3 #include <unistd.h> 4 4 #include <errno.h> 5 - #include "../cache.h" 5 + #include "../git-compat-util.h" 6 6 7 7 void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset) 8 8 {
+1 -2
compat/strcasestr.c
··· 1 - #include <string.h> 2 - #include <ctype.h> 1 + #include "../git-compat-util.h" 3 2 4 3 char *gitstrcasestr(const char *haystack, const char *needle) 5 4 {
+113
git-compat-util.h
··· 1 + #ifndef GIT_COMPAT_UTIL_H 2 + #define GIT_COMPAT_UTIL_H 3 + 4 + #include <unistd.h> 5 + #include <stdio.h> 6 + #include <sys/stat.h> 7 + #include <fcntl.h> 8 + #include <stddef.h> 9 + #include <stdlib.h> 10 + #include <stdarg.h> 11 + #include <string.h> 12 + #include <errno.h> 13 + #include <limits.h> 14 + #include <sys/param.h> 15 + #include <netinet/in.h> 16 + #include <sys/types.h> 17 + #include <dirent.h> 18 + 19 + #ifdef __GNUC__ 20 + #define NORETURN __attribute__((__noreturn__)) 21 + #else 22 + #define NORETURN 23 + #ifndef __attribute__ 24 + #define __attribute__(x) 25 + #endif 26 + #endif 27 + 28 + /* General helper functions */ 29 + extern void usage(const char *err) NORETURN; 30 + extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); 31 + extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); 32 + 33 + #ifdef NO_MMAP 34 + 35 + #ifndef PROT_READ 36 + #define PROT_READ 1 37 + #define PROT_WRITE 2 38 + #define MAP_PRIVATE 1 39 + #define MAP_FAILED ((void*)-1) 40 + #endif 41 + 42 + #define mmap gitfakemmap 43 + #define munmap gitfakemunmap 44 + extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset); 45 + extern int gitfakemunmap(void *start, size_t length); 46 + 47 + #else /* NO_MMAP */ 48 + 49 + #include <sys/mman.h> 50 + 51 + #endif /* NO_MMAP */ 52 + 53 + #ifdef NO_SETENV 54 + #define setenv gitsetenv 55 + extern int gitsetenv(const char *, const char *, int); 56 + #endif 57 + 58 + #ifdef NO_STRCASESTR 59 + #define strcasestr gitstrcasestr 60 + extern char *gitstrcasestr(const char *haystack, const char *needle); 61 + #endif 62 + 63 + static inline void *xmalloc(size_t size) 64 + { 65 + void *ret = malloc(size); 66 + if (!ret) 67 + die("Out of memory, malloc failed"); 68 + return ret; 69 + } 70 + 71 + static inline void *xrealloc(void *ptr, size_t size) 72 + { 73 + void *ret = realloc(ptr, size); 74 + if (!ret) 75 + die("Out of memory, realloc failed"); 76 + return ret; 77 + } 78 + 79 + static inline void *xcalloc(size_t nmemb, size_t size) 80 + { 81 + void *ret = calloc(nmemb, size); 82 + if (!ret) 83 + die("Out of memory, calloc failed"); 84 + return ret; 85 + } 86 + 87 + /* Sane ctype - no locale, and works with signed chars */ 88 + #undef isspace 89 + #undef isdigit 90 + #undef isalpha 91 + #undef isalnum 92 + #undef tolower 93 + #undef toupper 94 + extern unsigned char sane_ctype[256]; 95 + #define GIT_SPACE 0x01 96 + #define GIT_DIGIT 0x02 97 + #define GIT_ALPHA 0x04 98 + #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0) 99 + #define isspace(x) sane_istest(x,GIT_SPACE) 100 + #define isdigit(x) sane_istest(x,GIT_DIGIT) 101 + #define isalpha(x) sane_istest(x,GIT_ALPHA) 102 + #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT) 103 + #define tolower(x) sane_case((unsigned char)(x), 0x20) 104 + #define toupper(x) sane_case((unsigned char)(x), 0) 105 + 106 + static inline int sane_case(int x, int high) 107 + { 108 + if (sane_istest(x, GIT_ALPHA)) 109 + x = (x & ~0x20) | high; 110 + return x; 111 + } 112 + 113 + #endif
+6 -9
git.c
··· 8 8 #include <errno.h> 9 9 #include <limits.h> 10 10 #include <stdarg.h> 11 + #include "git-compat-util.h" 11 12 12 13 #ifndef PATH_MAX 13 14 # define PATH_MAX 4096 14 - #endif 15 - 16 - #ifdef NO_SETENV 17 - extern int gitsetenv(const char *, const char *, int); 18 15 #endif 19 16 20 17 static const char git_usage[] = ··· 156 153 } 157 154 158 155 #ifdef __GNUC__ 159 - static void usage(const char *exec_path, const char *fmt, ...) 156 + static void cmd_usage(const char *exec_path, const char *fmt, ...) 160 157 __attribute__((__format__(__printf__, 2, 3), __noreturn__)); 161 158 #endif 162 - static void usage(const char *exec_path, const char *fmt, ...) 159 + static void cmd_usage(const char *exec_path, const char *fmt, ...) 163 160 { 164 161 if (fmt) { 165 162 va_list ap; ··· 254 251 else if (!strcmp(arg, "help")) 255 252 show_help = 1; 256 253 else if (!show_help) 257 - usage(NULL, NULL); 254 + cmd_usage(NULL, NULL); 258 255 } 259 256 260 257 if (i >= argc || show_help) { 261 258 if (i >= argc) 262 - usage(exec_path, NULL); 259 + cmd_usage(exec_path, NULL); 263 260 264 261 show_man_page(argv[i]); 265 262 } ··· 297 294 execve(git_command, &argv[i], envp); 298 295 299 296 if (errno == ENOENT) 300 - usage(exec_path, "'%s' is not a git-command", argv[i]); 297 + cmd_usage(exec_path, "'%s' is not a git-command", argv[i]); 301 298 302 299 fprintf(stderr, "Failed to run command '%s': %s\n", 303 300 git_command, strerror(errno));
-4
mailinfo.c
··· 10 10 #include <iconv.h> 11 11 #include "cache.h" 12 12 13 - #ifdef NO_STRCASESTR 14 - extern char *gitstrcasestr(const char *haystack, const char *needle); 15 - #endif 16 - 17 13 static FILE *cmitmsg, *patchfile; 18 14 19 15 static int keep_subject = 0;
+1 -1
usage.c
··· 3 3 * 4 4 * Copyright (C) Linus Torvalds, 2005 5 5 */ 6 - #include "cache.h" 6 + #include "git-compat-util.h" 7 7 8 8 static void report(const char *prefix, const char *err, va_list params) 9 9 {