Skip to contentSkip to conversation

C implementation of the 'git' program.

PATCH In review Andreas Ericsson 6 participants 9 messages Jun 15, 2016

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

PATCH 1/1 C implementation of the 'git' program.

Subject: [PATCH] C implementation of the 'git' program.

This patch provides a C implementation of the 'git' program and introduces support for putting the git-* commands in their own directory. It also saves some time on executing git-* commands in a tight loop and it prints the currently available git commands in a nicely formatted list.

The location of the GIT_LIB can be obtained by running

	git --lib

which will hopefully give porcelainistas ample time to adapt their heavy-duty loops to use the programs in the library directly if they wish, although that's strictly speaking not really necessary any more.

Some timing values for 10000 invocations of git-var >&/dev/null; git.sh: 24.194s
git.c: 9.044s
git-var: 7.377s

The git-<tab><tab> behaviour can still be obtained by adding

	PATH=$PATH:$(git --lib)

to one's .profile.

Experimental libraries can be used by either setting the environment variable GIT_LIB, or by using

	git --lib=/some/experimental/lib

As a nice side-effect, the patch also fixes the problem with
make
make prefix=/usr install

---

 Documentation/git.txt |    6 +
 Makefile              |   24 ++---
 git.c                 |  224 +++++++++++++++++++++++++++++++++++++++++++++++++
 git.sh                |   76 -----------------
 4 files changed, 239 insertions(+), 91 deletions(-)
 create mode 100644 git.c
 delete mode 100755 git.sh

applies-to: 6466c53ae80cddbb581c5fdb2332f9321fade867 b2b97ed9901915dce573516e3a6f10a60bc0c04a

Documentation/git.txt +4 -2
index 2f9622f..848d09a 100644
@@ -8,7 +8,7 @@ git - the stupid content tracker
 
 SYNOPSIS
 --------
-'git-<command>' <args>
+'git' [--version] [--lib[=GIT_LIB]] [--help] <command> <args>
 
 DESCRIPTION
 -----------
@@ -535,7 +535,9 @@ include::../README[]
 
 Author
 ------
-Written by Linus Torvalds <[email protected]> and the git-list <[email protected]>.
+git's founding father is Linus Torvalds <[email protected]>.
+The current git nurse is Junio C. Hamano <[email protected]>.
+Numerous contributions stem from the git-list <[email protected]>.
 
 Documentation
 --------------
Makefile +11 -13
index b202be1..80d865b 100644
@@ -59,6 +59,8 @@ ALL_CFLAGS = $(CFLAGS)
 
 prefix = $(HOME)
 bindir = $(prefix)/bin
+# XXX set libdir to bindir for now to not break the porcelain
+libdir = $(bindir)
 template_dir = $(prefix)/share/git-core/templates/
 GIT_PYTHON_DIR = $(prefix)/share/git-core/python
 # DESTDIR=
@@ -86,7 +88,7 @@ SCRIPT_SH = \
 	git-prune.sh git-pull.sh git-push.sh git-rebase.sh \
 	git-repack.sh git-request-pull.sh git-reset.sh \
 	git-resolve.sh git-revert.sh git-sh-setup.sh git-status.sh \
-	git-tag.sh git-verify-tag.sh git-whatchanged.sh git.sh \
+	git-tag.sh git-verify-tag.sh git-whatchanged.sh \
 	git-applymbox.sh git-applypatch.sh git-am.sh \
 	git-merge.sh git-merge-stupid.sh git-merge-octopus.sh \
 	git-merge-resolve.sh git-merge-ours.sh git-grep.sh
@@ -323,19 +325,13 @@ SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)
 export prefix TAR INSTALL DESTDIR SHELL_PATH template_dir
 ### Build rules
 
-all: $(PROGRAMS) $(SCRIPTS)
+all: $(PROGRAMS) $(SCRIPTS) git
 
-all:
-	$(MAKE) -C templates
+git: git.c
+	$(CC) $(CFLAGS) $< -o $@ -DDEFAULT_GIT_LIB='"$(libdir)"' -DGIT_VERSION='"$(GIT_VERSION)"'
 
-git: git.sh Makefile
-	rm -f $@+ $@
-	sed -e '1s|#!.*/sh|#!$(call shq,$(SHELL_PATH))|' \
-	    -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
-	    -e 's/@@X@@/$(X)/g' \
-	    $(GIT_LIST_TWEAK) <[email protected] >$@+
-	chmod +x $@+
-	mv $@+ $@
+# Force rebuild so that "make prefix=<something> install" Just Works.
+.PHONY: git
 
 $(filter-out git,$(patsubst %.sh,%,$(SCRIPT_SH))) : % : %.sh
 	rm -f $@
@@ -420,8 +416,10 @@ check:
 ### Installation rules
 
 install: $(PROGRAMS) $(SCRIPTS)
+	$(INSTALL) -d -m755 $(call shellquote,$(DESTDIR)$(libdir))
+	$(INSTALL) $(PROGRAMS) $(SCRIPTS) $(call shellquote,$(DESTDIR)$(libdir))
 	$(INSTALL) -d -m755 $(call shellquote,$(DESTDIR)$(bindir))
-	$(INSTALL) $(PROGRAMS) $(SCRIPTS) $(call shellquote,$(DESTDIR)$(bindir))
+	$(INSTALL) git $(call shellquote,$(DESTDIR)$(bindir))
 	$(MAKE) -C templates install
 	$(INSTALL) -d -m755 $(call shellquote,$(DESTDIR)$(GIT_PYTHON_DIR))
 	$(INSTALL) $(PYMODULES) $(call shellquote,$(DESTDIR)$(GIT_PYTHON_DIR))
git.c new file +224 -0

224 lines not on this page · raw message

git.sh deleted +0 -76

76 lines not on this page · raw message

---
0.99.9.GIT

#1 lore raw
Quoted 5 lines · Andreas Ericsson

This patch provides a C implementation of the 'git' program and introduces support for putting the git-* commands in their own directory. It also saves some time on executing git-* commands in a tight loop and it prints the currently available git commands in a nicely formatted list.

I think this makes sense - "git" may be a very simple program even in shell, but hey, it's simple even in C, and while performance may be a secondary issue, when it is in C we will have the option to extend in more effectively later.

For example, we might implement some things directly in the "git" wrapper.

And the performance difference does seem to be quite noticeable too..

Quoted 3 lines

The location of the GIT_LIB can be obtained by running

	git --lib

I think this might be a bit ambiguous. When I see "GIT_LIB", to me it implies traditional libraries (ie a "libgit.a" kind of thing), not the kind of "git executable plugin" directory.

So I'd suggest renaming "--lib" and "GIT_LIB" to be more of a "--libexec" kind of flavor, if only to avoid that confusion. Even if the actual directory ends up being /usr/lib/git-<version>/ as in my original suggestion, I think it's good to make it clear that it's not just some kind of "link type" library, but more of a combination of libraries and plugins and executables.

But maybe that's just me?

Linus

#2 lore raw
Quoted 3 lines · Linus Torvalds

And the performance difference does seem to be quite noticeable too..

Yes. I was quite astonished when I noticed first, thinking the shell kept the parsed script in cache or some such. Apparently it doesn't.

Quoted 9 lines
Quoted 4 lines

The location of the GIT_LIB can be obtained by running

	git --lib

I think this might be a bit ambiguous. When I see "GIT_LIB", to me it implies traditional libraries (ie a "libgit.a" kind of thing), not the kind of "git executable plugin" directory.

So I'd suggest renaming "--lib" and "GIT_LIB" to be more of a "--libexec" kind of flavor, if only to avoid that confusion.

Someone said libexec was moving out (of Linux, at least), so I thought I'd better avoid that. Perhaps GIT_LIBDIR?

Btw, I re-visited ye ole "git binary directory?" thread and noticed the nifty trick you did with PATH from the wrapper. I'll add that to git.c and, unless I get a better suggestion, rename GIT_LIB to GIT_LIBDIR (and git --libdir).

Quoted 4 lines

Even if the actual directory ends up being /usr/lib/git-<version>/ as in my original suggestion, I think it's good to make it clear that it's not just some kind of "link type" library, but more of a combination of libraries and plugins and executables.

But maybe that's just me?

It's not just you, but GIT_DIR was taken and I felt lazy.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#3 lore raw

Dear diary, on Thu, Nov 10, 2005 at 11:50:16PM CET, I got a letter where Andreas Ericsson <[email protected]> said that...

Quoted 7 lines
Quoted 3 lines · Linus Torvalds

And the performance difference does seem to be quite noticeable too..

Yes. I was quite astonished when I noticed first, thinking the shell kept the parsed script in cache or some such. Apparently it doesn't.

The bulk of the time is likely not spent parsing, but executing subprocesses, that's hideously expensive. Just for fun, you can try to measure the improvement when you remove the $(dirname "$0"). Here, for 1000 tries it's roughly 9s normal, 7s without dirname and 3s direct invocation, which seems to give you _very roughly_ 2s per subprocess, and 2s other shell overhead, which is unusually right.

Actually, I can bring the git.sh runtime from 7s to 4.7s:

-       case "$cmd" in
-       -v|--v|--ve|--ver|--vers|--versi|--versio|--version)
+       if [[ "$cmd" = "-v" ||
+             "$cmd" = "--v" ||
+             "$cmd" = "--ve" ||
+             "$cmd" = "--ver" ||
+             "$cmd" = "--vers" ||
+             "$cmd" = "--versi" ||
+             "$cmd" = "--versio" ||
+             "$cmd" = "--version" ]]; then

echo "git version @@GIT_VERSION@@" exit 0 ;;
- esac
+ fi
(whitespace-mangled)

Well, subconsciously I never really trusted this case thing. ;-)

This leaves ~ 1.7s to other shell overhead and execve() (the main command call is without the fork(), while $(dirname) fork()s).

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

The location of the GIT_LIB can be obtained by running

	git --lib

I think this might be a bit ambiguous. When I see "GIT_LIB", to me it implies traditional libraries (ie a "libgit.a" kind of thing), not the kind of "git executable plugin" directory.

Quoted 6 lines
Quoted 2 lines

So I'd suggest renaming "--lib" and "GIT_LIB" to be more of a "--libexec" kind of flavor, if only to avoid that confusion.

Someone said libexec was moving out (of Linux, at least), so I thought I'd better avoid that. Perhaps GIT_LIBDIR?

This may not necessarily have anything in common with the actual directory name. I prefer libexec too (but personally don't care too much).

Signature · 4 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
VI has two modes: the one in which it beeps and the one in which
it doesn't.
#4 lore raw
In reply to Andreas Ericsson
Quoted 10 lines · Andreas Ericsson
Quoted 5 lines · Linus Torvalds

I think this might be a bit ambiguous. When I see "GIT_LIB", to me it implies traditional libraries (ie a "libgit.a" kind of thing), not the kind of "git executable plugin" directory.
So I'd suggest renaming "--lib" and "GIT_LIB" to be more of a "--libexec" kind of flavor, if only to avoid that confusion.

Someone said libexec was moving out (of Linux, at least), so I thought I'd better avoid that. Perhaps GIT_LIBDIR?

More like "the rest of git executables". GIT_EXEC_PATH, with "git --exec-path" command, perhaps?

#5 lore raw
In reply to Andreas Ericsson

Hi,

[email protected] (Andreas Ericsson) writes:

[snip]

Quoted 5 lines

As a nice side-effect, the patch also fixes the problem with
make
make prefix=/usr install

[snip]

Quoted 2 lines

+# Force rebuild so that "make prefix=<something> install" Just Works. +.PHONY: git

This is bad, since it does the wrong thing for the correct way:

make prefix=/usr
sudo make prefix=/usr install

With your patch 'git' will be compiled as root.

- Hari

#6 lore raw
Quoted 6 lines · Raja R Harinath

This is bad, since it does the wrong thing for the correct way:

make prefix=/usr
sudo make prefix=/usr install

With your patch 'git' will be compiled as root.

You are correct.

We should allow building as user and installing as root, with the installation step _not_ touching the build directory at all. A good litmus test is that you should be able to build and install from a root-squashed NFS mounted working tree, without giving write permissions to NFS-nobody user (obviously you need to give read and search permissions).

Andreas?

#7 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 5 lines · Raja R Harinath

This is bad, since it does the wrong thing for the correct way:

make prefix=/usr
sudo make prefix=/usr install

Quoted 7 lines
Quoted 1 line

With your patch 'git' will be compiled as root.

You are correct.

We should allow building as user and installing as root, with the installation step _not_ touching the build directory at all.

Sane point. This doesn't work without a ./configure script or some such though (something external to the Makefile that updates a config-file of some sort), as

make
(make finishes, user remembers he/she must use prefix= to install somewhere) make prefix=/usr

wouldn't rebuild git so binaries produced would still try to use $HOME/bin for libdir. Granted, it's easy enough to do 'touch git.c' before rebuilding, but it's not exactly intuitive.

I'll hack up a configure-script if that's alright. It should be fairly short and just define the GIT_VERSION and GIT_LIBDIR macros.

I imagine this can be done through the config.mak file, included from 221 in Makefile, supposing it's forcibly included rather than just if it exists, and we'll have the 'git:' target depend on it. I think this is the least intrusive change. OTOH, it might not be bad to move some of the uname_? logic to a shell-script (and have all targets depend on config.mak).

Thoughts? I've got a feeling I've overlooked something here.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#8 lore raw
Quoted 1 line · Andreas Ericsson

Thoughts? I've got a feeling I've overlooked something here.

If you are doing it, it might make sense to include it from Makefiles in subdirectories, so that on a platform with non GNU tar, you could say:

	$ cd t
	$ make

and get TAR=gtar definition from ../config.mk

I think Smurf's libize repository has something like that; it is found at:

        $ cat .git/remotes/smurf
        URL: http://netz.smurf.noris.de/git/git.git
        Pull: libize:libize