-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-add-empty-files
More file actions
executable file
·51 lines (40 loc) · 942 Bytes
/
git-add-empty-files
File metadata and controls
executable file
·51 lines (40 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env bash
set -o errexit
function main() {
declare -a files
local create_new=""
while [ "$#" -gt 0 ]; do
local arg="$1"; shift
case "$arg" in
"--new"|"-n")
create_new="yes"
;;
*)
files+=( "$arg" )
;;
esac
done
if [ ${#files[@]} -le 0 ]; then
echo "Need at least one file name"
return 1;
fi
for file in "${files[@]}"; do
add_empty_file "$create_new" "$file"
done
}
function add_empty_file() {
local create_new="$1"
local filepath="$2"
local mode="100644"
if [ -z "$create_new" -a ! -f "$filepath" ]; then
echo "File '$filepath' doesn't seem to exist."
return 2
fi
local status="$(git status --ignored --porcelain=v1 "$filepath")"
if [ "${status:0:3}" != "?? " -a "${status:0:3}" != "" ]; then
echo "Can only add untracked files -- '$status'"
return 3
fi
git update-index --add --cacheinfo "${mode},$(git hash-object -t blob -w /dev/null),${filepath}"
}
main "$@"