# [PATCH 05/11] Add a library function to add an alternate to the alternates file

3 messages from 2008-03-08 to 2008-03-08. Participants: Daniel Barkalow, Johannes Schindelin.
Thread: https://gitlist.dev/t/12590

## Daniel Barkalow, 2008-03-08 23:04

Subject: [PATCH 05/11] Add a library function to add an alternate to the alternates file
Message-ID: <alpine.LNX.1.00.0803081804090.19665@iabervon.org>
URL: https://gitlist.dev/e/alpine.LNX.1.00.0803081804090.19665%40iabervon.org

```
This is in the core so that, if the alternates file has already been
read, the addition can be parsed and put into effect for the current
process.

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
---
 cache.h     |    1 +
 sha1_file.c |   19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/cache.h b/cache.h
index bdefcc9..6aae174 100644
--- a/cache.h
+++ b/cache.h
@@ -587,6 +587,7 @@ extern struct alternate_object_database {
 	char base[FLEX_ARRAY]; /* more */
 } *alt_odb_list;
 extern void prepare_alt_odb(void);
+extern void add_to_alternates_file(const char *reference);
 
 struct pack_window {
 	struct pack_window *next;
diff --git a/sha1_file.c b/sha1_file.c
index 445a871..4ce4d9d 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -379,6 +379,25 @@ static void read_info_alternates(const char * relative_base, int depth)
 	munmap(map, mapsz);
 }
 
+void add_to_alternates_file(const char *reference)
+{
+	char *file;
+	char *alt;
+	int fd;
+
+	file = mkpath("%s/objects/info/alternates", get_git_dir());
+	fd = open(file, O_CREAT | O_WRONLY | O_APPEND, 0666);
+	if (fd < 0)
+		die("failed to create %s", file);
+	alt = mkpath("%s/objects\n", reference);
+	write_or_die(fd, alt, strlen(alt));
+	if (close(fd))
+		die("could not close %s", file);
+	fprintf(stderr, "Wrote %s to %s\n", alt, file);
+	if (alt_odb_tail)
+		link_alt_odb_entries(alt, alt + strlen(alt), '\n', NULL, 0);
+}
+
 void prepare_alt_odb(void)
 {
 	const char *alt;
-- 
1.5.4.3.327.g614d7.dirty


```

## Johannes Schindelin, 2008-03-08 23:18

Subject: Re: [PATCH 05/11] Add a library function to add an alternate to the alternates file
Message-ID: <alpine.LSU.1.00.0803090017150.3975@racer.site>
URL: https://gitlist.dev/e/alpine.LSU.1.00.0803090017150.3975%40racer.site
In-Reply-To: <alpine.LNX.1.00.0803081804090.19665@iabervon.org>

```
Hi,

On Sat, 8 Mar 2008, Daniel Barkalow wrote:


> +void add_to_alternates_file(const char *reference)
> +{
> +	char *file;
> +	char *alt;
> +	int fd;
> +
> +	file = mkpath("%s/objects/info/alternates", get_git_dir());
> +	fd = open(file, O_CREAT | O_WRONLY | O_APPEND, 0666);
> +	if (fd < 0)
> +		die("failed to create %s", file);

Even with alternates, which are probably written only rarely, I would have 
expected a lockfile procedure (if only to be consistent with the rest).

Ciao,
Dscho


```

## Daniel Barkalow, 2008-03-08 23:22

Subject: Re: [PATCH 05/11] Add a library function to add an alternate to the alternates file
Message-ID: <alpine.LNX.1.00.0803081820310.19665@iabervon.org>
URL: https://gitlist.dev/e/alpine.LNX.1.00.0803081820310.19665%40iabervon.org
In-Reply-To: <alpine.LSU.1.00.0803090017150.3975@racer.site>

```
On Sun, 9 Mar 2008, Johannes Schindelin wrote:

> Hi,
> 
> On Sat, 8 Mar 2008, Daniel Barkalow wrote:
> 
> 
> > +void add_to_alternates_file(const char *reference)
> > +{
> > +	char *file;
> > +	char *alt;
> > +	int fd;
> > +
> > +	file = mkpath("%s/objects/info/alternates", get_git_dir());
> > +	fd = open(file, O_CREAT | O_WRONLY | O_APPEND, 0666);
> > +	if (fd < 0)
> > +		die("failed to create %s", file);
> 
> Even with alternates, which are probably written only rarely, I would have 
> expected a lockfile procedure (if only to be consistent with the rest).

That's a good idea; I started with this inside clone, where you can be 
pretty certain not to get contention (and the shell version is using >> to 
update it), but in the library the lockfile is the right thing to do, so 
that other code can use it safely.

	-Daniel
*This .sig left intentionally blank*

```
