]> Lady’s Gitweb - HOMEDIR/blob - sh/git.sh
Initial commit
[HOMEDIR] / sh / git.sh
1 #!/usr/bin/env sh
2 # @(#)🏠📂 HOMEDIR sh/git.sh 2026-03-22T02:03:39Z
3 # SPDX-FileCopyrightText: 2026 Lady <https://www.ladys.computer/about/#lady>
4 # SPDX-License-Identifier: MPL-2.0
5
6 ## ⁌ Pseudo Git
7 ##
8 ## ∎ Copyright © 2026 Lady [@ Ladys Computer].
9 ##
10 ## ⋮ This Source Code Form is subject to the terms of the Mozilla
11 ## Public License, version 2.0.
12 ## If a copy of the M·P·L was not distributed with this file, You can
13 ## obtain one at {🔗<https://mozilla.org/MPL/2.0/>}.
14
15 ## § Description
16 ##
17 ## This script enables a “splitting” of Git working directories and
18 ## underlying repos, with pseud‐specific configurations.
19 ## It is intended to accommodate cloud syncing services where one
20 ## intends for the working copy of files to be synced, but not the
21 ## underlying Git directory (which is stored in a different
22 ## location).
23 ##
24 ## Working directory paths must have the format
25 ## `${path_PSEUDDIR}/~${PSEUD}/${REPONAME}´; that is, the first
26 ## directory inside of `${path_PSEUDDIR}´ which begins with a `~´
27 ## is taken to be a pseud, and the first directory inside of that is
28 ## taken as the repository root.
29 ## For working directories which do not match this format, this script
30 ## just calls Git normally.
31 ##
32 ## The corresponding repository for a working directory is taken to be
33 ## `${path_REPODIR}/~${PSEUD}/${REPONAME}.git´.
34 ## These are effectively bare repositories.
35 ## If no repository is found, one is created.
36
37 ## § Configuration
38 ##
39 ## The following variables provide hooks to override the commands used
40 ## by this script.
41 ##
42 ## As this script is expected to override `git´ in the `${PATH}´, the
43 ## actual path to the Git binary must be used.
44
45 : "${cmd_GIT:=/usr/bin/git}"
46 : "${cmd_MKDIR:=mkdir}"
47 : "${cmd_PRINTF:=printf}"
48 : "${cmd_PWD:=pwd}"
49 : "${cmd_SED:=sed}"
50 : "${cmd_TEST:=test}"
51 : "${cmd_TOUCH:=touch}"
52
53 ## The `path_PSEUDDIR´ variable gives the directory which contains the
54 ## actual underlying Git repository for the current directory.
55 ## By default, it is the first of the following which returns an actual
56 ## directory :⁠—
57 ##
58 ## • `~/Documents/Pseuds´
59 ##
60 ## • `~/Pseuds´
61 ##
62 ## • `~/pseuds´
63
64 if "${cmd_TEST}" -z "${path_PSEUDDIR}"
65 then :
66 if "${cmd_TEST}" -d "${HOME}"'/Documents/Pseuds'
67 then :
68 path_PSEUDDIR="${HOME}"'/Documents/Pseuds'
69 elif "${cmd_TEST}" -d "${HOME}"'/Pseuds'
70 then :
71 path_PSEUDDIR="${HOME}"'/Pseuds'
72 else :
73 path_PSEUDDIR="${HOME}"'/pseuds'
74 fi
75 fi
76
77 ## The `path_REPODIR´ variable gives the directory which contains the
78 ## actual underlying Git repository for the current directory.
79 ## By default, it is the first of the following which returns an actual
80 ## directory :⁠—
81 ##
82 ## • `~/Git´
83 ##
84 ## • `~/git´
85 ##
86 ## • `~/Repositories´
87 ##
88 ## • `/srv/git´
89
90 if "${cmd_TEST}" -z "${path_REPODIR}"
91 then :
92 if "${cmd_TEST}" -d "${HOME}"'/Git'
93 then :
94 path_REPODIR="${HOME}"'/Git'
95 elif "${cmd_TEST}" -d "${HOME}"'/git'
96 then :
97 path_REPODIR="${HOME}"'/git'
98 elif "${cmd_TEST}" -d "${HOME}"'/Repositories'
99 then :
100 path_REPODIR="${HOME}"'/Repositories'
101 else :
102 path_REPODIR='/srv/git'
103 fi
104 fi
105
106 ## § Implementation
107 ##
108 ## The `realpwd´ variable holds the “real” path to the current
109 ## directory.
110
111 realpwd="$(
112 "${cmd_PWD}" -P
113 )"
114
115 ## The `workdir´ variable holds the full path to the working directory,
116 ## taken to be the first directory after the first directory
117 ## beginning with a `~´.
118
119 workdir="$(
120 "${cmd_PRINTF}" '%s\n' "${realpwd}" |
121 "${cmd_SED}" '\%/~.*/%!d;s%\([^~]*/~[^/]*/[^/]*\)\(/.*\)*%\1%'
122 )"
123
124 ## If `${workdir}´ does not begin with `${path_PSEUDDIR}´, it is nulled
125 ## out.
126
127 if "${cmd_TEST}" "${workdir##"${path_PSEUDDIR}"}" = "${workdir}"
128 then :
129 workdir=
130 fi
131
132 ## The `reponame´ variable holds the pseud and repository name.
133
134 reponame="$(
135 "${cmd_PRINTF}" '%s\n' "${workdir}" |
136 "${cmd_SED}" 's%[^~]*/\(~[^/]*/[^/]*\)%\1%'
137 )"
138
139 ## The `pseudname´ variable holds just the pseud.
140
141 pseudname="$(
142 "${cmd_PRINTF}" '%s\n' "${reponame}" |
143 "${cmd_SED}" 's%\(~[^/]*\)/.*%\1%'
144 )"
145
146 ## If the repo name is null, this script just calls Git normally.
147
148 if "${cmd_TEST}" -z "${reponame}"
149 then :
150 "${cmd_GIT}" "$@"
151
152 ## Otherwise, it must call Git specially.
153 ## The configuration file at `${path_REPODIR}/${pseudname}/.gitconfig´
154 ## is included into the Git configuration.
155
156 else :
157 configfile="${path_REPODIR}"'/'"${pseudname}"'/.gitconfig'
158 baredir="${path_REPODIR}/${reponame}.git"
159
160 ## If the pseud directory does not exist within `${path_REPODIR}´, it
161 ## is created.
162
163 if "${cmd_TEST}" ! -d "${path_REPODIR}"'/'"${pseudname}"
164 then :
165 "${cmd_MKDIR}" -p "${path_REPODIR}"'/'"${pseudname}"
166 fi
167
168 ## If the configuration file does not already exist, it is created.
169
170 if "${cmd_TEST}" ! -f "${configfile}"
171 then :
172 "${cmd_TOUCH}" "${configfile}"
173 fi
174
175 ## If the repository does not already exist, it is initialized with a
176 ## Sha·256 format.
177
178 if "${cmd_TEST}" ! -d "${baredir}"
179 then :
180 cd -P "${workdir}"
181 "${cmd_GIT}" \
182 -c "include.path=${configfile}" \
183 --git-dir="${baredir}" init \
184 --object-format=sha256
185 cd -P "${OLDPWD}"
186 fi
187
188 ## Finally, Git is run with the appropriate Git directory and work
189 ## tree.
190
191 "${cmd_GIT}" \
192 -c "include.path=${configfile}" \
193 --git-dir="${baredir}" \
194 --work-tree="${workdir}" \
195 "$@"
196 fi
This page took 0.068495 seconds and 5 git commands to generate.