Skip to content

Commit

Permalink
Updates from upstream.
Browse files Browse the repository at this point in the history
  • Loading branch information
danfuzz committed Sep 28, 2023
1 parent 8ba2ae3 commit 80d1b7c
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
113 changes: 113 additions & 0 deletions scripts/lib/bashy-basics/buildy/ls-files
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/bin/bash
#
# Copyright 2022-2023 the Bashy-lib Authors (Dan Bornstein et alia).
# SPDX-License-Identifier: Apache-2.0

. "$(dirname "$(readlink -f "$0")")/_init.sh" || exit "$?"


#
# Argument parsing
#

define-usage $'
${name} [<path> ...]
List files in the project at or under the given <path>s, respecting
`.gitignore` (etc.). With no <path>s, lists all project files.
--full-paths
Output full paths. By default, outputs paths that are relative to the
project\'s base directory.
--output=<style> :: `json` `lines` `raw0`
* `json` -- Output a JSON array of all file paths. This is the default.
* `lines` -- Output newline-terminated file paths.
* `raw0` -- Output NUL-terminated file paths.
--include=<regex>
Regular expression indicating which files to include.
${help}
'

# Want help?
opt-action --call='{ usage; exit }' help/h

# Full paths?
opt-toggle --var=fullPaths full-paths

# Output style.
opt-value --var=outputStyle --init=json --enum='json lines raw0' output

# Include pattern.
opt-value --var=includeRegex --init='.' --filter='/./' include

# The paths.
rest-arg --var=paths --filter='/./' paths

process-args "$@" || usage --short


#
# Helper functions
#


#
# Main script
#

baseDir="$(base-dir)" || exit "$?"

# Canonicalize the paths.
error=0
for i in "${!paths[@]}"; do
p="${paths[i]}"
newPath="$(readlink -f "${p}")" || {
error-msg "No such path: ${p}"
error=1
continue
}

if [[ ! (${newPath} =~ ^"${baseDir}/") ]]; then
error-msg "Not a project path: ${p}"
error=1
fi
paths[i]="${newPath}"
done

if (( error )); then
exit 1
fi

cd "${baseDir}"

results=()

while IFS='' read -r -d $'\0' file; do
if [[ ${file} == $'\x01error' ]]; then
exit 1
fi
if [[ ${file} =~ ${includeRegex} ]]; then
if (( fullPaths )); then
file="${baseDir}/${file}"
fi
results+=("${file}")
fi
done < <(
# Note: The `|| printf` is to make it possible to unambiguously notice
# errors coming from `git ls-files`.
git ls-files -z --cached --others --exclude-standard "${paths[@]}" \
|| printf '\x01error\x00'
)

case "${outputStyle}" in
json)
jarray --input=strings "${results[@]}"
;;
lines)
printf '%s\n' "${results[@]}"
;;
raw0)
printf '%s\x00' "${results[@]}"
;;
esac
19 changes: 19 additions & 0 deletions scripts/lib/bashy-basics/buildy/pull-repo
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ define-usage $'
directly under the `repos` directory under the (configurable) `out`
directory.
If the repo exists and there are uncommitted changes, then those changes
get stashed.
--clone-config=<git-config>
Add a repo configuration, as with the `--config` option of `git clone`.
May be used more than once to add multiple configs. This _only_ has an
Expand Down Expand Up @@ -90,6 +93,13 @@ function actually-pull {

(
cd "${destDir}"

if any-changes; then
progress-msg ' Stashing uncommitted changes...'
git 1>&2 stash --quiet || return "$?"
progress-msg ' Now updating for realsies...'
fi

true \
&& git checkout --quiet "${mainBranch}" \
&& git 1>&2 pull --quiet "${idArgs[@]}" \
Expand All @@ -108,6 +118,15 @@ function actually-pull {
fi
}

# Checks to see if there are any changes in the repo containing the CWD.
function any-changes {
git update-index

git diff-index --quiet HEAD -- \
&& return 1 \
|| return 0
}

# Determines the main branch name of the repo in the given directory.
function main-branch-name {
local dir="$1"
Expand Down

0 comments on commit 80d1b7c

Please sign in to comment.