cgit

commit c1572bb5ec4540b5008490cf471cc4a5e65ef728

Author: John Keeping <john@keeping.me.uk>

Add "snapshot-prefix" repo configuration

Allow using a user-specified value for the prefix in snapshot files
instead of the repository basename.  For example, files downloaded from
the linux-stable.git repository should be named linux-$VERSION and not
linux-stable-$VERSION, which can be achieved by setting:

	repo.snapshot-prefix=linux

Signed-off-by: John Keeping <john@keeping.me.uk>
Reviewed-by: Christian Hesse <mail@eworm.de>

 cgit.c | 2 ++
 cgit.h | 1 +
 cgitrc.5.txt | 7 +++++++
 ui-refs.c | 2 +-
 ui-shared.c | 10 +++++++++-
 ui-shared.h | 1 +
 ui-snapshot.c | 4 ++--


diff --git a/cgit.c b/cgit.c
index bd9cb3fb11a66383d56256b2c0426e0070e7208c..d2f7b9c7bbb4f30e0c2ee813c79b50466e1a5d3e 100644
--- a/cgit.c
+++ b/cgit.c
@@ -79,6 +79,8 @@ 		item = string_list_append(&repo->submodules, xstrdup(path));
 		item->util = xstrdup(value);
 	} else if (!strcmp(name, "section"))
 		repo->section = xstrdup(value);
+	else if (!strcmp(name, "snapshot-prefix"))
+		repo->snapshot_prefix = xstrdup(value);
 	else if (!strcmp(name, "readme") && value != NULL) {
 		if (repo->readme.items == ctx.cfg.readme.items)
 			memset(&repo->readme, 0, sizeof(repo->readme));




diff --git a/cgit.h b/cgit.h
index 005ae63017050f0e6d5821fd51515ca0775f4469..847cd2e075ff7efc3d71eb01e4113ab8faa580c9 100644
--- a/cgit.h
+++ b/cgit.h
@@ -88,6 +88,7 @@ 	char *section;
 	char *clone_url;
 	char *logo;
 	char *logo_link;
+	char *snapshot_prefix;
 	int snapshots;
 	int enable_commit_graph;
 	int enable_log_filecount;




diff --git a/cgitrc.5.txt b/cgitrc.5.txt
index 4da166c51f90ed16a12731179a9988a674fdbf0b..a9d3d0a4052a93e59c220208adf503314a867d5e 100644
--- a/cgitrc.5.txt
+++ b/cgitrc.5.txt
@@ -599,6 +599,13 @@ 	A mask of snapshot formats for this repo that cgit generates links for,
 	restricted by the global "snapshots" setting. Default value:
 	<snapshots>.
 
+repo.snapshot-prefix::
+	Prefix to use for snapshot links instead of the repository basename.
+	For example, the "linux-stable" repository may wish to set this to
+	"linux" so that snapshots are in the format "linux-3.15.4" instead
+	of "linux-stable-3.15.4".  Default value: <empty> meaning to use
+	the repository basename.
+
 repo.section::
 	Override the current section name for this repository. Default value:
 	none.




diff --git a/ui-refs.c b/ui-refs.c
index 75f2789fc1653b76509f2ff3f8a9120051dd30e6..50d9d3002951760c4cf228d0cce6af7c016b9511 100644
--- a/ui-refs.c
+++ b/ui-refs.c
@@ -100,7 +100,7 @@
 	if (!ref || strlen(ref) < 1)
 		return;
 
-	basename = cgit_repobasename(repo->url);
+	basename = cgit_snapshot_prefix(repo);
 	if (starts_with(ref, basename))
 		strbuf_addstr(&filename, ref);
 	else




diff --git a/ui-shared.c b/ui-shared.c
index e719c1b00303e7258df96f18a96b0c1482217ef7..d8578739576f090256f4b1bbe8232f1ff77f0b36 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -152,6 +152,14 @@ 		return ++rv;
 	return rvbuf;
 }
 
+const char *cgit_snapshot_prefix(const struct cgit_repo *repo)
+{
+	if (repo->snapshot_prefix)
+		return repo->snapshot_prefix;
+
+	return cgit_repobasename(repo->url);
+}
+
 static void site_url(const char *page, const char *search, const char *sort, int ofs, int always_root)
 {
 	char *delim = "?";
@@ -1110,7 +1118,7 @@ 	const struct cgit_snapshot_format* f;
 	struct strbuf filename = STRBUF_INIT;
 	size_t prefixlen;
 
-	cgit_compose_snapshot_prefix(&filename, cgit_repobasename(repo->url), hex);
+	cgit_compose_snapshot_prefix(&filename, cgit_snapshot_prefix(repo), hex);
 	prefixlen = filename.len;
 	for (f = cgit_snapshot_formats; f->suffix; f++) {
 		if (!(repo->snapshots & f->bit))




diff --git a/ui-shared.h b/ui-shared.h
index b3eb8c5be63f763bb6c3f24d132c2b50b5803d5c..92a1755012f163370bd4782f9a2bbcf30c8983ae 100644
--- a/ui-shared.h
+++ b/ui-shared.h
@@ -78,6 +78,7 @@ extern void cgit_compose_snapshot_prefix(struct strbuf *filename,
 					 const char *base, const char *ref);
 extern void cgit_print_snapshot_links(const struct cgit_repo *repo,
 				      const char *head, const char *hex);
+extern const char *cgit_snapshot_prefix(const struct cgit_repo *repo);
 extern void cgit_add_hidden_formfields(int incl_head, int incl_search,
 				       const char *page);
 




diff --git a/ui-snapshot.c b/ui-snapshot.c
index 237a75f76d71d3e1be29cb98b4e5b6f4fd333060..b9e2a36f9dd57a475c364437c7de82d487986565 100644
--- a/ui-snapshot.c
+++ b/ui-snapshot.c
@@ -154,7 +154,7 @@
 	if (get_oid(snapshot.buf, &oid) == 0)
 		goto out;
 
-	reponame = cgit_repobasename(repo->url);
+	reponame = cgit_snapshot_prefix(repo);
 	if (starts_with(snapshot.buf, reponame)) {
 		const char *new_start = snapshot.buf;
 		new_start += strlen(reponame);
@@ -214,7 +214,7 @@ 	if (!hex)
 		hex = head;
 
 	if (!prefix)
-		prefix = xstrdup(cgit_repobasename(ctx.repo->url));
+		prefix = xstrdup(cgit_snapshot_prefix(ctx.repo));
 
 	make_snapshot(f, hex, prefix, filename);
 	free(prefix);