ref: a6b7833442b430a1db521b0e2d18d2cda1763920
parent: a4bde0a775f6d7d1ddb2346e096df3591b3adbb7
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Oct 9 11:45:34 EDT 2020
query: chdir to root of repo for queries add util for repo finding, use it in git/query.
--- a/conf.c
+++ b/conf.c
@@ -35,25 +35,7 @@
return 0;
}
-static void
-showroot(void)
-{
- char path[256], buf[256], *p;
- if((getwd(path, sizeof(path))) == nil)
- sysfatal("could not get wd: %r");
- while((p = strrchr(path, '/')) != nil){
- snprint(buf, sizeof(buf), "%s/.git/HEAD", path);
- if(access(buf, AEXIST) == 0){
- print("%s\n", path);
- return;
- }
- *p = '\0';
- }
- sysfatal("not a git repository");
-}
-
-
void
usage(void)
{
@@ -66,7 +48,7 @@
void
main(int argc, char **argv)
{
- char *file[32], *p, *s;
+ char *file[32], repo[512], *p, *s;
int i, j, nfile, findroot;
nfile = 0;
@@ -77,8 +59,12 @@
default: usage(); break;
}ARGEND;
- if(findroot)
- showroot();
+ if(findroot){
+ if(findrepo(repo, sizeof(repo)) == -1)
+ sysfatal("%r");
+ print("%s\n", repo);
+ exits(nil);
+ }
if(nfile == 0){
file[nfile++] = ".git/config";
if((p = getenv("home")) != nil)
--- a/git.h
+++ b/git.h
@@ -278,6 +278,7 @@
int hassuffix(char *, char *);
int swapsuffix(char *, int, char *, char *, char *);
char *strip(char *);
+int findrepo(char *, int);
/* packing */
void dtinit(Dtab *, void *, int);
--- a/query.c
+++ b/query.c
@@ -130,7 +130,7 @@
int i, j, n;
Hash *h;
char *p, *e, *s;
- char query[2048];
+ char query[2048], repo[512];
ARGBEGIN{
case 'p': fullpath++; break;
@@ -139,6 +139,10 @@
}ARGEND;
gitinit();
+ if(findrepo(repo, sizeof(repo)) == -1)
+ sysfatal("find root: %r");
+ if(argc == 0)
+ usage();
fmtinstall('P', Pfmt);
s = "";
p = query;
--- a/util.c
+++ b/util.c
@@ -256,3 +256,25 @@
vfprint(2, fmt, ap);
va_end(ap);
}
+
+/* Finds the directory containing the git repo. */
+int
+findrepo(char *buf, int nbuf)
+{
+ char *p, *suff;
+
+ suff = "/.git/HEAD";
+ if(getwd(buf, nbuf - strlen(suff) - 1) == nil)
+ return -1;
+
+ for(p = buf + strlen(buf); p != nil; p = strrchr(buf, '/')){
+ strcpy(p, suff);
+ if(access(buf, AEXIST) == 0){
+ *p = 0;
+ return 0;
+ }
+ *p = '\0';
+ }
+ werrstr("not a git repository");
+ return -1;
+}
--- a/walk.c
+++ b/walk.c
@@ -136,26 +136,6 @@
r->npath = o + 1;
}
-static void
-findroot(void)
-{
- char path[256], buf[256], *p;
-
- if(access("/mnt/git/ctl", AEXIST) != 0)
- sysfatal("no running git/fs");
- if((getwd(path, sizeof(path))) == nil)
- sysfatal("could not get wd: %r");
- while((p = strrchr(path, '/')) != nil){
- snprint(buf, sizeof(buf), "%s/.git", path);
- if(access(buf, AEXIST) == 0){
- chdir(path);
- return;
- }
- *p = '\0';
- }
- sysfatal("not a git repository");
-}
-
int
sameqid(Dir *d, char *qf)
{
@@ -224,7 +204,7 @@
void
main(int argc, char **argv)
{
- char *rpath, *tpath, *bpath, buf[8];
+ char *rpath, *tpath, *bpath, buf[8], repo[512];
char *p, *e;
int i, dirty;
Wres r;
@@ -254,7 +234,11 @@
usage();
}ARGEND
- findroot();
+ if(access("/mnt/git/ctl", AEXIST) != 0)
+ sysfatal("no running git/fs");
+ if(findrepo(repo, sizeof(repo)) == -1)
+ sysfatal("find root: %r");
+ chdir(repo);
dirty = 0;
memset(&r, 0, sizeof(r));
if(access("/mnt/git/ctl", AEXIST) != 0)