Skip to contentSkip to conversation

C version of git-count-objects

PATCH In review Peter Hagervall 5 participants 12 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 C version of git-count-objects

Answering the call Linus made[1], sort of, but for a completely different program.

Anyway, it ought to be at least as portable as the shell script, and a whole lot faster, however much that matters.

[1] http://article.gmane.org/gmane.comp.version-control.git/19073

---

 Makefile             |    5 +--
 count-objects.c      |   56 +++++++++++++++++++++++++++++++++++++
 git-count-objects.sh |   31 --------------------
 3 files changed, 59 insertions(+), 33 deletions(-)
Makefile +3 -2
index 8ce27a6..53e7591 100644
@@ -115,7 +115,7 @@ ### --- END CONFIGURATION SECTION ---
 SCRIPT_SH = \
 	git-add.sh git-bisect.sh git-branch.sh git-checkout.sh \
 	git-cherry.sh git-clean.sh git-clone.sh git-commit.sh \
-	git-count-objects.sh git-diff.sh git-fetch.sh \
+	git-diff.sh git-fetch.sh \
 	git-format-patch.sh git-ls-remote.sh \
 	git-merge-one-file.sh git-parse-remote.sh \
 	git-prune.sh git-pull.sh git-push.sh git-rebase.sh \
@@ -165,7 +165,8 @@ PROGRAMS = \
 	git-upload-pack$X git-verify-pack$X git-write-tree$X \
 	git-update-ref$X git-symbolic-ref$X git-check-ref-format$X \
 	git-name-rev$X git-pack-redundant$X git-repo-config$X git-var$X \
-	git-describe$X git-merge-tree$X git-blame$X git-imap-send$X
+	git-describe$X git-merge-tree$X git-blame$X git-imap-send$X \
+	git-count-objects$X
 
 BUILT_INS = git-log$X
 
count-objects.c new file +56 -0

56 lines not on this page · raw message

git-count-objects.sh deleted +0 -31
deleted file mode 100755
index 40c58ef..0000000
@@ -1,31 +0,0 @@
-#!/bin/sh
-#
-# Copyright (c) 2005 Junio C Hamano
-#
-
-GIT_DIR=`git-rev-parse --git-dir` || exit $?
-
-dc </dev/null 2>/dev/null || {
-	# This is not a real DC at all -- it just knows how
-	# this script feeds DC and does the computation itself.
-	dc () {
-		while read a b
-		do
-			case $a,$b in
-			0,)	acc=0 ;;
-			*,+)	acc=$(($acc + $a)) ;;
-			p,)	echo "$acc" ;;
-			esac
-		done
-	}
-}
-
-echo $(find "$GIT_DIR/objects"/?? -type f -print 2>/dev/null | wc -l) objects, \
-$({
-    echo 0
-    # "no-such" is to help Darwin folks by not using xargs -r.
-    find "$GIT_DIR/objects"/?? -type f -print 2>/dev/null |
-    xargs du -k "$GIT_DIR/objects/no-such" 2>/dev/null |
-    sed -e 's/[ 	].*/ +/'
-    echo p
-} | dc) kilobytes
#1 lore raw

count-objects.c line 22

Quoted 3 lines
count-objects.c new file +3 -0
+                       if (access(subdir, R_OK | X_OK))
+                               continue;
+                       chdir(subdir);

You've got yourself a needless race condition right there. Just do the chdir and check the return value. (And besides, access checks with the wrong set of permissions, should this ever end up in set[ug]id context.)

Morten

#2 lore raw
In reply to Peter Hagervall
Quoted 6 lines · Peter Hagervall

Answering the call Linus made[1], sort of, but for a completely different program.

Anyway, it ought to be at least as portable as the shell script, and a whole lot faster, however much that matters.

[...]

count-objects.c line 26

Quoted 11 lines
count-objects.c new file +11 -0
+	for (i = 0; i < 16; i++) {
+		subdir[0] = hex_digits[i];
+		for (j = 0; j < 16; j++) {
+			subdir[1] = hex_digits[j];
+			if (access(subdir, R_OK | X_OK))
+				continue;
+			chdir(subdir);
+			if (!(dp = opendir("."))) {
+				error("can't open subdir %s", subdir);
+				continue;
+			}

Looks like you're missing a chdir(".."); there.

Nicolas

#3 lore raw

PATCH 1/1 C version of git-count-objects, second try

Quoted 9 lines · Nicolas Pitre
Quoted 6 lines · Peter Hagervall

Answering the call Linus made[1], sort of, but for a completely different program.

Anyway, it ought to be at least as portable as the shell script, and a whole lot faster, however much that matters.

[...]

Quoted 14 lines

Review

Quoted 11 lines
(quoted diff) +11 -0
+	for (i = 0; i < 16; i++) {
+		subdir[0] = hex_digits[i];
+		for (j = 0; j < 16; j++) {
+			subdir[1] = hex_digits[j];
+			if (access(subdir, R_OK | X_OK))
+				continue;
+			chdir(subdir);
+			if (!(dp = opendir("."))) {
+				error("can't open subdir %s", subdir);
+				continue;
+			}

Looks like you're missing a chdir(".."); there.

Thanks, I overlooked that one (and the race condition pointed out by Morten). Anyway, fixed those now, and removed an unused array.

---

 Makefile             |    5 ++-
 count-objects.c      |   55 +++++++++++++++++++++++++++++++++++++
 git-count-objects.sh |   31 --------------------
 3 files changed, 58 insertions(+), 33 deletions(-)
Makefile +3 -2
index 8ce27a6..53e7591 100644
@@ -115,7 +115,7 @@ ### --- END CONFIGURATION SECTION ---
 SCRIPT_SH = \
 	git-add.sh git-bisect.sh git-branch.sh git-checkout.sh \
 	git-cherry.sh git-clean.sh git-clone.sh git-commit.sh \
-	git-count-objects.sh git-diff.sh git-fetch.sh \
+	git-diff.sh git-fetch.sh \
 	git-format-patch.sh git-ls-remote.sh \
 	git-merge-one-file.sh git-parse-remote.sh \
 	git-prune.sh git-pull.sh git-push.sh git-rebase.sh \
@@ -165,7 +165,8 @@ PROGRAMS = \
 	git-upload-pack$X git-verify-pack$X git-write-tree$X \
 	git-update-ref$X git-symbolic-ref$X git-check-ref-format$X \
 	git-name-rev$X git-pack-redundant$X git-repo-config$X git-var$X \
-	git-describe$X git-merge-tree$X git-blame$X git-imap-send$X
+	git-describe$X git-merge-tree$X git-blame$X git-imap-send$X \
+	git-count-objects$X
 
 BUILT_INS = git-log$X
 
count-objects.c new file +55 -0

55 lines not on this page · raw message

git-count-objects.sh deleted +0 -31
deleted file mode 100755
index 40c58ef..0000000
@@ -1,31 +0,0 @@
-#!/bin/sh
-#
-# Copyright (c) 2005 Junio C Hamano
-#
-
-GIT_DIR=`git-rev-parse --git-dir` || exit $?
-
-dc </dev/null 2>/dev/null || {
-	# This is not a real DC at all -- it just knows how
-	# this script feeds DC and does the computation itself.
-	dc () {
-		while read a b
-		do
-			case $a,$b in
-			0,)	acc=0 ;;
-			*,+)	acc=$(($acc + $a)) ;;
-			p,)	echo "$acc" ;;
-			esac
-		done
-	}
-}
-
-echo $(find "$GIT_DIR/objects"/?? -type f -print 2>/dev/null | wc -l) objects, \
-$({
-    echo 0
-    # "no-such" is to help Darwin folks by not using xargs -r.
-    find "$GIT_DIR/objects"/?? -type f -print 2>/dev/null |
-    xargs du -k "$GIT_DIR/objects/no-such" 2>/dev/null |
-    sed -e 's/[ 	].*/ +/'
-    echo p
-} | dc) kilobytes
#4 lore raw
In reply to Nicolas Pitre
Quoted 9 lines · Nicolas Pitre
Quoted 6 lines · Peter Hagervall

Answering the call Linus made[1], sort of, but for a completely different program.

Anyway, it ought to be at least as portable as the shell script, and a whole lot faster, however much that matters.

[...]

Quoted 13 lines

count-objects.c line 26

Quoted 11 lines
count-objects.c new file +11 -0
+	for (i = 0; i < 16; i++) {
+		subdir[0] = hex_digits[i];
+		for (j = 0; j < 16; j++) {
+			subdir[1] = hex_digits[j];
+			if (access(subdir, R_OK | X_OK))
+				continue;
+			chdir(subdir);
+			if (!(dp = opendir("."))) {
+				error("can't open subdir %s", subdir);
+				continue;
+			}

Looks like you're missing a chdir(".."); there.

Why would you even _need_ to chdir() anywhere, anyway?

#5 lore raw
Quoted 25 lines · Junio C Hamano
Quoted 9 lines · Nicolas Pitre
Quoted 6 lines · Peter Hagervall

Answering the call Linus made[1], sort of, but for a completely different program.

Anyway, it ought to be at least as portable as the shell script, and a whole lot faster, however much that matters.

[...]

Quoted 13 lines

count-objects.c line 26

Quoted 11 lines
count-objects.c new file +11 -0
+	for (i = 0; i < 16; i++) {
+		subdir[0] = hex_digits[i];
+		for (j = 0; j < 16; j++) {
+			subdir[1] = hex_digits[j];
+			if (access(subdir, R_OK | X_OK))
+				continue;
+			chdir(subdir);
+			if (!(dp = opendir("."))) {
+				error("can't open subdir %s", subdir);
+				continue;
+			}

Looks like you're missing a chdir(".."); there.

Quoted 1 line

Why would you even _need_ to chdir() anywhere, anyway?

To avoid appending the filename to the path before each lstat() I'd guess.

Nicolas

#6 lore raw
Quoted 27 lines · Nicolas Pitre
Quoted 25 lines · Junio C Hamano
Quoted 22 lines · Nicolas Pitre
Quoted 6 lines · Peter Hagervall

Answering the call Linus made[1], sort of, but for a completely different program.

Anyway, it ought to be at least as portable as the shell script, and a whole lot faster, however much that matters.

[...]

count-objects.c line 26

Quoted 11 lines
count-objects.c new file +11 -0
+	for (i = 0; i < 16; i++) {
+		subdir[0] = hex_digits[i];
+		for (j = 0; j < 16; j++) {
+			subdir[1] = hex_digits[j];
+			if (access(subdir, R_OK | X_OK))
+				continue;
+			chdir(subdir);
+			if (!(dp = opendir("."))) {
+				error("can't open subdir %s", subdir);
+				continue;
+			}

Looks like you're missing a chdir(".."); there.

Quoted 4 lines
Quoted 1 line

Why would you even _need_ to chdir() anywhere, anyway?

To avoid appending the filename to the path before each lstat() I'd guess.

Yes, that's pretty much the reason.

Peter

#7 lore raw
Quoted 5 lines · Peter Hagervall
Quoted 3 lines

To avoid appending the filename to the path before each lstat() I'd guess.

Yes, that's pretty much the reason.

It's a bad reason, though.

For one thing, it just doesn't work. You'll have to chdir() back, and you can't use ".." in case the user has set up some symlink thing. So you end up doing other really strange things.

You can do this much more efficiently with something like this:

	const char *obj = git_object_directory();
	int len = strlen(obj);
	char *dir = malloc(len + 300);

	memcpy(dir, obj, len);
	if (len && obj[len-1] != '/')
		dir[len++] = '/';
	dir[len+2] = 0;
	for (i = 0; i < 16; i++) {
		dir[len] = hexdigit[i];
		for (j = 0; j < 16; j+) {
			dir[len+1] = hexdigit[j];
			dir[len+2] = 0;
			DIR *d = opendir(dir);
			if (!d)
				continue;
			nr += count(d, dir, len+2);
			closedir(d);
		}
	}

where the "count()" function just ends up doing something like

	int count(DIR *d, const char *prefix, int len)
	{
		int nr = 0;
		struct dirent *de;

		prefix[len++] = '/';
		while ((de = readdir(d)) != NULL) {
			int fd;
			if (de->d_name[0] == '.')
				continue;
			strcpy(prefix + len, de->d_name);
			fd = open(prefix, O_RDONLY);
			.. check if it's ok, perhaps.. ?
			if (ok)
				nr++;
			close(fd);
		}
		return nr;
	}

and you're done. Efficient, and it's easy to add the endign to the pathname, because you're passing in a buffer that is big enough, and you're telling people where they should put their suffixes..

And no, the above has never been compiled or tested, and I wrote it with one eye closed, while drinking heavily and experimenting with some funky 'shrooms. So caveat emptor.

Linus

#8 lore raw
Quoted 19 lines · Linus Torvalds
Quoted 5 lines · Peter Hagervall
Quoted 3 lines

To avoid appending the filename to the path before each lstat() I'd guess.

Yes, that's pretty much the reason.

It's a bad reason, though.

For one thing, it just doesn't work. You'll have to chdir() back, and you can't use ".." in case the user has set up some symlink thing. So you end up doing other really strange things.

You can do this much more efficiently with something like this:

	const char *obj = git_object_directory();
	int len = strlen(obj);

<snip>

Quoted 11 lines
				continue;
			strcpy(prefix + len, de->d_name);
			fd = open(prefix, O_RDONLY);
			.. check if it's ok, perhaps.. ?
			if (ok)
				nr++;
			close(fd);
		}
		return nr;
	}
Quoted 3 lines

and you're done. Efficient, and it's easy to add the endign to the pathname, because you're passing in a buffer that is big enough, and you're telling people where they should put their suffixes..

Thanks, I'll make a third stab at it tomorrow, if anyone is interested that is?

Peter

#9 lore raw
Quoted 2 lines · Peter Hagervall

Thanks, I'll make a third stab at it tomorrow, if anyone is interested that is?

How about something like this instead?

-- >8 --

 Makefile        |    2 -
 builtin-count.c |  124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 builtin.h       |    1 
 git.c           |    1 
 4 files changed, 127 insertions(+), 1 deletions(-)
Makefile +1 -1
index 8ce27a6..14193aa 100644
@@ -214,7 +214,7 @@ LIB_OBJS = \
 	$(DIFF_OBJS)
 
 BUILTIN_OBJS = \
-	builtin-log.o builtin-help.o
+	builtin-log.o builtin-help.o builtin-count.o
 
 GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
 LIBS = $(GITLIBS) -lz
builtin-count.c new file +124 -0

124 lines not on this page · raw message

builtin.h +1 -0
index 47408a0..76169e3 100644
@@ -19,5 +19,6 @@ extern int cmd_version(int argc, const c
 extern int cmd_whatchanged(int argc, const char **argv, char **envp);
 extern int cmd_show(int argc, const char **argv, char **envp);
 extern int cmd_log(int argc, const char **argv, char **envp);
+extern int cmd_count_objects(int argc, const char **argv, char **envp);
 
 #endif
git.c +1 -0
index 01b7e28..00fb399 100644
@@ -46,6 +46,7 @@ static void handle_internal_command(int 
 		{ "log", cmd_log },
 		{ "whatchanged", cmd_whatchanged },
 		{ "show", cmd_show },
+		{ "count-objects", cmd_count_objects },
 	};
 	int i;
 
#10 lore raw

On Thu, Apr 27, 2006 at 03:07:37PM -0700, Junio C Hamano wrote:

...

Quoted 1 line

+int cmd_count_objects(int ac, const char **av, char *ep)

                                                       ^

...

Quoted 1 line

+extern int cmd_count_objects(int argc, const char **argv, char **envp);

                                                                  ^^

Looks like we have a type mismatch here, no?

Peter

#11 lore raw
Quoted 11 lines · Peter Hagervall

On Thu, Apr 27, 2006 at 03:07:37PM -0700, Junio C Hamano wrote:

...

Quoted 1 line

+int cmd_count_objects(int ac, const char **av, char *ep)

                                                       ^

...

Quoted 1 line

+extern int cmd_count_objects(int argc, const char **argv, char **envp);

                                                                  ^^

Looks like we have a type mismatch here, no?

Interesting. Lack of #include <builtin.h> was causing the compiler not to notice X-<.