Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts

Tuesday, August 21, 2018

git bash - deleted local branches

You can find out which branches have been merged into a branch by running the below:

$ git branch --merged origin/master
* master
  feature/some-branch
  feature/some-other-branch

This will also print out master as part of it, so we can then pipe that into grep to match results without master:

$ git branch --merged origin/master | grep -v master
  feature/some-branch
  feature/some-other-branch

Obviously, you won't want the name master in your branch name, otherwise it'll be excluded too. You can fancier with regex here.

Now we can take this result and pipe it into xargs, to run git branch -d passing the output as arguments

$ git branch --merged origin/master
  | grep -v master
  | xargs -r git branch -d

I'm splitting up the command onto separate lines for readability. The -r will only run the git command if there is output.

I've noticed this doesn't always delete all old branches, but it can help. For example, perhaps you had a branch that was a work in progress, or a spike, with changes that aren't merged - this won't be picked up.

You can do one more trick to detect old branches that have no upstream branch anymore:

$ git branch -vv

This will list branches without an upstream branch with gone. Those can generally be deleted too.

Monday, August 20, 2018

Today I learned - VSTS CLI and jq

Today I learned about the VSTS command line tool from a colleague.

I might actually have heard about it before, but wasn't able to try it out because I was previously using the on-premise TFS and not the cloud based VSTS, but hey, today I tried it out :)

For example, you can query VSTS for pull requests you've created by doing the following:

$ cd /your-vsts-git-repo
$ vsts login
$ vsts code pr list --creator "James Barrow"

This will return a JSON object containing info about what pull requests you have created.

If you don't really care about the entire structure, you can then use another command line tool I've come across before called jq.

For example, if you only care about the title, status and url of the pull request, you could do the following:

$ vsts code pr list --creator "James Barrow" | jq ".[] | { title, status, url }"

That's a long command, but what it basically does is pipes the JSON result into jq, which parses it by taking each element in the array of the result, and then filtering it to only take the title, status and url properties of each item in the array.

And finally, since we all know the best way to use git is via the command line and git bash, you can then also create a bash alias in your ~/.bashrc file:

alias prs='vsts code pr list --creator "James Barrow" | jq ".[] | {title,status,url}"'

So you can just check for your pull requests now by simply running:

$ prs
{
  "title": "RE #1234 - some pull request",
  "status": "active",
  "url": "https://blah.visualstudio.com/.../pullRequests/2345"
}

Which gives you something like that ;)

Much simpler than having to open up Chrome, go to VSTS, find the right tab for pull requests, etc...

I want to dig more into using jq to pull out info on if the pull requests have had votes as well, so I can just run a command from my command line and see if I need to follow up on a PR or not, but this will have to wait for another day :)

One last note is that use of single and double quotes might be important depending on your shell of choice, so keep that in mind too.

Hope this helped someone.

UPDATE: There is also vsts code pr list -o table which prints a nice format

Wednesday, June 13, 2018

PowerShell Tips

I'll likely update this as I go along


Create a file like you would with touch

echo $null > filename

Wednesday, May 16, 2018

git-run for managing many repositories

Some may make use of a monorepo, for example over there at Google, however for those of us that don't and have to deal with multiple repositories, possibly unrelated ones across various projects, then a great tool to have in your toolbelt is git-run, or just gr.

It can keep track of where your repositories are located, and can also be used to tag the various repositories. You can then run a git command across all of these repositories (or based on a tag).

To set it up:

$ npm install -g git-run
$ git tag discover PATH

One way I have been using that is to create a tag per project, so across a project which may have many repositories, I can see my git status across all of those by executing:

$ gr @project status

It can also run normal shell commands as well

$ gr @project cat package.json


One thing I realised it didn't do in my git bash shell, was run my aliases that I tend to use (e.g. I have a complicated bash alias for a git log command). But luckily someone else has already got an open pull request to fix this (at least for bash), which suited my needs. Yay for open source!

Try it out and let me know what you think :)

Update: I also recently discovered when trying it on a new computer - make sure you don't forget to add tags to the repositories it discovers during gr tag discover, because it won't save its configuration and you'll get an error.

Monday, March 26, 2018

Package manager tips

Initially I was just going to post about how to install npm packages while offline, but in case I ever come back to update this page with more tips or package manages, this post is now titled "Package manager tips".

npm tips

  1. Configure initial values for npm init -y
    • npm config set init.author.name YOUR_NAME
    • npm config set init.author.email YOUR_EMAIL
    • npm config set init.license UNLICENSED
  2. Installing a package offline:
    • versions older than npm 5, use the local-npm package
    • npm install --prefer-offline
  3. Update a package, e.g. Angular CLI
    • npm uninstall -g @angular/cli
    • npm cache verify # if npm < 5, npm cache clean
    • npm install -g @angular/cli@latest
  4. View the versions of a package:
    • npm view webpack version --json
    • npm view webpack versions --json

yarn tips

  1. Configure initial values for yarn init -y
    • yarn config set init-author-name YOUR_NAME
    • yarn config set init-author-email YOUR_EMAIL
    • yarn config set init-license UNLICENSED

Friday, January 19, 2018

CommonAssemblyInfo

I'm writing this post since I posted this on Twitter, and within half an hour, was asked to share my knowledge:


In Visual Studio, there's a little known feature that lets you add files as links to a project. Using this trick, we can also ease maintenance slightly by having a CommonAssemblyInfo.cs file where common attributes across the assemblies in a solution can be applied, but maintained in a single file.

Here is the sample demo application to show the steps one normally goes through. We have an existing command line utility to calculate tax based on an entered amount and tax rate. Quite simple.


In the Properties folder there is an AssemblyInfo.cs file listing the various attributes applied to the assembly.


This isn't always set up as expected, because most people forget to look at this file. One thing that stands out for example is the Company and Copyright information is defaulted sometimes to Microsoft or some other vendor based on the system settings. This may be undesirable, and so we can update these settings.


See some links at the bottom of this page for more info on these attributes.



You can see these are shown in the properties on the executable that is built.

Maybe we go back to look at our application and want to separate out the domain aspects of the console application, perhaps because in future we may want to share that with a web application frontend. The first step one would do is creating a new project and moving the classes around, fixing references, etc.


But you'll notice that most of these attributes are the same. The only two that maybe would be different are actually just the title of the assembly and the Guid it uses. If we rename things someday, we'd have to update at least two files (maybe we rename CommonAssemblyInfoDemo to something more useful, for example).

The first thing I usually do when creating a solution with more than one project, is first creating a file called CommonAssemblyInfo.cs that lives in the same directory as the solution file (CommonAssemblyInfoDemo.sln). This is usually done outside of Visual Studio, and then using Visual Studio to add the existing item to the solution, to make it easier to edit. I also setup solution folders that are numbered to keep things nicely organised.


You'll notice that only the common attributes are left in this file. But we still need to incorporate it into the other projects.

To do this, we also need to add it as an existing item to each project, however, there is a trick - we need to add it as a link.




You'll notice in the Solution Explorer that the icon of the file has a link symbol in it, to indicate it's just a reference to an existing file, so that all links refer to the same file.

Now we just update each projects AssemblyInfo to just include the information related specifically to that project.


If we need to update the AssemblyVersion across the entire solution, it's now a simple matter of just updating the CommonAssemblyInfo file to do this, instead of going through each project and remembering to update each one.

Hope this helped someone, the extra effort when setting up a new project can sometimes save time in the future.

For example, if we had a continuous integration build, and we just wanted to update the version number across all these artifacts based on some number set in our CI system, we could just update the CommonAssemblyInfo file before we run the solution through the build.

Links




Thursday, March 23, 2017

tail on Windows with PowerShell

One of the well known Unix/Linux commands is tail, which gets the tail of a file, meaning the end of a file and prints it out to the output stream.

I used to try find nice programs for this, including Baretail which has some very nice features like colouring lines in that match certain patterns etc.

But if you just want a simple, and now built into Windows solution, just use PowerShell:

Get-Content -Tail 10 filename.txt

This will show the last 10 lines of the file. You can even follow the tail, or watch it for changes, as below:

gc -Tail 10 -Wait filename.txt

Another nice thing you can do is then pipe this into Select-String to filter the output:

gc -Tail 10 -Wait filename.txt | Select-String -Pattern somepattern

Tuesday, July 21, 2015

Visual Studio tip: Hide selected text

Today I noticed the menu item Edit > Outlining > Stop Hiding Current (CTRL+M CTRL+U), and wondered to myself what it does. Well, turns out you can hide selected areas of text and then later unhide them. But how do you hide them in the first place??
"The option to hide a selection only shows in the Outlining menu once you've selected text" - Captain Obvious
Thank you Captain Obvious!
So as an example, lets say we have one of the controllers for a sample ASP.NET MVC project, and just want to inspect it to see what it does, we see the below action with a long bunch of lines:
404 - Image Not Found
We go through the first few lines and realise that's all just code related to validation, followed by loading up an item and some more validation. We're not really interested in the validation just yet so we can just collapse it by selecting those lines, and going Edit > Outlining > Hide Selection (or if like me you like keyboard shortcuts, CTRL+M followed by CTRL+H). We now can see what the method's actually trying to do, and can easily ignore the validation code for now:
404 - Image Not Found
404 - Image Not Found
If later you do want to see the folded stuff, it's as easy as expanding the code again. It seems that VS remembers this custom code folding section for later on when I close and open the file, and even after closing VS itself (though I wouldn't rely on it to be there forever... I'm guessing it's stored in some local user settings file).
I've seen people use the #region directive a lot for this kind of thing - making the code easier to read - but then someone else who doesn't like #regions will delete it later, or someone will restructure it. The advantage of doing this is that it doesn't affect anyone else on your team.
To remove the custom code folded section, just do the opposite, Edit > Outlining > Stop Hiding Current (CTRL+M CTRL+U) ... H for hide, U for unhide.
Another use case for this would be doing code reviews, or maybe if you're presenting pieces of code that you don't want the audience to read and get too far ahead of the presentation.

Thursday, February 14, 2013

Git tip - diff of only modified files

I was wondering how to see a diff of only those items that were modified in my Git workspace, and thanks to Stackoverflow, you can use the --diff-filter parameter:

git diff --diff-filter=M
git dt --diff-filter=M

The second one is if you use a visual diff tool. I prefer WinMerge, might do a post on my setup at some point, it's pretty simple once you know how.

Wednesday, February 1, 2012

Symbolic links and hard links in Windows

A lot of people coming from the Linux world would probably have at some point used symbolic links or soft links, and hard links.

Windows has had shortcuts for a long time, but these are actually files that are used to point to other files, they aren't actually entries in the filesystem that merely reference other files, which is what the links are, meaning that shortcuts take up more space.

A lesser known fact about Windows is that it actually also has symbolic links, hard links, and also with Windows NTFS has the concept of a junction point, which is a hard link for a directory. The differences between them is discussed quite nicely in the post "Windows File Junctions, Symbolic Links and Hard Links".

It also appears that the capability has been around for a while, going back to Windows 2000. With Windows 7 and probably most newer Windows versions these days, there's also a nice command line tool mklink to help with this.

Open up a command line, type mklink /? and then try it out for yourself in a test area, playing with creating new files, linking to them, deleting the original files, seeing how the links are affected, etc.

Disclaimer: If you delete something that wasn't just a pointer to a file, and was the actual file, that's your own fault, don't come looking for me :D

Thursday, June 9, 2011

Starting Windows Services Remotely

Did you know you can use the sc command to start services remotely?

sc \\SERVERNAME start SERVICENAME

e.g.

sc \\test-server query AppFabricCachingService
sc \\test-server start AppFabricCachingService

Tuesday, May 10, 2011

Visual Studio Clipboard Ring

Today I found out about the Visual Studio Clipboard Ring, which keeps the last 15 entries that you have entered into the clipboard using copy and cut (CTRL+C, CTRL+V, SHIFT+DEL), and lets you paste into the Visual Studio Editor an item from that list.

You just press CTRL+SHIFT+V when pasting, and each successive press will cycle through the entries. Very useful if you kept something on the clipboard, but then pressed SHIFT+DEL to delete a line, forgetting about what you wanted to paste! Which I tend to do a lot!