git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Question about .git/objects/info/alternates

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Mar 24, 2010, 19:23 UTC
Message-ID
<20100324192315.GA19322@progeny.tock>
In-Reply-To
<a038bef51003241153g33445607qb3ab750e08b0584@mail.gmail.com>
Chris Packham wrote:
Show 5 quoted lines
> It occurs to me that the UI around alternates is a bit lacking i.e.
> there isn't a git command to display the alternates in use or to add
> them to an existing repository (or at least I couldn't find one
> skimming the docs or googling). So here's my attempt to add a 'git
> alternates' command which can display, add or remove an alternate.
Quite welcome!

Something like this explanation probably belongs in the commit message. That way, years down the line, people don’t need to trawl the list archives to see what your goal was.

Comments:
> From a5c64de20937da132376d717f19a1d52b54701d2 Mon Sep 17 00:00:00 2001
> From: Chris Packham <judge.packham@gmail.com>
> Date: Wed, 24 Mar 2010 11:34:11 -0700

Redundant next to the email header. It is useful to be able to include fields that differ from the e-mail header (often Subject:, sometimes Date:, sometimes From:), but aside from that, this metadata should be omitted when sending patches to the git mailing list.

Show 9 quoted lines
> +#
> +# Runs through the alternates file calling the callback function $1
> +# with the name of the alternate as the first argument to the callback
> +# any additional arguments are passed to the callback function.
> +#
> +walk_alternates()
> +{
> +    local alternates=$GIT_DIR/objects/info/alternates
> +    local callback=$1

I couldn’t find any other uses of “local” in-tree. I assume old shells don’t support it.

Will this be a problem for recursion? Maybe the callback should be called in a subshell.

Show 6 quoted lines
> +    shift
> +
> +    if [ -e $alternates ]; then
> +        while read line
> +        do
> +            $callback $line $*
Probably "$line" "$@" instead of $line $* would be more flexible.
Show 11 quoted lines
> +#
> +# Walk function to display one alternate object store and, if the user
> +# has specified -r, recursively call show_alternates on the git
> +# repository that the object store belongs to.
> +#
> +show_alternates_walk()
> +{
> +    say "Object store $1"
> +    say "    referenced via $GIT_DIR"
> +
> +    local new_git_dir=${line%%/objects}
use of local.
> +    if [ "$recursive" == "true" -a "$GIT_DIR" != "$new_git_dir" ]

Should use = instead of == (portability). Also git scripts tend to spell out 'test' and avoid the -a and -o operators:

	if test "$recursive" = true && test "$GIT_DIR" != "$new_git-dir"
though that is not a hard and fast rule.
> +add_alternate()
> +{
[...]
> +    touch $GIT_DIR/objects/info/alternates
Necessary?
> +    echo "$(readlink -f $dir)" >> $GIT_DIR/objects/info/alternates
Maybe
	readlink -f "$dir" >> $GIT_DIR/objects/info/alternates
would be simpler.
Show 6 quoted lines
> +rewrite_alternates()
> +{
> +    if test "$1" != "$2"; then
> +        echo $2 >> $3
> +    fi
> +}
What does this function do?  (Could use a comment.)
> +del_alternate()
> +{
[...]
> +    local alternates=$GIT_DIR/objects/info/alternates
use of local.
> +
> +    new_alts_file=$(mktemp $alternates-XXXXXX)

Not used elsewhere in git. Is this needed? Maybe a single $GIT_DIR/objects/info/new-alternates.tmp or similar would be good enough.

> +    # save the git from repeatedly reading a 0 length file
> +    if test $(stat -c "%s" $alternates) -eq 0; then
Not used elsewhere in core git.  test -s can help.
> +# Option parsing

See OPTIONS_SPEC in git-repack.sh for an example of how to simplify this.

Show 6 quoted lines
> +# Now go and do it
> +case $oper in
> +    add) add_alternate ;;
> +    del) del_alternate ;;
> +    *)   show_alternates ;;
> +esac
Thank you for your excellent work!  Looks very useful.

Regards, Jonathan

Previous: Chris PackhamNext: Junio C Hamano
Message 4 of 17 in “Question about .git/objects/info/alternates”
  1. Chris PackhamMar 22, 2010
  2. Jonathan NiederMar 23, 2010
  3. Chris PackhamMar 24, 2010
  4. Jonathan NiederMar 24, 2010
  5. Junio C HamanoMar 24, 2010
  6. Chris PackhamMar 24, 2010
  7. Chris PackhamMar 25, 2010
  8. 1/2 Add git alternate commandChris Packham, Mar 25, 2010
  9. Junio C HamanoMar 29, 2010
  10. Chris PackhamMar 31, 2010
  11. 2/2 tests for git alternate commandChris Packham, Mar 25, 2010
  12. Johannes SixtMar 25, 2010
  13. Chris PackhamMar 25, 2010
  14. Miklos VajnaMar 26, 2010
  15. Johannes SixtMar 26, 2010
  16. Stephen BoydMar 24, 2010
  17. Chris PackhamMar 24, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.