Skip to content

Commit

Permalink
archive: specfile support (--pretty=format: in archive files)
Browse files Browse the repository at this point in the history
Add support for a new attribute, specfile.  Files marked as being
specfiles are expanded by git-archive when they are written to an
archive.  It has no effect on worktree files.  The same placeholders
as those for the option --pretty=format: of git-log et al. can be
used.

The attribute is useful for creating auto-updating specfiles.  It is
limited by the underlying function format_commit_message(), though.
E.g. currently there is no placeholder for git-describe like output,
and expanded specfiles can't contain NUL bytes.  That can be fixed
in format_commit_message() later and will then benefit users of
git-log, too.

Signed-off-by: Rene Scharfe <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
René Scharfe authored and gitster committed Sep 3, 2007
1 parent 7b95089 commit 8460b2f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Documentation/gitattributes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,20 @@ frotz unspecified
----------------------------------------------------------------


Creating an archive
~~~~~~~~~~~~~~~~~~~

`specfile`
^^^^^^^^^^

If the attribute `specfile` 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].


GIT
---
Part of the gitlink:git[7] suite
5 changes: 4 additions & 1 deletion archive-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static unsigned long offset;
static time_t archive_time;
static int tar_umask = 002;
static int verbose;
static const struct commit *commit;

/* writes out the whole block, but only if it is full */
static void write_if_needed(void)
Expand Down Expand Up @@ -285,7 +286,8 @@ static int write_tar_entry(const unsigned char *sha1,
buffer = NULL;
size = 0;
} 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)
die("cannot read %s", sha1_to_hex(sha1));
}
Expand All @@ -304,6 +306,7 @@ int write_tar_archive(struct archiver_args *args)

archive_time = args->time;
verbose = args->verbose;
commit = args->commit;

if (args->commit_sha1)
write_global_extended_header(args->commit_sha1);
Expand Down
5 changes: 4 additions & 1 deletion archive-zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
static int verbose;
static int zip_date;
static int zip_time;
static const struct commit *commit;

static unsigned char *zip_dir;
static unsigned int zip_dir_size;
Expand Down Expand Up @@ -195,7 +196,8 @@ static int write_zip_entry(const unsigned char *sha1,
if (S_ISREG(mode) && zlib_compression_level != 0)
method = 8;
result = 0;
buffer = convert_sha1_file(path, sha1, mode, &type, &size);
buffer = sha1_file_to_archive(path, sha1, mode, &type, &size,
commit);
if (!buffer)
die("cannot read %s", sha1_to_hex(sha1));
crc = crc32(crc, buffer, size);
Expand Down Expand Up @@ -316,6 +318,7 @@ int write_zip_archive(struct archiver_args *args)
zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
verbose = args->verbose;
commit = args->commit;

if (args->base && plen > 0 && args->base[plen - 1] == '/') {
char *base = xstrdup(args->base);
Expand Down
3 changes: 3 additions & 0 deletions archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct archiver_args {
const char *base;
struct tree *tree;
const unsigned char *commit_sha1;
const struct commit *commit;
time_t time;
const char **pathspec;
unsigned int verbose : 1;
Expand Down Expand Up @@ -42,4 +43,6 @@ extern int write_tar_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 *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 */
55 changes: 54 additions & 1 deletion builtin-archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "exec_cmd.h"
#include "pkt-line.h"
#include "sideband.h"
#include "attr.h"

static const char archive_usage[] = \
"git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
Expand Down Expand Up @@ -80,6 +81,57 @@ static int run_remote_archiver(const char *remote, int argc,
return !!rv;
}

static void *convert_to_archive(const char *path,
const void *src, unsigned long *sizep,
const struct commit *commit)
{
static struct git_attr *attr_specfile;
struct git_attr_check check[1];
char *interpolated = NULL;
unsigned long allocated = 0;

if (!commit)
return NULL;

if (!attr_specfile)
attr_specfile = git_attr("specfile", 8);

check[0].attr = attr_specfile;
if (git_checkattr(path, ARRAY_SIZE(check), check))
return NULL;
if (!ATTR_TRUE(check[0].value))
return NULL;

*sizep = format_commit_message(commit, src, &interpolated, &allocated);

return interpolated;
}

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)
{
int rv = -1, i;
Expand Down Expand Up @@ -109,7 +161,7 @@ void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
const unsigned char *commit_sha1;
time_t archive_time;
struct tree *tree;
struct commit *commit;
const struct commit *commit;
unsigned char sha1[20];

if (get_sha1(name, sha1))
Expand Down Expand Up @@ -142,6 +194,7 @@ void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
}
ar_args->tree = tree;
ar_args->commit_sha1 = commit_sha1;
ar_args->commit = commit;
ar_args->time = archive_time;
}

Expand Down
19 changes: 19 additions & 0 deletions t/t5000-tar-tree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ commit id embedding:
TAR=${TAR:-tar}
UNZIP=${UNZIP:-unzip}

SPECFILEFORMAT=%H%n

test_expect_success \
'populate workdir' \
'mkdir a b c &&
echo simple textfile >a/a &&
mkdir a/bin &&
cp /bin/sh a/bin &&
printf "%s" "$SPECFILEFORMAT" >a/specfile &&
ln -s a a/l1 &&
(p=long_path_to_a_file && cd a &&
for depth in 1 2 3 4 5; do mkdir $p && cd $p; done &&
Expand Down Expand Up @@ -104,6 +107,22 @@ test_expect_success \
'validate file contents with prefix' \
'diff -r a c/prefix/a'

test_expect_success \
'create an archive with a specfile' \
'echo specfile specfile >a/.gitattributes &&
git archive HEAD >f.tar &&
rm a/.gitattributes'

test_expect_success \
'extract specfile' \
'(mkdir f && cd f && $TAR xf -) <f.tar'

test_expect_success \
'validate specfile contents' \
'git log --max-count=1 "--pretty=format:$SPECFILEFORMAT" HEAD \
>f/a/specfile.expected &&
diff f/a/specfile.expected f/a/specfile'

test_expect_success \
'git archive --format=zip' \
'git archive --format=zip HEAD >d.zip'
Expand Down

0 comments on commit 8460b2f

Please sign in to comment.