about summary refs log tree commit diff
path: root/functions/git_functions.sh
blob: afbc16c700a5a3e19465316a8a8771413af03427 (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# output current branch to STDOUT
function wgbranch {
  cat ./.git/HEAD | perl -p -e 's/^ref:\srefs\/heads\/(.+)$/\1/g'
}


# edit git conflicts one-by-one
function vconflicts() {
  $EDITOR $(git status --porcelain | awk '/^UU/ { print $2 }')
}


function git-tasks {
  echo "fix - bug patching"
  echo "refactor - changing structure; functionality remains unchanged"
  echo "feat - introducing a new feature"
  echo "style - updating UI / UX"
  echo "chore - changing configuration, adding comments, etc"
  echo "wip - placeholder tag signifying ongoing work"
  echo "build - updating anything related to building and deploying"
  echo "deps - updating related to project dependencies"
  echo "docs - updating related to project documentation"
}


function git-discard {
  option=$1

  if [[ $option == '' ]]; then
      echo "Please supply option: --staged, --unstaged, --untracked, or --all"
  fi

  if [[ $option == '--all' ]]; then
      git-discard --staged && git-discard --unstaged && git-discard --untracked
  fi

  if [[ $option == '--staged' ]]; then
      staged_files=$(git --no-pager diff --name-only --staged)

      echo -n "Discarding staged..." &&
      git reset HEAD $staged_files >/dev/null &&
      echo "done."
  fi

  if [[ $option == '--unstaged' ]]; then
      unstaged_files=$(git --no-pager diff --name-only)

      echo -n "Discarding unstaged..." &&
      git checkout -- $unstaged_files >/dev/null
      echo "done."
  fi

  if [[ $option == '--untracked' ]]; then
      untracked_files=$(git ls-files --others --exclude-standard)

      echo -n "Discarding untracked..."
      for file in $untracked_files; do
        if [ -f $file ]; then
            rm $file
        else
          rm -rf $file
        fi
      done
      echo "done."
  fi
}


function wgd {
  input=$1
  git diff "./**/*/${input}*"
}


function wga {
  input=$1
  git add "./**/*/${input}*"
}


# Outputs staged, unstaged, untracked files
# Similar to `git status` output but without the cruft
function wg-git-changed-files {
  tracked_staged=$(wg-diff-tracked-staged)
  tracked_unstaged=$(wg-diff-tracked-unstaged)
  untracked_unstaged=$(wg-diff-untracked-unstaged)

  echo "${tracked_staged}\n${tracked_unstaged}\n${untracked_unstaged}"
}

function wg-diff-tracked-staged {
  git --no-pager diff --name-only --staged
}

function wg-diff-tracked-unstaged {
  git --no-pager diff --name-only
}

function wg-diff-untracked-unstaged {
  git ls-files --others --exclude-standard
}


# git status "plumbing" version
# Useful for piping into grep -> xargs git add
function wgst {
  git status -s | awk '{ print $2 }'
}


# git add by file regex pattern
function wgadd {
  pattern="$2"

  wgst | grep "${pattern}" | xargs git add
}


# compare file with another branch
function wgcompare_file {
  file_path="$1"
  compare_branch_a="master"
  compare_branch_b="$(wgbranch)"

  git diff "${compare_branch_a}:${file_path}" "${compare_branch_b}:${file_path}"
}


# output the stash ticket number to STDOUT
function wgtix {
  wgbranch | perl -p -e 's/(?:feature|bugfix|refactor)\/(\w+-\d+).+$/\1/'
}


# search for a git branch by ticket number
# useful when combined with `wgcheckout`
# e.g.
# $ wgcheckout "$(wgfind 1045)"
# checks-out feature/GDMX-1045 ...
#
# if the `TICKET_NO` cannot be found, it will return the current branch
function wgfind {
  TICKET_NO="$1"

  BRANCHNAME=$(git branch | grep "$TICKET_NO" | perl -p -e 's/^\s*//')

  if [ -z $BRANCHNAME ]; then
    BRANCHNAME="$(wgbranch)"
  fi

  echo "$BRANCHNAME"
}


# wrapper fn for "git checkout" that exports previous branch to env
function wgcheckout {
  if [ -z $1 ]; then
      branchname="develop"
  else
    branchname="$1"
  fi

  echo " -- wgcheckout -- "
  echo "Storing branch \"$(wgbranch)\" in WGPREV ..."
  export WGPREV="$(wgbranch)"
  echo "Checking out \"$branchname\" ..."
  echo
  echo " -- git checkout -- "
  git checkout "$branchname"
  echo
}


# opens the current ticket-branch in web browser
function wgjira {
  base_url="https://jira.hugeinc.com/browse"
  ticket=$(wgtix)

  open "${base_url}/${ticket}"
}


# wgcheckout combined with a fuzzy search
function wgfcheckout {
  branchname=$(trim $(git branch | fzf-tmux))

  [ ! -z "$branchname" ] && wgcheckout "$branchname" || return
}


# View an author's work within a specified date range.
function wgviewcommits {
  author=$([ -z "$1" ] && echo "William Carroll" || echo "$1")
  todays_date=$(date +'%Y-%m-%d')
  date=$([ -z "$2" ] && echo "${todays_date}" || echo "$2")

  git log --all --author="${author}" --after="${date} 00:00" \
        --before="${date} 23:59"
}