summaryrefslogtreecommitdiff
path: root/sys/src/cmd/git/commit
blob: 5b61fe1eb2a8d1661bd62c88f5e4f2377c7d9d2d (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/rc -e
rfork ne
. /sys/lib/git/common.rc

fn findbranch{
	branch=`{git/branch}
	if(test -e $gitfs/branch/$branch/tree){
		refpath=.git/refs/$branch
		initial=false
	}
	if not if(test -e $gitfs/object/$branch/tree){
		refpath=.git/HEAD
		initial=false
	}
	if not if(! test -e $gitfs/HEAD/tree){
		refpath=.git/refs/$branch
		initial=true
	}
	if not
		die 'invalid branch:' $branch
}

# Remove commentary lines.
# Remove leading and trailing empty lines.
# Combine consecutive empty lines between paragraphs.
# Remove trailing spaces from lines.
# Ensure there's trailing newline.
fn cleanmsg{
	awk '
	/^[ 	]*#/ {next}
	/^[ 	]*$/ {empty = 1; next}

	wet && empty {printf "\n"}
	{wet = 1; empty = 0}
	{sub(/[ 	]+$/, ""); print $0}
	'
}

fn editmsg{
	if(! test -s $msgfile.tmp){
		>$msgfile.tmp {
			echo '# Author:' $name '<'$email'>'
			echo '#'
			for(p in $parents)
				echo '# parent:' $p
			git/walk -fAMR $files | subst '^' '# '
			echo '#'
			echo '# Commit message:'
		}
		edit=1
	}
	if(! ~ $#edit 0){
		giteditor=`{git/conf core.editor}
		if(~ $#editor 0)
			editor=$giteditor
		if(~ $#editor 0)
			editor=hold
		$editor $msgfile.tmp
	}
	cleanmsg < $msgfile.tmp > $msgfile
	if(! test -s $msgfile)
		die 'empty commit message'
}

fn parents{
	if(! ~ $#revise 0)
		parents=`{cat $gitfs/HEAD/parent}
	if not if(test -f .git/index9/merge-parents)
		parents=`{cat .git/index9/merge-parents | sort | uniq}
	if not if(~ $initial true)
		parents=()
	if not
		parents=`{git/query $branch}
}

fn commit{
	msg=`''{cat $msgfile}
	if(! ~ $#parents 0)
		pflags='-p'^$parents
	hash=`{git/save -n $"name -e $"email -m $"msg $pflags $files || die $status}
	rm -f .git/index9/merge-parents
}

fn update{
	mkdir -p `{basename -d $refpath}
	# Paranoia: let's not mangle the repo.
	if(~ $#hash 0)
		die 'botched commit'
	echo $branch: $hash
	echo $hash > $refpath
	for(f in $files){
		if(test -e .git/index9/removed/$f || ! test -e $f){
			rm -f .git/index9/removed/$f
			rm -f .git/index9/tracked/$f
		}
		if not{
			mkdir -p `{basename -d $f}
			walk -eq $f > .git/index9/tracked/$f
		}
	}
}

fn sigexit{
	if(~ ! $#msgfile 0)
		rm -f $msgfile $msgfile.tmp
}

gitup

flagfmt='m:msg message, r:revise, e:edit'; args='[file ...]'
eval `''{aux/getflags $*} || exec aux/usage

msgfile=/tmp/git-msg.$pid
if(~ $#msg 1)
	echo $msg >$msgfile.tmp
if not if(~ $#revise 1){
	msg=1
	echo revising commit `{cat $gitfs/HEAD/hash}
	cat $gitfs/HEAD/msg >$msgfile.tmp
}

files=()
if(! ~ $#* 0)
	files=`$nl{git/walk -c `$nl{cleanname -d $gitrel $*}}
if(~ $status '' || ~ $#files 0 && ! test -f .git/index9/merge-parents && ~ $#revise 0)
	die 'nothing to commit' $status
@{
	flag e +
	whoami
	findbranch
	parents
	editmsg
	commit
	update
} || die 'could not commit:' $status
exit ''