threads / discuss / 584

Adapting scripts to work in current (not top) directory

Subject: Adapting scripts to work in current (not top) directory

## tl;dr

5 messages between May 12, 2005 and May 13, 2005.

replies: 4people: 3as markdown or json

Alexey Nezhdanov· May 12, 2005, 13:58 UTC · lore

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· May 12, 2005, 19:15 UTC · re: Alexey Nezhdanov · lore

Re: Adapting scripts to work in current (not top) directory

Alexey Nezhdanov wrote:
Show 8 quoted lines
> 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· May 12, 2005, 20:12 UTC · re: Alexey Nezhdanov · lore

Re: Adapting scripts to work in current (not top) directory

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.

Show 5 quoted lines
> 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.)

Show 9 quoted lines
> 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· May 13, 2005, 10:59 UTC · re: Petr Baudis · lore

Re: Adapting scripts to work in current (not top) directory

On Friday, 13 May 2005 00:12 Petr Baudis wrote:
Show 18 quoted lines
> 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.
Show 8 quoted lines
> > 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.
Show 13 quoted lines
>
> > 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· May 13, 2005, 13:53 UTC · re: Alexey Nezhdanov · lore

Re: Adapting scripts to work in current (not top) directory

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.
Show 20 quoted lines
> =========================
> --- /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.
Show 7 quoted lines
> =========================
> 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.

Show 10 quoted lines
> 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.
Show 5 quoted lines
> + 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

← back to recent threads