Skip to contentSkip to conversation

Allow pack header preprocessing before unpack-objects/index-pack.

PATCH In review Nicolas Pitre 3 participants 13 messages Aug 11, 2016

Subsystems (2)
lore
Share & tools
lore
#6 lore raw

Re: [PATCH 6/6] remove .keep pack lock files when done with refs update

In reply to Nicolas Pitre

git-fetch.sh do · line 57

Quoted 11 lines · Nicolas Pitre
git-fetch.sh +1 -1
@@ -51,7 +51,7 @@ do
 		verbose=Yes
 		;;
 	-k|--k|--ke|--kee|--keep)
-		keep=--keep
+		keep=-k -k
 		;;
 	--reflog-action=*)
 		rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`

Micronit. This cannot be right without quoting.

#10 lore raw

Re: [PATCH 6/6] remove .keep pack lock files when done with refs update

In reply to Nicolas Pitre
Quoted 4 lines · Nicolas Pitre

This makes both git-fetch and git-push (fetch-pack and receive-pack) safe against a possible race with aparallel git-repack -a -d that could prune the new pack while it is not yet referenced, and remove the .keep file after refs have been updated.

Thanks for finishing this series out. I've gotten sidetracked the past two days and wasn't able to finish it myself. Nicely done too, your code looks much better than my drafts... :-)

#11 lore raw
In reply to Nicolas Pitre

Rather nicely done. I see upto 2/6 are identical to what has sitting in "pu" for the past few days, modulo some indentation?

I wonder how much testing has this series seen, by the way.

Also, I wonder what happens after 5/6 errors out. It dies and the caller (typically receive-pack) reports it back, which would fail the push (and does not update the refs). Retrying in such a case would probably use the same set of refs on both ends, resulting in exactly the same pack...

#12 lore raw
Quoted 2 lines · Junio C Hamano

Rather nicely done. I see upto 2/6 are identical to what has sitting in "pu" for the past few days, modulo some indentation?

Yes. I just inserted them in the serie as I didn't see them in pu until a very recent pull.

Quoted 1 line

I wonder how much testing has this series seen, by the way.

It has been tested a bit. At least until it started behaving correctly for me. There might still be bugs remaining, but nothing that should screw a repository up. I think it should be good enough for next.

Quoted 5 lines

Also, I wonder what happens after 5/6 errors out. It dies and the caller (typically receive-pack) reports it back, which would fail the push (and does not update the refs). Retrying in such a case would probably use the same set of refs on both ends, resulting in exactly the same pack...

Well, the purpose of 5/6 is to avoid creating a .keep file if it already exists. This is more a security thing so if you decide to mark one of your pack with .keep (say an history pack that doesn't need to be repacked) then you don't want a funny git server to send you the same pack just for the purpose of overwriting the same .keep file (that could be removed right away if it ends up being considered as a lock file). This is made more obvious with 6/6 as the .keep file is not removed if it was there before. So this is not actually an error if creating a .keep file fails with EEXIST.

As for index-pack dying, it will be reported the same way as unpack-objects dying -- no difference there. Of course if it dies on the first push, it is likely to die on all subsequent pushes since the condition for that to happen is most probably due to a serious problem (out of disk space, corruption in the existing objects in the repository, etc).

The pack is put in place with move_temp_to_file() so if a previous attempt already stored a pack with the same name before successfully updating corresponding refs then the old pack will silently be kept since we know both packs contain the same objects when they have the same name. When a pack is moved it is always complete so there shouldn't be any (new) instance of repository corruption in normal circumbstances even in the presence of index-pack dying.

#8 lore raw

Re: [PATCH 6/6] remove .keep pack lock files when done with refs update

In reply to Nicolas Pitre
Quoted 10 lines · Nicolas Pitre

Another area of that patch I'm not really sure of is this:

git-fetch.sh +1 -0
@@ -408,6 +415,7 @@ fetch_main () {
          append_fetch_head "$sha1" "$remote" \
                  "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
       done
+      if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
     ) || exit ;;
   esac
 
Quoted 2 lines

How can I make sure to not clobber a non-zero return code with the rm that would prevent the exit while still performing the rm in all cases?

OK looking at it again I think the || exit is only meant to match the exit 1 before that point. Since this is a subshell then the exit 1 wouldn't exit it all on its own. Otherwise there is no errors expected to trickle out of the while loop and I _think_ the above is correct.

Do you still want a new patch for the micronit quoting issue?

#7 lore raw

Re: [PATCH 6/6] remove .keep pack lock files when done with refs update

In reply to Junio C Hamano
Quoted 15 lines · Junio C Hamano

git-fetch.sh do · line 57

Quoted 11 lines · Nicolas Pitre
git-fetch.sh +1 -1
@@ -51,7 +51,7 @@ do
 		verbose=Yes
 		;;
 	-k|--k|--ke|--kee|--keep)
-		keep=--keep
+		keep=-k -k
 		;;
 	--reflog-action=*)
 		rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`

Micronit. This cannot be right without quoting.

OK. Shell programming isn't really my turf.

Another area of that patch I'm not really sure of is this:

@@ -408,6 +415,7 @@ fetch_main () {
          append_fetch_head "$sha1" "$remote" \
                  "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
       done
+      if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
     ) || exit ;;

esac

How can I make sure to not clobber a non-zero return code with the rm that would prevent the exit while still performing the rm in all cases?

#3 lore raw

PATCH 4/6 improve fetch-pack's handling of kept packs

In reply to Nicolas Pitre

Since functions in fetch-clone.c were only used from fetch-pack.c, its content has been merged with fetch-pack.c. This allows for better coupling of features with much simpler implementations.

One new thing is that the (abscence of) --thin also enforce it on index-pack now, such that index-pack will abort if a thin pack was _not_ asked for.

The -k or --keep, when provided twice, now causes the fetched pack to be left as a kept pack just like receive-pack currently does. Eventually this will be used to close a race against concurrent repacking.

---
 Documentation/git-fetch-pack.txt |    3 +-
 Makefile                         |    2 +-
 cache.h                          |    4 --
 fetch-clone.c                    |   87 ------------------------------
 fetch-pack.c                     |  110 +++++++++++++++++++++++++++++++++++---
 5 files changed, 106 insertions(+), 100 deletions(-)
Documentation/git-fetch-pack.txt +2 -1
index bff9aa6..3e6cd88 100644
@@ -32,7 +32,8 @@ OPTIONS
 -k::
 	Do not invoke 'git-unpack-objects' on received data, but
 	create a single packfile out of it instead, and store it
-	in the object database.
+	in the object database. If provided twice then the pack is
+	locked against repacking.
 
 --exec=<git-upload-pack>::
 	Use this to specify the path to 'git-upload-pack' on the
Makefile +1 -1
index 28a5d91..80d4a4e 100644
@@ -260,7 +260,7 @@ LIB_OBJS = \
 	quote.o read-cache.o refs.o run-command.o dir.o object-refs.o \
 	server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
 	tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
-	fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
+	revision.o pager.o tree-walk.o xdiff-interface.o \
 	write_or_die.o trace.o list-objects.o grep.o \
 	alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
 	color.o wt-status.o archive-zip.o archive-tar.o
cache.h +0 -4
index 6cb7e1d..f2ec5c8 100644
@@ -416,10 +416,6 @@ extern int copy_fd(int ifd, int ofd);
 extern void write_or_die(int fd, const void *buf, size_t count);
 extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);
 
-/* Finish off pack transfer receiving end */
-extern int receive_unpack_pack(int fd[2], const char *me, int quiet, int);
-extern int receive_keep_pack(int fd[2], const char *me, int quiet, int);
-
 /* pager.c */
 extern void setup_pager(void);
 extern int pager_in_use;
fetch-clone.c deleted +0 -87

87 lines not on this page · raw message

fetch-pack.c +103 -7

139 lines not on this page · raw message

Signature · 1 line
1.4.3.3.g87b2-dirty
#1 lore raw

PATCH 2/6 Teach receive-pack how to keep pack files based on object count.

In reply to Nicolas Pitre

From: Shawn Pearce <[email protected]>

Since keeping a pushed pack or exploding it into loose objects should be a local repository decision this teaches receive-pack to decide if it should call unpack-objects or index-pack --stdin --fix-thin based on the setting of receive.unpackLimit and the number of objects contained in the received pack.

If the number of objects (hdr_entries) in the received pack is below the value of receive.unpackLimit (which is 5000 by default) then we unpack-objects as we have in the past.

If the hdr_entries >= receive.unpackLimit then we call index-pack and ask it to include our pid and hostname in the .keep file to make it easier to identify why a given pack has been kept in the repository.

Currently this leaves every received pack as a kept pack. We really don't want that as received packs will tend to be small. Instead we want to delete the .keep file automatically after all refs have been updated. That is being left as room for future improvement.

---
 Documentation/config.txt |   11 +++++-
 cache.h                  |    1 +
 receive-pack.c           |   98 +++++++++++++++++++++++++++++++++++++---------
 sha1_file.c              |    2 +-
 4 files changed, 91 insertions(+), 21 deletions(-)
Documentation/config.txt +10 -1
index d9e73da..9d3c71c 100644
@@ -301,7 +301,16 @@ imap::
 	The configuration variables in the 'imap' section are described
 	in gitlink:git-imap-send[1].
 
-receive.denyNonFastforwads::
+receive.unpackLimit::
+	If the number of objects received in a push is below this
+	limit then the objects will be unpacked into loose object
+	files. However if the number of received objects equals or
+	exceeds this limit then the received pack will be stored as
+	a pack, after adding any missing delta bases.  Storing the
+	pack from a push can make the push operation complete faster,
+	especially on slow filesystems.
+
+receive.denyNonFastForwards::
 	If set to true, git-receive-pack will deny a ref update which is
 	not a fast forward. Use this to prevent such an update via a push,
 	even if that push is forced. This configuration variable is
cache.h +1 -0
index e997a85..6cb7e1d 100644
@@ -376,6 +376,7 @@ extern struct packed_git *parse_pack_ind
 						char *idx_path);
 
 extern void prepare_packed_git(void);
+extern void reprepare_packed_git(void);
 extern void install_packed_git(struct packed_git *pack);
 
 extern struct packed_git *find_sha1_pack(const unsigned char *sha1, 
receive-pack.c +79 -19

125 lines not on this page · raw message

sha1_file.c +1 -1
index 5e6c8b8..7bda2d4 100644
@@ -663,7 +663,7 @@ void prepare_packed_git(void)
 	prepare_packed_git_run_once = 1;
 }
 
-static void reprepare_packed_git(void)
+void reprepare_packed_git(void)
 {
 	prepare_packed_git_run_once = 0;
 	prepare_packed_git();
Signature · 1 line
1.4.3.3.g87b2-dirty
#4 lore raw

PATCH 5/6 have index-pack create .keep file more carefully

In reply to Nicolas Pitre

If by chance we receive a pack which content (list of objects) matches another pack that we already have, and if that pack is marked with a .keep file, then we should not overwrite it.

---
 index-pack.c |   17 ++++++++++-------
 1 files changed, 10 insertions(+), 7 deletions(-)
index-pack.c +10 -7
index a3b55f9..8d64a88 100644
@@ -788,14 +788,17 @@ static void final(const char *final_pack
 				 get_object_directory(), sha1_to_hex(sha1));
 			keep_name = name;
 		}
-		keep_fd = open(keep_name, O_RDWR | O_CREAT, 0600);
-		if (keep_fd < 0)
-			die("cannot write keep file");
-		if (keep_msg_len > 0) {
-			write_or_die(keep_fd, keep_msg, keep_msg_len);
-			write_or_die(keep_fd, "\n", 1);
+		keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
+		if (keep_fd < 0) {
+			if (errno != EEXIST)
+				die("cannot write keep file");
+		} else {
+			if (keep_msg_len > 0) {
+				write_or_die(keep_fd, keep_msg, keep_msg_len);
+				write_or_die(keep_fd, "\n", 1);
+			}
+			close(keep_fd);
 		}
-		close(keep_fd);
 	}
 
 	if (final_pack_name != curr_pack_name) {
Signature · 1 line
1.4.3.3.g87b2-dirty
#9 lore raw

Re: [PATCH 6/6] remove .keep pack lock files when done with refs update

In reply to Nicolas Pitre
Quoted 12 lines · Nicolas Pitre
Quoted 10 lines · Nicolas Pitre

Another area of that patch I'm not really sure of is this:

git-fetch.sh +1 -0
@@ -408,6 +415,7 @@ fetch_main () {
          append_fetch_head "$sha1" "$remote" \
                  "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
       done
+      if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
     ) || exit ;;
   esac
 
Quoted 7 lines
Quoted 2 lines

How can I make sure to not clobber a non-zero return code with the rm that would prevent the exit while still performing the rm in all cases?

OK looking at it again I think the || exit is only meant to match the exit 1 before that point. Since this is a subshell then the exit 1 wouldn't exit it all on its own. Otherwise there is no errors expected to trickle out of the while loop and I _think_ the above is correct.

I think so too.

Quoted 1 line

Do you still want a new patch for the micronit quoting issue?

Not needed, will park them in "pu" and play with them -- hopefully in "next" by the end of the week.

Thanks.

#5 lore raw

PATCH 6/6 remove .keep pack lock files when done with refs update

In reply to Nicolas Pitre

This makes both git-fetch and git-push (fetch-pack and receive-pack) safe against a possible race with aparallel git-repack -a -d that could prune the new pack while it is not yet referenced, and remove the .keep file after refs have been updated.

---
 Documentation/git-index-pack.txt |   11 ++++
 git-fetch.sh                     |   10 ++++-
 index-pack.c                     |   38 +++++++++------
 receive-pack.c                   |   96 +++++++++++++++++++++++++++++---------
 4 files changed, 116 insertions(+), 39 deletions(-)
Documentation/git-index-pack.txt +11 -0
index 1235416..2229ee8 100644
@@ -69,6 +69,17 @@ OPTIONS
 	locate any which have outlived their usefulness.
 
 
+Note
+----
+
+Once the index has been created, the list of object names is sorted
+and the SHA1 hash of that list is printed to stdout. If --stdin was
+also used then this is prefixed by either "pack\t", or "keep\t" if a
+new .keep file was successfully created. This is useful to remove a
+.keep file used as a lock to prevent the race with gitlink:git-repack[1]
+mentioned above.
+
+
 Author
 ------
 Written by Sergey Vlasov <[email protected]>
git-fetch.sh +9 -1
index 2b5538f..e8ce296 100755
@@ -51,7 +51,7 @@ do
 		verbose=Yes
 		;;
 	-k|--k|--ke|--kee|--keep)
-		keep=--keep
+		keep=-k -k
 		;;
 	--reflog-action=*)
 		rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
@@ -368,6 +368,7 @@ fetch_main () {
       ;; # we are already done.
   *)
     ( : subshell because we muck with IFS
+      pack_lockfile=
       IFS=" 	$LF"
       (
 	  git-fetch-pack --thin $exec $keep "$remote" $rref || echo failed "$remote"
@@ -378,6 +379,12 @@ fetch_main () {
 	  failed)
 		  echo >&2 "Fetch failure: $remote"
 		  exit 1 ;;
+	  # special line coming from index-pack with the pack name
+	  pack)
+		  continue ;;
+	  keep)
+		  pack_lockfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
+		  continue ;;
 	  esac
 	  found=
 	  single_force=
@@ -408,6 +415,7 @@ fetch_main () {
 	  append_fetch_head "$sha1" "$remote" \
 		  "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
       done
+      if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
     ) || exit ;;
   esac
 
index-pack.c +23 -15
index 8d64a88..042aea8 100644
@@ -757,6 +757,7 @@ static void final(const char *final_pack
 		  const char *keep_name, const char *keep_msg,
 		  unsigned char *sha1)
 {
+	char *report = "pack";
 	char name[PATH_MAX];
 	int err;
 
@@ -767,18 +768,6 @@ static void final(const char *final_pack
 		if (err)
 			die("error while closing pack file: %s", strerror(errno));
 		chmod(curr_pack_name, 0444);
-
-		/*
-		 * Let's just mimic git-unpack-objects here and write
-		 * the last part of the buffer to stdout.
-		 */
-		while (input_len) {
-			err = xwrite(1, input_buffer + input_offset, input_len);
-			if (err <= 0)
-				break;
-			input_len -= err;
-			input_offset += err;
-		}
 	}
 
 	if (keep_msg) {
@@ -798,6 +787,7 @@ static void final(const char *final_pack
 				write_or_die(keep_fd, "\n", 1);
 			}
 			close(keep_fd);
+			report = "keep";
 		}
 	}
 
@@ -821,6 +811,27 @@ static void final(const char *final_pack
 		if (move_temp_to_file(curr_index_name, final_index_name))
 			die("cannot store index file");
 	}
+
+	if (!from_stdin) {
+		printf("%s\n", sha1_to_hex(sha1));
+	} else {
+		char buf[48];
+		int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
+				   report, sha1_to_hex(sha1));
+		xwrite(1, buf, len);
+
+		/*
+		 * Let's just mimic git-unpack-objects here and write
+		 * the last part of the input buffer to stdout.
+		 */
+		while (input_len) {
+			err = xwrite(1, input_buffer + input_offset, input_len);
+			if (err <= 0)
+				break;
+			input_len -= err;
+			input_offset += err;
+		}
+	}
 }
 
 int main(int argc, char **argv)
@@ -937,8 +948,5 @@ int main(int argc, char **argv)
 	free(index_name_buf);
 	free(keep_name_buf);
 
-	if (!from_stdin)
-		printf("%s\n", sha1_to_hex(sha1));
-
 	return 0;
 }
receive-pack.c +73 -23

138 lines not on this page · raw message

Signature · 1 line
1.4.3.3.g87b2-dirty
#0 lore raw

PATCH 1/6 Allow pack header preprocessing before unpack-objects/index-pack.

Some applications which invoke unpack-objects or index-pack --stdin may want to examine the pack header to determine the number of objects contained in the pack and use that value to determine which executable to invoke to handle the rest of the pack stream.

However if the caller consumes the pack header from the input stream then its no longer available for unpack-objects or index-pack --stdin, both of which need the version and object count to process the stream.

This change introduces --pack_header=ver,cnt as a command line option that the caller can supply to indicate it has already consumed the pack header and what version and object count were found in that header. As this option is only meant for low level applications such as receive-pack we are not documenting it at this time.

---

Patch description text shamelessly stolen from a similar patch from Shawn Pearce <[email protected]>.

 builtin-unpack-objects.c |   15 +++++++++++++++
 index-pack.c             |   13 +++++++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)
builtin-unpack-objects.c +15 -0
index 74a90c1..e6d7574 100644
@@ -371,6 +371,21 @@ int cmd_unpack_objects(int argc, const c
 				recover = 1;
 				continue;
 			}
+			if (!strncmp(arg, "--pack_header=", 14)) {
+				struct pack_header *hdr;
+				char *c;
+
+				hdr = (struct pack_header *)buffer;
+				hdr->hdr_signature = htonl(PACK_SIGNATURE);
+				hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
+				if (*c != ',')
+					die("bad %s", arg);
+				hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
+				if (*c)
+					die("bad %s", arg);
+				len = sizeof(*hdr);
+				continue;
+			}
 			usage(unpack_usage);
 		}
 
index-pack.c +13 -0
index b37dd78..a3b55f9 100644
@@ -841,6 +841,19 @@ int main(int argc, char **argv)
 				keep_msg = "";
 			} else if (!strncmp(arg, "--keep=", 7)) {
 				keep_msg = arg + 7;
+			} else if (!strncmp(arg, "--pack_header=", 14)) {
+				struct pack_header *hdr;
+				char *c;
+
+				hdr = (struct pack_header *)input_buffer;
+				hdr->hdr_signature = htonl(PACK_SIGNATURE);
+				hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
+				if (*c != ',')
+					die("bad %s", arg);
+				hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
+				if (*c)
+					die("bad %s", arg);
+				input_len = sizeof(*hdr);
 			} else if (!strcmp(arg, "-v")) {
 				verbose = 1;
 			} else if (!strcmp(arg, "-o")) {
Signature · 1 line
1.4.3.3.g87b2-dirty
#2 lore raw

PATCH 3/6 git-fetch can use both --thin and --keep with fetch-pack now

In reply to Nicolas Pitre
---
 git-fetch.sh |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
git-fetch.sh +2 -2
index 539dff6..2b5538f 100755
@@ -20,7 +20,7 @@ verbose=
 update_head_ok=
 exec=
 upload_pack=
-keep=--thin
+keep=
 while case "$#" in 0) break ;; esac
 do
 	case "$1" in
@@ -370,7 +370,7 @@ fetch_main () {
     ( : subshell because we muck with IFS
       IFS=" 	$LF"
       (
-	  git-fetch-pack $exec $keep "$remote" $rref || echo failed "$remote"
+	  git-fetch-pack --thin $exec $keep "$remote" $rref || echo failed "$remote"
       ) |
       while read sha1 remote_name
       do
Signature · 1 line
1.4.3.3.g87b2-dirty