Tuesday, March 7, 2017

[How To]: Not commit when a string is present in git

The Use Case
Howdy! So, I ran into an interesting problem this week. In order to test something, I had to make a change to code that should NEVER be commit... then, I commit it. D'oh!

If you want to protect yourself from something similar, or prevent data leakage, read on.

The Setup
When you clone or initialize a git project, git creates a "./.git" folder for you. This is where it tracks information about the repository, and stores some git metadata and logs.

Another thing this folder contains are "hooks", which are run at some point during the git workflow. For instance, if we had a "pre-push" hook, it would run before the push goes up to the remote.

So, what can we do with these hooks? Anything that BASH lets us. The most common use case for hooks is stopping a commit if something isn't right with the project, which is what we'll be doing in our case. However, you could do things like:

  • Print out the total number of lines changed
  • Check for a string and just give warnings (i,e mispellings, swear words)
  • Queue a local or remote backup
  • Package the project
Really, anything.

Protecting Ourselves
We would like to not commit if a string is present in the file. Imagine if we wanted to search for //[DANGER]. The presence of [DANGER] in any tracked file would stop the commit, so you could remember to go back and change the reason for it being there.

To do this, open the pre-commit.sample file and save it as pre-commit (with no extension). If there isn't a sample file, simply make the pre-commit file.

Before we write any code, let's look at what we want:

  • If a string is present ANYWHERE in the directory, fail
Alternatively, in large code bases, we want:
  • Search the diff for newly added instances of the string
Since searching through every file is going to take too long to do on every commit.

The Steps

Make the File
You should now have a file called ./.git/hooks/pre-commit. You make need to mark the pre-commit file as executable with chmod + x pre-commit.

Add this code to the file

printf "[Check]: Searching for commit mollyguard: '%s'..." "$breakingString"
sleep 1
#                                                                       VVVVVVV YOUR REGEX RIGHT THERE
if [ $(git diff --word-diff --cached --diff-filter=ACUM -U0 | grep -E "+*\[DANGER\]" | wc -l) -gt 0 ]
printf "\n"
cat <<\EOF
Error: Trying to commit a tree that contains uncommitable changes 

Search the projects for $breakingString and figure out why the comment is there
exit 1;

printf "Good!\n"

What this code is doing: 
  • Look at the last diff
  • Search the diff for "+{string}" (because + means it was ADDED, not removed in the last commit)
  • Count the lines
If the lines are greater than 0, and instance was found, so we shouldn't commit this branch.

Of course, this is searching just the latest commit, because searching a large software project is going to take a while. If you're working on a smaller project (or on a very fast machine), you can replace the if statement with simply

if [ $(grep -R "\[DANGER\]"../../  | wc- l) > 0 ]

Sunday, March 5, 2017

.NET - "%5C" Showing Up on URLs

Are you a developer, who is accepting a file or some string of text and putting it into a cloud? Are you, also, per chance, retrieving its URL, and notcing this pesky "%5C popping up in our URLs? Well, I know recently I was. It was a pretty strange, out of the blue error with an interesting cause. Here are some details for you:

What Causes This?
The actual causes of this problem are numerous. The bottom line is, '%5C' in a URL means that the backslash '\' got encoded in the URL. Somewhere along the way, either unintentionally or on purpose, a string containing one wound up in the encoding step.

The culprit for many users is using .NET's Path utility for trying to parse URLs. Here's an example:

Let's look at expectation vs Reality

Code: Path.GetDirectoryName("I/want/to/upload/to/this/place")
Expected: "I/want/to/upload/to/this/"
Actual: "I\want\to\upload\to\this"

This is because Path uses a field called Path.DirectorySeperatorChar, which on Windows is '\'... A bit backwards that Path can READ forward slashes as directory names, yet only output backslashes in the return value.

How Do I Fix This?
There are plenty of good solutions to fix this. However, with so many different causes to the problem, it's hard to say exactly.

To Fix it in a simple string
It's possible that all you need to do is Path.GetDirectoryName(your_string).Replace(Path.DirectorySeperatorChar, Path.AltDirectorySeperatorChar), and you're good to go.

Hopefully this shed some light on what the problem may be in your case. Hopefully, moving forward, Microsoft is not afraid to add a .NET feature like PathFlags or something so that this doesn't crop up as much as it currently does.

Saturday, November 12, 2016

Installing Racer

Let's talk about Rust for a moment, folks. If C is akin to the Bourgeoise, then Rust is kind of like Robin Hood. It takes ideas from C, and implements them in safe and better ways. In my opinion, it's almost foolish to start a new C project today, with all the extensibility of the High-level languages today and the safety and relative ease-of-use of languages like Rust and Go. One front where Rust is lacking, however, is IDE integration. At the time of writing, there is an Active RFC for features in the Rust compiler that will make it easier for IDE plugin authors to add Rust integration. Thankfully, in the interim, there's the Racer project which does something similar.

What does Racer do?
Racer provides automcompletion for Rust code. It does this by sending incomplete symbols in your Rust source file to racer, searching the standard library for a candidate, and providing the name and type information as you go.

So, in order for Racer to work, we need the Rust source code.

How do I install Racer?
The easiest way is with Cargo, Rust's package manager. The following commands should get you up and running. You need to have cargo, rustc, and git installed for these to work.

In the following commands, simply replace .bashrc with .zshrc. Everything else should stay the same.

Install racer from crates.io
cargo install racer

Add the cargo directory to your $PATH variable

echo "export PATH=\"\$PATH:$HOME/.cargo/bin\"" >> .bashrc

Download the rust source code 
git clone --depth 1 https://github.com/rust-lang/rust.git $HOME/.rustsrc

Set your RUST_SRC_PATH environment variable to look for your rust src checkout
echo "export RUST_SRC_PATH=\"$HOME/.rustsrc/src\"" >> .bashrc

Sunday, July 31, 2016

Setting up Emacs for Haskell

Haskell is a bit of a quirky language. It has been around for a very long time, yet hasn't nearly received the professional love that languages like Java, C#, or even F# have gotten. Professional IDE's for the language doesn't exist; good IDE plugins for the languages barely do. So, it has become standard within Haskell community that either Emacs or Vim be used. In this article we'll be examining how to start working with Emacs and Haskell effectively.

First Things First
We have to get Emacs and Haskell installed, if they're not already. If you already have emacs and ghc installed, then you can safely skip this step. Otherwise, you need to install the following packages:

emacs (Version 24 or greater)
ghc (Version 7.5 or greater)

sudo apt-get install ghc emacs
sudo dnf install ghc emacs
sudo pacman -S ghc emacs
The next steps are much easier to perform if you already have Emacs set to look at the MELPA repositories. If you'd like instructions on how to set up MELPA, I've written a guide about it Here.

Emacs Packages

The Emacs package which controls Haskell can be found in the "haskell-mode" package. The two other helpful packages that are also in the MELPA repositories are haskell-emacs, which lets you make Emacs extensions in Haskell, and haskell-snippets, which will present a certain type of code completion into Emacs for haskell by the use of yasnippet.

To install the necessary packages, do:

M-x package-install [ret] haskell-mode [ret]
M-x package-install [ret] yasnippet
M-x package-install [ret] haskell-snippets [ret]
haskell-snippets and yasnippet are completely optional, but they will make your experience far better.

 A bit of a frustrating trend with haskell-mode is that it's making more and more features opt-in, in order to reduce the memory overhead of using haskell mode. What this means for the average user is inconvenience in configuring haskell-mode.

In your ~/.emacs file (or wherever you prefer to extend Emacs from), put in the following lines:

(add-hook 'haskell-mode-hook 'haskell-indentation-mode)
(add-hook 'haskell-mode'hook 'interactive-haskell-mode)
(add-hook 'haskell-mode-hook 'yas-minor-mode)
Or, if you plan to use all of these features:
(add-hook 'haskell-mode-hook (lambda () (interactive) (haskell-indentation-mode) (interactive-haskell-mode) (yas-minor-mode)))
This makes it so that when you enter haskell-mode (which you do when you open any .hs file), the haskell indentation mode, interactive haskell mode, and the yas minor mode are all enabled.

haskell-mode is what provides syntax highlighting for haskell, amongst a few other features.
haskell-indentation-mode is what allows you to automatically indent haskell source with text
interactive-haskell-mode is what lets you run Haskell code right in Emacs to check for errors, make tests, etc.
yas-minor-mode lets you insert snippets of Haskell code into the file you're in, which makes working with Haskell far easier, due to the immense amount of syntax sugar that Haskell makes use of.

Using Emacs for Haskell
Let's make sure that we have everything working. In Emacs, open up a file called "test.hs" and add the following lines:

myFunction :: (a -> b) -> a -> b
myFunction f a = f a
myFunction2 :: Int -> Int
myFunction2 a = myFunction (+ 2) a
Now, do C-c C-l (hitting c and l while holding control). If everything is installed correctly, it should show you a lambda and a greater-than symbol. This is a ghci prompt, which will evaluate your current file and let you interact with the functions and data types you've defined in this file or any that are imported into it.

Just to see what would happen when there's an error, change the first line to this:
myFunction :: (a -> b) -> a -> c 
And try to run C-c C-l again. Notice that it leaves a white exclamation point to the right of the problem in the source code, and underlines exactly where the error is in red.
  as well as showing you the error in the bottom part of the window
This is how your Emacs workflow will go.

Further Reading
There is plenty more to do with Emacs and Haskell, but this will get you started. You can activate true code completion, get linting messages, etc. through the use of an extra extension called ghc-mod. However, ghc-mod is currently broken in cabal... Whenever the version in the cabal repositories is fixed, I will add information about ghc-mod to the guide.

If your distribution has ghc-mod, it's would betroth you to read about installing it in Emacs here.

How To: Change your desktop background in BSPWM

Howdy! So, you've installed bspwm and you noticed that it's not as fancy as what you're used to. No fancy settings for changing your desktop environment, no 'settings', nothing. We're left to our own devices.

Thankfully, Linux is full of devices for us to use. One of which, feh, will let us change what wallpaper is used by bspwm.

Install feh

The first step is to install feh. Feh is small program used to view images without using a ton of resources. Luckily, feh has been adapted to work as a background setter.

    sudo apt-get install feh
    sudo dnf install feh
    sudo pacman -S feh

Use Feh
In order to simply use feh as a photo viewer, you can call "feh picture.jpg", where "picture.jpg" is any image file that ImageMagick can open. In order to use feh to create a desktop background, call this command:

feh --bg-center picture.jpg
Once again, where picture.jpg is just about any image file.

And there you have it! A desktop background... for now. The thing is, after this session, the background will revert to normal. In case you want this to remain your desktop background, we need to edit out bspwmrc file.

Configure feh
 In your bspwmrc file, which is usually located in ~/.config/bspwm/bspwmrc, we need to call feh on startup. On mostly any line, preferably towards the end of the file, add this line:
feh --bg-center ~/Pictures/background.jpg
where the filepath is the path to the file you plan to use for your background. Now, when you start bspwm, it should set up the background for you automatically.

Further Configuration

Now, there are further ways that you can configure feh. Right now it's set to center the image in the middle of the desktop, surrounding it with a black border. If you'd prefer to have it stretch, or tile, you can use these flags in place of --bg-center:

--bg-fill : This will preserve the aspect ratio of the file, and optionally cut off whatever part of the image doesn't scale.

--bg-max : this will scale the image to its maximum size, and put black borders on one side.

--bg-scale : Will distort the image to fit exactly within the confines of the desktop. Since this doesn't preserve the aspect ratio, stretching may be a result.

--bg-tile : Will repeat the image until it fits within the monitor.

Advanced Configuration
So, now you know how to get the desktop set up in bspwm. But, bspwm "configurability" is a bit more than your average configurability. We can do some pretty cool stuff with the fact that we're using a shell to set the desktop background.

How about instead of the earlier command, we put this in the configuration file:

feh --bg-center $(find ~/Pictures/Backgrounds -type f | shuf -n 1)
So that it randomly chooses a background out of a "Backgrounds" folder in your Pictures directory?

Anything like this is possible in bspwm. You can have it change based on the time of day, or the type of music playing, or... anything really. The choice is always yours with BSPWM!

How To: Install bspwm

Are you a Gnome or KDE user who is tired of the ordinary? Are you an i3 or herbstluftwm user who has heard the benefits of using bspwm? If you are, you have probably looked into it. And, furthermore, when you looked into it you may have found that it's a bit more involved than you had hoped. No worries! Following this guide should get you up and running with your very own bspwm configuration.

Install the Packages
Installing the packages is, unfortunately, a little but harder than just calling apt-get. We have to compile them. Now, a bit more fortunately, all of the dependencies for bspwm are readily available in your repositories.

On Ubuntu:
sudo apt-get install xcb libxcb-util0-dev libxcb-ewmh-dev libxcb-randr0-dev libxcb-icccm4-dev libxcb-keysyms1-dev libxcb-xinerama0-dev libasound2-dev libxcb-xtest0-dev

On Fedora:
sudo dnf install xcb-util-devel xcb-util-keysyms-devel xcb-util-wm-devel alsa-lib-devel dmenu rxvt-unicode terminus-fonts

Not too bad. What these commands do is install the dependencies that will be needed when we're building bspwm.

Build Tools
Since we're manually building bspwm, we need certain build tools. Libraries, compilers, etc. 
sudo apt-get install gcc make
sudo dnf groupinstall "Development Tools"
Alrighty. So, we have the dependencies and the build tools. Now, we need to download BSPWM.

The Packages

Let's download the two packages using git, now.
cd Downloads
git clone https://github.com/baskerville/bspwm.git
git clone https://github.com/baskerville/sxhkd.git
If all went well, you should have two folders in your 'Downloads' folder - "bspwm" and "sxhkd"

The final step to installing the bspwm system itself is compiling it. To do this, go into the Downloads folder and type 

cd bspwm
sudo make install

   Now, if you're not used to compiling things, this process probably looks a tad scary. There should be a lot of text scrolling along in the terminal - that is okay. Unless you see "Error, exiting" during this step, everything is working fine.

After this, issue:

cd ..
cd sxhkd
sudo make install
If everything went well, we're ready to start doing some of the initial configuration. Awesome!


Since bspwm and sxhkd are both installed, let's take a minute to analyze exactly where we are. We've installed bspwm, the window manager, and sxhkd, the keyboard daemon. How bspwm works this:

bspwm manages your windows. Where they're placed, their layout on your desktop, etc. It doesn't handle anything else, unlike traditional window managers.

bspc tells bspwm what to do. When you see bspwm moving windows, closing windows, etc. it's because bspc told bspwm over its socket file.

sxhkd listens to the keyboard for keyboard shortcuts. These keyboard shortcuts can execute any arbitrary command, although they are commonly used to issue bspc commands, such as the command that tells bspc to close a window, etc.

Running - With a display manager
Display managers control your X11 / Wayland (what makes your computer graphical, instead of all commands) sessions, as well as logging in. When you start your computer and see "username" and "password", you're looking at your display manager. Some common display managers on Linux are lightdm (commonly ubuntu), gdm (commonly gnome), lxdm (commonly lxde), and kdm (commonly kde).

bspwm comes with the files necessary to add the window manager to the display manager you already use. In order to use your display manager to log in, you need to copy the "bspwm.desktop" file (from bspwm/contrib/freedesktop). In the Downloads folder:

sudo cp bspwm/contrib/freedesktop/bspwm.desktop /usr/share/xsessions
Now, you can restart your display manager, or choose to reboot. bspwm should be an option in your display manager. You can skip the "Without a display manager" section and move onto configuration.

Running - Without a display manager

Without a display manager, things are a bit more advanced. I'm assuming that if you're choosing to forego using a display manager, that you're a fairly advanced user, so I'll keep it short. Change the contents of your ~/.xinitrc file to say
exec bspwm
And you'll be good to go. But wait! We have to configure the keybindings.

Keybindings and Autostart
Now, you should have bspwm completely installed on your system. However, if you tried to log into bspwm right now you would be greeted with... well, nothing. And, for the moment, that's fine. But the bad part is you won't be able to do anything either. In order to fix this, we need to set ourselves up some keybindings.

Bspwm comes with files with default configurations. Let's get started with the window manager by copying these default keybindings and behavior's over to their respective directories. First, we must create those directories.

mkdir ~/.config/bspwm
mkdir ~/.config/sxhkd

bspwm's configuration:

cp ~/Downloads/bspwm/examples/bspwmrc ~/.config/bspwm/
chmod +x ~/.config/bspwm/bspwmrc
sxhkd's configuration:

cp ~/Downloads/bspwm/examples/sxhkdrc ~/.config/sxhkd
Further configuration
Now, it may be preferable to edit either of these files. The function of bspwmrc in .config/bspwm is essentially an "autostart" file. It lists commands that bspwm should run when it starts up. Now, remember, bspwm is controlled via commands, so rules which govern how bspwm behaves are located in this file.

Things such as how monitors should be handled, window borders, rules for allowing certain windows to float, etc. are all configured here, as well as any programs you want to run when bspwm starts up (like, say, your bluetooth daemon).

sxhkdrc in .config/sxhkd controls keybindings. Keybindings for bspc are common, and important to make note of. By default, some of the most important default commands are:

super + return | urxvt
super + space | dmenu_run
super + escape | [reload sxhkd configuration]
super+alt+Escape | logout of bspwm
super+m | Change the desktop mode

These commands, especially super+return should get you started.
Note: super is the mod4 key on the keyboard, commonly called the "Windows" key as it usually dons the Windows logo
Now, with all of these files in place, we should be ready to start bspwm for the first time. If you don't have urxvt already installed, it's important to issue:

sudo dnf install rxvt-unicode
sudo apt-get install rxvt-unicode
So that you're able to use the terminal when you start using bspwm. The terminal is essential, as it will allow you to modify your sxhkd configuration once you're in bspwm.

If you'd like dmenu (it's an application launcher), issue:

sudo dnf install dmenu

sudo apt-get install suckless-tools
Using bspwm
 Now, start bspwm. If you installed bspwm into your display manager, you can select it from the menu there. If you chose to use .xinitrc, then use "startx" at a tty.

It may seem like it's not working when you start it. It will either show a black screen or the "splash screen" of the display manager when you first log into it.

This is how it looks on Ubuntu

Since we installed urxvt, we can launch the terminal with super+enter, or whatever you changed it to.

By default, it takes up the entire screen like this. But, notice that when you open another program, it will split the total space in half.

This is showcasing bspwm's tiling ability. However, bspwm can do so much more than the default sxhkd and bspwmrc files allow you to do.

Well, there we have it. We now have a working bspwm configuration, and we're ready to get tinkering! Typing "man bspc" at a terminal will show you all of the awesome things that you can do with bspwm, so there's plenty to try out in the new desktop.

This is the beginning of a series of articles about bspwm and sxhkd on this blog. For more, see below, where I'll be adding additional content as it is made:

Modifying keybindings in bspwm : [In Progress]

Happy hacking!

Huge thanks to https://github.com/windelicato/dotfiles/wiki/bspwm-for-dummies by windelicato, where most of this guide was sourced from. I paraphrased much of this guide from his original writeup here for the purpose of clearing up the process, and fixing one mistake he made in the original guide.

What is a Monad? What is a Higher Kinded type?

If you're stumbling upon this document, I'm guessing that you're just getting into a statically typed programming language and are (rightfully) confused about what all the kerfuffle regarding Monads and Higher-Kinded types are. This blog article will clear all the confusion you have about what monads and higher kinded types really are, but how to use higher kinded types (in, say, Haskell) is not covered here.

Background - Static Types
Before we can really talk about what a monad is, let's take a step back. In terms of programming, monads only exist in programming langauges which support strong static typing. That means, that the type system of the programming language follows a mathematical type system. See: Wikipedia.  The reason that this is so neat is that Type Theory is the basis of all of mathematics. Every other branch, including Calculus, Algebra, Trigonometry, Statistics, etc. can all be defined in terms of Type Theory operations.

In programming languages, types denote the type of data you're working with. Numbers may be classified as "int" or "double" in a lot of languages, which are different types which represent numbers. Text may be of type "string", etc. Languages that have "strong" static types take this a step further, being a direct representation of their type-theory counterparts.

Background - Type Theory
Types in type theory work as such: there are two variations of information. 'Types' and 'Kinds'. Kinds are, in a way, the father of types.

The kind, denoted "*", is literally pronounced "type". It is used to describe any type that doesn't take a type argument (more on this later). This can include things like functions, ints, doubles, strings... pretty much every type that you're experienced with, outside of generics.

The kind * -> * is where things begin to be interesting. This is about as far as Haskell and many other strongly-typed languages truly go (with the exception of kind (* -> *) -> *, the higher-order kind). This is the kind of any type that takes one type parameter... a little unclear? Read on.

Higher Kinded Types and Monads
* -> * you can think of as a "template template". Think of, in Java, this class:

class myClass<T>{
  T myItem;

In this case, the type "myClass" is the first * and the type "T" is the second *. In Haskell, which is strongly statically typed, a data type declaration, such as

data List a = Node a | Rest (List a)

In this case here, List a has the kind "* -> *" because it takes one type parameter (a). 

The important thing to realize about this, is that kinds of this type can be generecized within the type declarations of other functions. As an example of exactly what that means:

myFunction :: l a -> [a]

would take any * -> * that takes 'a' as a type paramter, and returns an array of that type from it.

Where do Monads fit into the equation?
Monads are a concept from category theory. There, monads are an endofunctor paired with two natural operations. The details of this are interesting, but outside the scope of this quick run-down of HKT's and monads. Check out Category Theory if you'd like to know more.

The function declaration of monad's function is as such:

Monad m => m a -> (a -> m b) -> m b

So, as you can see, Monads are only possible with the use of Higher-Kinded types ((m a) and (m b), make the kind * -> *). Monads also define particular functions which follow the 'monadic laws', however, these are unrelated to their basis in the type system.

Type theory is a gigantic branch of mathematics, and this quick run-down of how types work in statically typed languages does not even closely begin to scratch the surface. Hopefully you've taken away a bit more about what it means to be "higher kinded", and a bit how monads use the type system to exist.

If you'd like to learn more or correct me on something I said wrong up above, by all means feel free to comment!