* rs/archive:
  archive - leakfix for format_subst()
  Define NO_MEMMEM on Darwin as it lacks the function
  archive: rename attribute specfile to export-subst
  archive: specfile syntax change: "$Format:%PLCHLDR$" instead of just "%PLCHLDR" (take 2)
  add memmem()
  Remove unused function convert_sha1_file()
  archive: specfile support (--pretty=format: in archive files)
  Export format_commit_message()
This commit is contained in:
Junio C Hamano 2007-09-10 00:14:50 -07:00
Родитель 9784c5c53e 20fbfd869f
Коммит 6972ab7ae1
13 изменённых файлов: 197 добавлений и 23 удалений

Просмотреть файл

@ -421,6 +421,23 @@ frotz unspecified
---------------------------------------------------------------- ----------------------------------------------------------------
Creating an archive
~~~~~~~~~~~~~~~~~~~
`export-subst`
^^^^^^^^^^^^^^
If the attribute `export-subst` is set for a file then git will expand
several placeholders when adding this file to an archive. The
expansion depends on the availability of a commit ID, i.e. if
gitlink:git-archive[1] has been given a tree instead of a commit or a
tag then no replacement will be done. The placeholders are the same
as those for the option `--pretty=format:` of gitlink:git-log[1],
except that they need to be wrapped like this: `$Format:PLACEHOLDERS$`
in the file. E.g. the string `$Format:%H$` will be replaced by the
commit hash.
GIT GIT
--- ---
Part of the gitlink:git[7] suite Part of the gitlink:git[7] suite

Просмотреть файл

@ -28,6 +28,8 @@ all::
# #
# Define NO_STRCASESTR if you don't have strcasestr. # Define NO_STRCASESTR if you don't have strcasestr.
# #
# Define NO_MEMMEM if you don't have memmem.
#
# Define NO_STRLCPY if you don't have strlcpy. # Define NO_STRLCPY if you don't have strlcpy.
# #
# Define NO_STRTOUMAX if you don't have strtoumax in the C library. # Define NO_STRTOUMAX if you don't have strtoumax in the C library.
@ -396,12 +398,14 @@ ifeq ($(uname_S),Darwin)
NEEDS_LIBICONV = YesPlease NEEDS_LIBICONV = YesPlease
OLD_ICONV = UnfortunatelyYes OLD_ICONV = UnfortunatelyYes
NO_STRLCPY = YesPlease NO_STRLCPY = YesPlease
NO_MEMMEM = YesPlease
endif endif
ifeq ($(uname_S),SunOS) ifeq ($(uname_S),SunOS)
NEEDS_SOCKET = YesPlease NEEDS_SOCKET = YesPlease
NEEDS_NSL = YesPlease NEEDS_NSL = YesPlease
SHELL_PATH = /bin/bash SHELL_PATH = /bin/bash
NO_STRCASESTR = YesPlease NO_STRCASESTR = YesPlease
NO_MEMMEM = YesPlease
NO_HSTRERROR = YesPlease NO_HSTRERROR = YesPlease
ifeq ($(uname_R),5.8) ifeq ($(uname_R),5.8)
NEEDS_LIBICONV = YesPlease NEEDS_LIBICONV = YesPlease
@ -424,6 +428,7 @@ ifeq ($(uname_O),Cygwin)
NO_D_TYPE_IN_DIRENT = YesPlease NO_D_TYPE_IN_DIRENT = YesPlease
NO_D_INO_IN_DIRENT = YesPlease NO_D_INO_IN_DIRENT = YesPlease
NO_STRCASESTR = YesPlease NO_STRCASESTR = YesPlease
NO_MEMMEM = YesPlease
NO_SYMLINK_HEAD = YesPlease NO_SYMLINK_HEAD = YesPlease
NEEDS_LIBICONV = YesPlease NEEDS_LIBICONV = YesPlease
NO_FAST_WORKING_DIRECTORY = UnfortunatelyYes NO_FAST_WORKING_DIRECTORY = UnfortunatelyYes
@ -442,6 +447,7 @@ ifeq ($(uname_S),FreeBSD)
endif endif
ifeq ($(uname_S),OpenBSD) ifeq ($(uname_S),OpenBSD)
NO_STRCASESTR = YesPlease NO_STRCASESTR = YesPlease
NO_MEMMEM = YesPlease
NEEDS_LIBICONV = YesPlease NEEDS_LIBICONV = YesPlease
BASIC_CFLAGS += -I/usr/local/include BASIC_CFLAGS += -I/usr/local/include
BASIC_LDFLAGS += -L/usr/local/lib BASIC_LDFLAGS += -L/usr/local/lib
@ -456,6 +462,7 @@ ifeq ($(uname_S),NetBSD)
endif endif
ifeq ($(uname_S),AIX) ifeq ($(uname_S),AIX)
NO_STRCASESTR=YesPlease NO_STRCASESTR=YesPlease
NO_MEMMEM = YesPlease
NO_STRLCPY = YesPlease NO_STRLCPY = YesPlease
NEEDS_LIBICONV=YesPlease NEEDS_LIBICONV=YesPlease
endif endif
@ -467,6 +474,7 @@ ifeq ($(uname_S),IRIX64)
NO_IPV6=YesPlease NO_IPV6=YesPlease
NO_SETENV=YesPlease NO_SETENV=YesPlease
NO_STRCASESTR=YesPlease NO_STRCASESTR=YesPlease
NO_MEMMEM = YesPlease
NO_STRLCPY = YesPlease NO_STRLCPY = YesPlease
NO_SOCKADDR_STORAGE=YesPlease NO_SOCKADDR_STORAGE=YesPlease
SHELL_PATH=/usr/gnu/bin/bash SHELL_PATH=/usr/gnu/bin/bash
@ -661,6 +669,10 @@ ifdef NO_HSTRERROR
COMPAT_CFLAGS += -DNO_HSTRERROR COMPAT_CFLAGS += -DNO_HSTRERROR
COMPAT_OBJS += compat/hstrerror.o COMPAT_OBJS += compat/hstrerror.o
endif endif
ifdef NO_MEMMEM
COMPAT_CFLAGS += -DNO_MEMMEM
COMPAT_OBJS += compat/memmem.o
endif
ifeq ($(TCLTK_PATH),) ifeq ($(TCLTK_PATH),)
NO_TCLTK=NoThanks NO_TCLTK=NoThanks

Просмотреть файл

@ -17,6 +17,7 @@ static unsigned long offset;
static time_t archive_time; static time_t archive_time;
static int tar_umask = 002; static int tar_umask = 002;
static int verbose; static int verbose;
static const struct commit *commit;
/* writes out the whole block, but only if it is full */ /* writes out the whole block, but only if it is full */
static void write_if_needed(void) static void write_if_needed(void)
@ -285,7 +286,8 @@ static int write_tar_entry(const unsigned char *sha1,
buffer = NULL; buffer = NULL;
size = 0; size = 0;
} else { } else {
buffer = convert_sha1_file(path.buf, sha1, mode, &type, &size); buffer = sha1_file_to_archive(path.buf, sha1, mode, &type,
&size, commit);
if (!buffer) if (!buffer)
die("cannot read %s", sha1_to_hex(sha1)); die("cannot read %s", sha1_to_hex(sha1));
} }
@ -304,6 +306,7 @@ int write_tar_archive(struct archiver_args *args)
archive_time = args->time; archive_time = args->time;
verbose = args->verbose; verbose = args->verbose;
commit = args->commit;
if (args->commit_sha1) if (args->commit_sha1)
write_global_extended_header(args->commit_sha1); write_global_extended_header(args->commit_sha1);

Просмотреть файл

@ -12,6 +12,7 @@
static int verbose; static int verbose;
static int zip_date; static int zip_date;
static int zip_time; static int zip_time;
static const struct commit *commit;
static unsigned char *zip_dir; static unsigned char *zip_dir;
static unsigned int zip_dir_size; static unsigned int zip_dir_size;
@ -195,7 +196,8 @@ static int write_zip_entry(const unsigned char *sha1,
if (S_ISREG(mode) && zlib_compression_level != 0) if (S_ISREG(mode) && zlib_compression_level != 0)
method = 8; method = 8;
result = 0; result = 0;
buffer = convert_sha1_file(path, sha1, mode, &type, &size); buffer = sha1_file_to_archive(path, sha1, mode, &type, &size,
commit);
if (!buffer) if (!buffer)
die("cannot read %s", sha1_to_hex(sha1)); die("cannot read %s", sha1_to_hex(sha1));
crc = crc32(crc, buffer, size); crc = crc32(crc, buffer, size);
@ -316,6 +318,7 @@ int write_zip_archive(struct archiver_args *args)
zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE); zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
zip_dir_size = ZIP_DIRECTORY_MIN_SIZE; zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
verbose = args->verbose; verbose = args->verbose;
commit = args->commit;
if (args->base && plen > 0 && args->base[plen - 1] == '/') { if (args->base && plen > 0 && args->base[plen - 1] == '/') {
char *base = xstrdup(args->base); char *base = xstrdup(args->base);

Просмотреть файл

@ -8,6 +8,7 @@ struct archiver_args {
const char *base; const char *base;
struct tree *tree; struct tree *tree;
const unsigned char *commit_sha1; const unsigned char *commit_sha1;
const struct commit *commit;
time_t time; time_t time;
const char **pathspec; const char **pathspec;
unsigned int verbose : 1; unsigned int verbose : 1;
@ -42,4 +43,6 @@ extern int write_tar_archive(struct archiver_args *);
extern int write_zip_archive(struct archiver_args *); extern int write_zip_archive(struct archiver_args *);
extern void *parse_extra_zip_args(int argc, const char **argv); extern void *parse_extra_zip_args(int argc, const char **argv);
extern void *sha1_file_to_archive(const char *path, const unsigned char *sha1, unsigned int mode, enum object_type *type, unsigned long *size, const struct commit *commit);
#endif /* ARCHIVE_H */ #endif /* ARCHIVE_H */

Просмотреть файл

@ -10,6 +10,7 @@
#include "exec_cmd.h" #include "exec_cmd.h"
#include "pkt-line.h" #include "pkt-line.h"
#include "sideband.h" #include "sideband.h"
#include "attr.h"
static const char archive_usage[] = \ static const char archive_usage[] = \
"git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]"; "git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
@ -80,6 +81,100 @@ static int run_remote_archiver(const char *remote, int argc,
return !!rv; return !!rv;
} }
static void *format_subst(const struct commit *commit, const char *format,
unsigned long *sizep)
{
unsigned long len = *sizep, result_len = 0;
const char *a = format;
char *result = NULL;
for (;;) {
const char *b, *c;
char *fmt, *formatted = NULL;
unsigned long a_len, fmt_len, formatted_len, allocated = 0;
b = memmem(a, len, "$Format:", 8);
if (!b || a + len < b + 9)
break;
c = memchr(b + 8, '$', len - 8);
if (!c)
break;
a_len = b - a;
fmt_len = c - b - 8;
fmt = xmalloc(fmt_len + 1);
memcpy(fmt, b + 8, fmt_len);
fmt[fmt_len] = '\0';
formatted_len = format_commit_message(commit, fmt, &formatted,
&allocated);
free(fmt);
result = xrealloc(result, result_len + a_len + formatted_len);
memcpy(result + result_len, a, a_len);
memcpy(result + result_len + a_len, formatted, formatted_len);
result_len += a_len + formatted_len;
len -= c + 1 - a;
a = c + 1;
}
if (result && len) {
result = xrealloc(result, result_len + len);
memcpy(result + result_len, a, len);
result_len += len;
}
*sizep = result_len;
return result;
}
static void *convert_to_archive(const char *path,
const void *src, unsigned long *sizep,
const struct commit *commit)
{
static struct git_attr *attr_export_subst;
struct git_attr_check check[1];
if (!commit)
return NULL;
if (!attr_export_subst)
attr_export_subst = git_attr("export-subst", 12);
check[0].attr = attr_export_subst;
if (git_checkattr(path, ARRAY_SIZE(check), check))
return NULL;
if (!ATTR_TRUE(check[0].value))
return NULL;
return format_subst(commit, src, sizep);
}
void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
unsigned int mode, enum object_type *type,
unsigned long *size,
const struct commit *commit)
{
void *buffer, *converted;
buffer = read_sha1_file(sha1, type, size);
if (buffer && S_ISREG(mode)) {
converted = convert_to_working_tree(path, buffer, size);
if (converted) {
free(buffer);
buffer = converted;
}
converted = convert_to_archive(path, buffer, size, commit);
if (converted) {
free(buffer);
buffer = converted;
}
}
return buffer;
}
static int init_archiver(const char *name, struct archiver *ar) static int init_archiver(const char *name, struct archiver *ar)
{ {
int rv = -1, i; int rv = -1, i;
@ -109,7 +204,7 @@ void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
const unsigned char *commit_sha1; const unsigned char *commit_sha1;
time_t archive_time; time_t archive_time;
struct tree *tree; struct tree *tree;
struct commit *commit; const struct commit *commit;
unsigned char sha1[20]; unsigned char sha1[20];
if (get_sha1(name, sha1)) if (get_sha1(name, sha1))
@ -142,6 +237,7 @@ void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
} }
ar_args->tree = tree; ar_args->tree = tree;
ar_args->commit_sha1 = commit_sha1; ar_args->commit_sha1 = commit_sha1;
ar_args->commit = commit;
ar_args->time = archive_time; ar_args->time = archive_time;
} }

Просмотреть файл

@ -592,7 +592,6 @@ extern void trace_argv_printf(const char **argv, int count, const char *format,
/* convert.c */ /* convert.c */
extern char *convert_to_git(const char *path, const char *src, unsigned long *sizep); extern char *convert_to_git(const char *path, const char *src, unsigned long *sizep);
extern char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep); extern char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep);
extern void *convert_sha1_file(const char *path, const unsigned char *sha1, unsigned int mode, enum object_type *type, unsigned long *size);
/* diff.c */ /* diff.c */
extern int diff_auto_refresh_index; extern int diff_auto_refresh_index;

Просмотреть файл

@ -787,8 +787,8 @@ static void fill_person(struct interp *table, const char *msg, int len)
interp_set_entry(table, 6, show_date(date, tz, DATE_ISO8601)); interp_set_entry(table, 6, show_date(date, tz, DATE_ISO8601));
} }
static long format_commit_message(const struct commit *commit, long format_commit_message(const struct commit *commit, const void *format,
const char *msg, char **buf_p, unsigned long *space_p) char **buf_p, unsigned long *space_p)
{ {
struct interp table[] = { struct interp table[] = {
{ "%H" }, /* commit hash */ { "%H" }, /* commit hash */
@ -843,6 +843,7 @@ static long format_commit_message(const struct commit *commit,
char parents[1024]; char parents[1024];
int i; int i;
enum { HEADER, SUBJECT, BODY } state; enum { HEADER, SUBJECT, BODY } state;
const char *msg = commit->buffer;
if (ILEFT_RIGHT + 1 != ARRAY_SIZE(table)) if (ILEFT_RIGHT + 1 != ARRAY_SIZE(table))
die("invalid interp table!"); die("invalid interp table!");
@ -924,7 +925,7 @@ static long format_commit_message(const struct commit *commit,
char *buf = *buf_p; char *buf = *buf_p;
unsigned long space = *space_p; unsigned long space = *space_p;
space = interpolate(buf, space, user_format, space = interpolate(buf, space, format,
table, ARRAY_SIZE(table)); table, ARRAY_SIZE(table));
if (!space) if (!space)
break; break;
@ -1165,7 +1166,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
char *buf; char *buf;
if (fmt == CMIT_FMT_USERFORMAT) if (fmt == CMIT_FMT_USERFORMAT)
return format_commit_message(commit, msg, buf_p, space_p); return format_commit_message(commit, user_format, buf_p, space_p);
encoding = (git_log_output_encoding encoding = (git_log_output_encoding
? git_log_output_encoding ? git_log_output_encoding

Просмотреть файл

@ -61,6 +61,7 @@ enum cmit_fmt {
}; };
extern enum cmit_fmt get_commit_format(const char *arg); extern enum cmit_fmt get_commit_format(const char *arg);
extern long format_commit_message(const struct commit *commit, const void *template, char **buf_p, unsigned long *space_p);
extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char **buf_p, unsigned long *space_p, int abbrev, const char *subject, const char *after_subject, enum date_mode dmode); extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char **buf_p, unsigned long *space_p, int abbrev, const char *subject, const char *after_subject, enum date_mode dmode);
/** Removes the first commit from a list sorted by date, and adds all /** Removes the first commit from a list sorted by date, and adds all

29
compat/memmem.c Normal file
Просмотреть файл

@ -0,0 +1,29 @@
#include "../git-compat-util.h"
void *gitmemmem(const void *haystack, size_t haystack_len,
const void *needle, size_t needle_len)
{
const char *begin = haystack;
const char *last_possible = begin + haystack_len - needle_len;
/*
* The first occurrence of the empty string is deemed to occur at
* the beginning of the string.
*/
if (needle_len == 0)
return (void *)begin;
/*
* Sanity check, otherwise the loop might search through the whole
* memory.
*/
if (haystack_len < needle_len)
return NULL;
for (; begin <= last_possible; begin++) {
if (!memcmp(begin, needle, needle_len))
return (void *)begin;
}
return NULL;
}

Просмотреть файл

@ -687,18 +687,3 @@ char *convert_to_working_tree(const char *path, const char *src, unsigned long *
return buf; return buf;
} }
void *convert_sha1_file(const char *path, const unsigned char *sha1,
unsigned int mode, enum object_type *type,
unsigned long *size)
{
void *buffer = read_sha1_file(sha1, type, size);
if (S_ISREG(mode) && buffer) {
void *converted = convert_to_working_tree(path, buffer, size);
if (converted) {
free(buffer);
buffer = converted;
}
}
return buffer;
}

Просмотреть файл

@ -172,6 +172,12 @@ extern uintmax_t gitstrtoumax(const char *, char **, int);
extern const char *githstrerror(int herror); extern const char *githstrerror(int herror);
#endif #endif
#ifdef NO_MEMMEM
#define memmem gitmemmem
void *gitmemmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen);
#endif
extern void release_pack_memory(size_t, int); extern void release_pack_memory(size_t, int);
static inline char* xstrdup(const char *str) static inline char* xstrdup(const char *str)

Просмотреть файл

@ -28,12 +28,15 @@ commit id embedding:
TAR=${TAR:-tar} TAR=${TAR:-tar}
UNZIP=${UNZIP:-unzip} UNZIP=${UNZIP:-unzip}
SUBSTFORMAT=%H%n
test_expect_success \ test_expect_success \
'populate workdir' \ 'populate workdir' \
'mkdir a b c && 'mkdir a b c &&
echo simple textfile >a/a && echo simple textfile >a/a &&
mkdir a/bin && mkdir a/bin &&
cp /bin/sh a/bin && cp /bin/sh a/bin &&
printf "A\$Format:%s\$O" "$SUBSTFORMAT" >a/substfile &&
ln -s a a/l1 && ln -s a a/l1 &&
(p=long_path_to_a_file && cd a && (p=long_path_to_a_file && cd a &&
for depth in 1 2 3 4 5; do mkdir $p && cd $p; done && for depth in 1 2 3 4 5; do mkdir $p && cd $p; done &&
@ -104,6 +107,22 @@ test_expect_success \
'validate file contents with prefix' \ 'validate file contents with prefix' \
'diff -r a c/prefix/a' 'diff -r a c/prefix/a'
test_expect_success \
'create an archive with a substfile' \
'echo substfile export-subst >a/.gitattributes &&
git archive HEAD >f.tar &&
rm a/.gitattributes'
test_expect_success \
'extract substfile' \
'(mkdir f && cd f && $TAR xf -) <f.tar'
test_expect_success \
'validate substfile contents' \
'git log --max-count=1 "--pretty=format:A${SUBSTFORMAT}O" HEAD \
>f/a/substfile.expected &&
diff f/a/substfile.expected f/a/substfile'
test_expect_success \ test_expect_success \
'git archive --format=zip' \ 'git archive --format=zip' \
'git archive --format=zip HEAD >d.zip' 'git archive --format=zip HEAD >d.zip'