# Adapting scripts to work in current (not top) directory

5 messages from 2005-05-12 to 2005-05-13. Participants: Alexey Nezhdanov, H. Peter Anvin, Petr Baudis.
Thread: https://gitlist.dev/t/584

## Alexey Nezhdanov, 2005-05-12 13:58

Subject: Adapting scripts to work in current (not top) directory
Message-ID: <200505121758.10971.snake@penza-gsm.ru>
URL: https://gitlist.dev/e/200505121758.10971.snake%40penza-gsm.ru

```
All git and cogito scripts wants .git subdirectory. If I'm in a subdirectory 
that have no .git direcory in it I'm out of luck.
I have wrote an example script that determines the lowest possible .git 
directory position and changes to it to satisfy user request.

Problems with script:
1) May be I misunderstood the git ideology and it needs not this at all.

if point (1) is false then there are couple of other problems:
2) Script is extremelly ugly. I'm a week bash programmer so please criticize.
3) This logic shold be somehow embedded to all git- and cg- scripts. I can not 
figure how to do it non-intruisively.
4) files and patch with spaces not supported. Probably fixable but first I 
want to resolve points (1), (2) and (3)

===========================
#!/bin/bash
#
# Add new file to a GIT repository.
# Copyright (c) Petr Baudis, 2005
#
# Takes a list of file names at the command line, and schedules them
# for addition to the GIT repository at the next commit.

. ${COGITO_LIB:-/home/snake/lib/cogito/}cg-Xlib

[ "$1" ] || die "usage: cg-add FILE..."

gitpath=
subpath=
curpath=`pwd`
for ((i=2;i<9999;i=i+1)) ; do {
        path1=`echo $curpath | cut -d / -f 0-$i`
        path2=`echo $curpath | cut -d / -f $((i+1))-`
        [ -d "$path1"/.git ] && gitpath=$path1 && subpath=$path2
        [ "$path1" == "$curpath" ] && break
}; done

for file in "$@"; do
        if [ -f "$file" ]; then
                echo "Adding file $file"
        else
                die "$file does not exist"
        fi
done

cd "$gitpath"

files=

for file in "$@"; do
        files="$files $subpath/$file"
done

git-update-cache --add -- $files
===========================

-- 
Respectfully
Alexey Nezhdanov


```

## H. Peter Anvin, 2005-05-12 19:15

Subject: Re: Adapting scripts to work in current (not top) directory
Message-ID: <4283AB68.1090003@zytor.com>
URL: https://gitlist.dev/e/4283AB68.1090003%40zytor.com
In-Reply-To: <200505121758.10971.snake@penza-gsm.ru>

```
Alexey Nezhdanov wrote:
> All git and cogito scripts wants .git subdirectory. If I'm in a subdirectory 
> that have no .git direcory in it I'm out of luck.
> I have wrote an example script that determines the lowest possible .git 
> directory position and changes to it to satisfy user request.
> 
> Problems with script:
> 1) May be I misunderstood the git ideology and it needs not this at all.
> 

Linus has explicitly said he doesn't want that.

	-hpa

```

## Petr Baudis, 2005-05-12 20:12

Subject: Re: Adapting scripts to work in current (not top) directory
Message-ID: <20050512201215.GI324@pasky.ji.cz>
URL: https://gitlist.dev/e/20050512201215.GI324%40pasky.ji.cz
In-Reply-To: <200505121758.10971.snake@penza-gsm.ru>

```
Dear diary, on Thu, May 12, 2005 at 03:58:10PM CEST, I got a letter
where Alexey Nezhdanov <snake@penza-gsm.ru> told me that...
> All git and cogito scripts wants .git subdirectory. If I'm in a subdirectory 
> that have no .git direcory in it I'm out of luck.

This is fine for Cogito, but Git itself shouldn't care - unless you mean
the bundled mini-plumbing scripts. I don't know if anyone (and who) uses
them except Linus, but I'm not likely to make much effort to maintain
them, or even to actually accept any non-trivial changes to them.

> I have wrote an example script that determines the lowest possible .git 
> directory position and changes to it to satisfy user request.
> 
> Problems with script:
> 1) May be I misunderstood the git ideology and it needs not this at all.

Cogito really needs it.

> if point (1) is false then there are couple of other problems:
> 2) Script is extremelly ugly. I'm a week bash programmer so please criticize.
> 3) This logic shold be somehow embedded to all git- and cg- scripts. I can not 
> figure how to do it non-intruisively.

Add it to cg-Xlib. You can just update $_git appropriately. (Except when
you were explicitly passed GIT_DIR.)

> gitpath=
> subpath=
> curpath=`pwd`
> for ((i=2;i<9999;i=i+1)) ; do {
>         path1=`echo $curpath | cut -d / -f 0-$i`
>         path2=`echo $curpath | cut -d / -f $((i+1))-`
>         [ -d "$path1"/.git ] && gitpath=$path1 && subpath=$path2
>         [ "$path1" == "$curpath" ] && break
> }; done

I would gradually trim the $curpath by $(dirname) until I hit
$curpath/.git or the root directory.

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor

```

## Alexey Nezhdanov, 2005-05-13 10:59

Subject: Re: Adapting scripts to work in current (not top) directory
Message-ID: <200505131459.08891.snake@penza-gsm.ru>
URL: https://gitlist.dev/e/200505131459.08891.snake%40penza-gsm.ru
In-Reply-To: <20050512201215.GI324@pasky.ji.cz>

```
On Friday, 13 May 2005 00:12 Petr Baudis wrote:
> Dear diary, on Thu, May 12, 2005 at 03:58:10PM CEST, I got a letter
> where Alexey Nezhdanov <snake@penza-gsm.ru> told me that...
>
> > All git and cogito scripts wants .git subdirectory. If I'm in a
> > subdirectory that have no .git direcory in it I'm out of luck.
>
> This is fine for Cogito, but Git itself shouldn't care - unless you mean
> the bundled mini-plumbing scripts. I don't know if anyone (and who) uses
> them except Linus, but I'm not likely to make much effort to maintain
> them, or even to actually accept any non-trivial changes to them.
>
> > I have wrote an example script that determines the lowest possible .git
> > directory position and changes to it to satisfy user request.
> >
> > Problems with script:
> > 1) May be I misunderstood the git ideology and it needs not this at all.
>
> Cogito really needs it.
Ok, so sticking with cogito.

> > if point (1) is false then there are couple of other problems:
> > 2) Script is extremelly ugly. I'm a week bash programmer so please
> > criticize.
> > 3) This logic shold be somehow embedded to all git- and cg- 
> > scripts. I can not figure how to do it non-intruisively.
>
> Add it to cg-Xlib. You can just update $_git appropriately. (Except when
> you were explicitly passed GIT_DIR.)
ok, that's easy.
>
> > gitpath=
> > subpath=
> > curpath=`pwd`
> > for ((i=2;i<9999;i=i+1)) ; do {
> >         path1=`echo $curpath | cut -d / -f 0-$i`
> >         path2=`echo $curpath | cut -d / -f $((i+1))-`
> >         [ -d "$path1"/.git ] && gitpath=$path1 && subpath=$path2
> >         [ "$path1" == "$curpath" ] && break
> > }; done
>
> I would gradually trim the $curpath by $(dirname) until I hit
> $curpath/.git or the root directory.
I have opted for current directory and not root. Explanation is later this 
mail. New variant:
=========================
--- /home/snake/scm/cogito/cg-Xlib      2005-05-12 08:47:29.000000000 +0400
+++ cg-Xlib     2005-05-13 13:40:58.000000000 +0400
@@ -8,7 +8,17 @@
 
 _cg_cmd=${0##*/}
 
-_git=${GIT_DIR:-.git}
+repopath=`pwd`
+while ((1)) ; do {
+  [ -d "$repopath/.git" ] && break
+  [ "$repopath" == "/" ] && {
+    repopath=`pwd`;
+    break;
+  };
+  repopath=`dirname $repopath`
+}; done
+
+_git=${GIT_DIR:-$repopath/.git}
 _git_objects=${GIT_OBJECT_DIRECTORY:-$_git/objects}
=========================
Here is a problem:
Because of git backend only some of the commands should be enabled to work 
from current directory. F.e. cg-init should not scan upper directories for 
".git" but should instead stick to current directory. In the script above 
this workarounded as choosing current directory instead of root but this will 
broke if we trying to initialise repo in a subdirectory of another repo.
So I have analysed list of cogito commands to find out which ones should be 
adopted to use "find repo" behaivoir:

"+" means "definitely yes"
"-" means "definitely no"
"?" means "not sure"
"." means "not matter"

+ cg-add
? cg-admin-lsobj
- cg-admin-uncommit
? cg-branch-add
? cg-branch-ls
- cg-cancel
- cg-clone
- cg-commit
+ cg-diff
- cg-export
. cg-help
- cg-init
+ cg-log
+ cg-ls
- cg-merge
+ cg-mkpatch
? cg-patch
- cg-pull
- cg-restore
+ cg-rm
- cg-seek
+ cg-status
- cg-tag
? cg-tag-ls
- cg-update
. cg-version

Any opinions?

-- 
Respectfully
Alexey Nezhdanov


```

## Petr Baudis, 2005-05-13 13:53

Subject: Re: Adapting scripts to work in current (not top) directory
Message-ID: <20050513135359.GA28138@pasky.ji.cz>
URL: https://gitlist.dev/e/20050513135359.GA28138%40pasky.ji.cz
In-Reply-To: <200505131459.08891.snake@penza-gsm.ru>

```
Dear diary, on Fri, May 13, 2005 at 12:59:08PM CEST, I got a letter
> I have opted for current directory and not root. Explanation is later this 
> mail. New variant:

Yes, that's fine.

> =========================
> --- /home/snake/scm/cogito/cg-Xlib      2005-05-12 08:47:29.000000000 +0400
> +++ cg-Xlib     2005-05-13 13:40:58.000000000 +0400
> @@ -8,7 +8,17 @@
>  
>  _cg_cmd=${0##*/}
>  
> -_git=${GIT_DIR:-.git}
> +repopath=`pwd`
> +while ((1)) ; do {
> +  [ -d "$repopath/.git" ] && break
> +  [ "$repopath" == "/" ] && {
> +    repopath=`pwd`;
> +    break;
> +  };
> +  repopath=`dirname $repopath`
> +}; done
> +
> +_git=${GIT_DIR:-$repopath/.git}
>  _git_objects=${GIT_OBJECT_DIRECTORY:-$_git/objects}

Looks basically fine.

> =========================
> Here is a problem:
> Because of git backend only some of the commands should be enabled to work 
> from current directory. F.e. cg-init should not scan upper directories for 
> ".git" but should instead stick to current directory. In the script above 
> this workarounded as choosing current directory instead of root but this will 
> broke if we trying to initialise repo in a subdirectory of another repo.

I'd set some flag like $reposubdir if .git was found in some of the
parent directories. Then test that in the relevant commands.

> So I have analysed list of cogito commands to find out which ones should be 
> adopted to use "find repo" behaivoir:
> 
> "+" means "definitely yes"
> "-" means "definitely no"
> "?" means "not sure"
> "." means "not matter"
> 
> + cg-add
> ? cg-admin-lsobj

Sure.

> - cg-admin-uncommit
> ? cg-branch-add
> ? cg-branch-ls

Sure (both).

> - cg-cancel
> - cg-clone
> - cg-commit

This one should, committing only the subdirectory content.

> + cg-diff
> - cg-export

Perhaps it could export just the subdirectory?

> . cg-help
> - cg-init
> + cg-log
> + cg-ls

(Listing the appropriate subdir.)

> - cg-merge
> + cg-mkpatch
> ? cg-patch

This one should, using the appropriate -p level.

> - cg-pull
> - cg-restore

This one should, restoring only the subdirectory.

> + cg-rm
> - cg-seek
> + cg-status
> - cg-tag
> ? cg-tag-ls

Why not...

> - cg-update
> . cg-version

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor

```
